Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error after I put Async function in useEffect

In the useEffect function, if I just mention the getResults function variable, the app doesn't crash. But when I call it as I am doing in code below, I get these errors:

react-dom.development.js:21857 Uncaught TypeError: destroy is not a function

and

Consider adding an error boundary to your tree to customize error handling behavior.

  function App() {   const [foods, setFoods] = useState([]);   const [isLoaded, setIsLoaded] = useState(false);   useEffect(() => getResponse());   const getResponse = async () => {     const response = await fetch(sampleRequest);     const data = await response.json();     setFoods(data.hits);   };   let query = "Tomato";   let sampleRequest = `https://api.edamam.com/search?q=${query}&app_id=${"1811484f"}&app_key=${"9cac93361efc99e2ebfbb8a453882af8"}`;    return (     <div className="App">       <div className="main">         <div className="navbars">           {" "}           <Navbars></Navbars>         </div>         <div className="listings">           <Listing></Listing>           <Listing></Listing>           <Listing></Listing>           <Listing></Listing>           <Listing></Listing>           <Listing></Listing>           <Listing></Listing>           <Listing></Listing>           <Listing></Listing>           <Listing></Listing>         </div>         <div className="footer">           <h5>Made By YoYo Strangler in 2019</h5>         </div>       </div>     </div>   ); }  export default App; 
like image 957
Irakli Tchigladze Avatar asked Oct 21 '19 23:10

Irakli Tchigladze


People also ask

Why async Cannot be used in useEffect?

Why? Because React's useEffect hook expects a cleanup function returned from it which is called when the component unmounts. Using an async function here will cause a bug as the cleanup function will never get called.

Why does the callback function in the useEffect Hook Cannot be asynchronous?

You cannot directly make the callback function supplied to the useEffect hook async because: async functions implicitly return a promise, and; useEffect expects its callback to either return nothing or a clean-up function.

Is useEffect synchronous or asynchronous?

useEffect is usually the place where data fetching happens in React. Data fetching means using asynchronous functions, and using them in useEffect might not be as straightforward as you'd think. Read on to learn more about it!

Can I use await inside useEffect?

React can run this async function but can not run the cleanup function. Don't use raw async function directly in the useEffect.


Video Answer


1 Answers

You're returning the result of calling getResponse() from the useEffect function. If you return anything from useEffect, it has to be a function. Changing your code to this should fix it because you're no longer returning anything from the useEffect function.

useEffect(() => {    getResponse(); }); 

The useEffect 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.

useEffect(() => {    doSomething();    return () => {     console.log("This will be logged on unmount");   } }); 
like image 142
Nick Avatar answered Sep 24 '22 18:09

Nick