DEV Community

Discussion on: Mimic React life cycle methods with Hooks

Collapse
 
savagepixie profile image
SavagePixie

While the above effect will run as is, React will show a warning saying that "An effect function must not return anything besides a function, which is used for clean-up." since our effect returns a Promise. To fix this, we'll move the data fetching code to an asynchronous function outside the effect.

You can also use the promise returned and do something simpler like

React.useEffect(() => fetch("https://jsonplaceholder.typicode.com/posts")
   .then(response => response.json())
   .then(setPosts), [])