Skip to main content

React - Uncaught TypeError: destroy is not a function


Written by Aryan Mittal

While experimenting with useEffect hooks in React and React Native, I came across the following error: Uncaught TypeError: destroy is not a function and my app was unable to run. After debugging and searching around, I found the cause and how to solve it.


A simplified version of my code looked like this:

The key here is that myFunction is async and the shorthand arrow function syntax I used.


The reason the simple code above was crashing my app is due to how the useEffect hook, async functions, and the shorthand arrow function syntax works.


One feature of the useEffect hook is a cleanup function. If you return anything from the useEffect hook function, it must be a cleanup function. This function will run when the component unmounts. This can be thought of as roughly equivalent to the componentWillUnmount lifecycle method in class components.


In JavaScript, functions marked with the async keyword enable the use of the await feature, which lets developers pause the execution of a function while waiting for an asynchronous task to finish. Async functions also always return a Promise; if the function doesn’t return one already, the return value is automatically wrapped in a Promise.


Finally, the shorthand arrow function syntax allows developers to omit the curly braces around the function body, which is useful for simple one-liners. The value of the function body automatically becomes the return value of the arrow function, removing the need of the return keyword. This functionality is called Implicit Return.


Now, how do these tidbits come together to cause such a cryptic error? Simply put, the value of myFunction, which is a Promise, becomes the return value of the arrow function in the useEffect hook. Remember that the useEffect hook expects a cleanup function to be returned? A Promise is not a function. so React stumbles and produces the error.


To fix your app, change the useEffect arrow function to add curly braces and remove the Implicit Return, as shown:

Now, the arrow function in the useEffect hook returns undefined, which is acceptable and tells React that there is no cleanup function needed. This will solve the problem, but it would be great if React gave a more useful error message when this occurs!


Comments

Popular posts from this blog

Achieving the iOS “glass” blur with React Native

Written By: Aryan Mittal Perhaps the most common component in every mobile app is the header. It tells your user where they are in your app, and it’s at the very top of the screen. So, it’s crucial to have a polished and native-feeling header in your React Native apps. I use the React Navigation library for Voluntime ’s mobile app, and I wanted to achieve the iOS “frosted glass” blur effect that Apple is known for. Most of Apple’s iOS apps utilize the blurred header, including Settings, the App Store, Notes, and Reminders. It gives the user a hint about what’s hiding behind the header, and it makes the app feel polished and thought out. Since I was using React Navigation, I started by searching for “react navigation blurred header” on Google. The results recommended writing your own custom header or installing additional libraries to achieve the blur effect. This wasn’t something I wanted to do for such a small change, so I dug deeper into React Navigation’s documentation. Finally, aft...

Calculating the number of business days between dates in Python

Written By: Aryav Nagar Calculating the number of business days between varying lengths of time, even excluding federal holidays, may seem like a tricky problem to solve, but the solution is quite simple. While developing Stockscast , an AI-based stock market forecaster, I came across this issue as I needed to find the number of days the stock exchange would be open throughout the year. The solution I found involved the use of pandas.tseries.holiday.USFederalHolidayCalendar() and pandas.tseries.offsets.CustomBusinessDay() functions are found in the Pandas library. By creating a custom calendar, using the CustomBusinessDay function, with the frequency of USFederalHolidayCalendar , we can create a calendar excluding both weekends and federal holidays. Next, find the length of time you want to calculate the number of business days in. In my case, I needed to find the number of business days between one week, one month, three months, six months, and one year from now. Finally, w...