OneCompiler

Where should I make API calls when using React Hooks?

In class components, we usually make the requests from the componentDidMount method

componentDidMount () {
   //code
}

When using hooks how can I do the same?

1 Answer

5 years ago by

React recently added hooks, to replace componentDidMount lifecycle method in classes. useEffect() hook is used instead of componentDidMount and componentDidUpdate, which gets fired when a page loads and also when a state gets changed.

when you specify the second parameter to a state variable, it gets fired only when that state variable gets changed

useEffect (()=>{
//Code
},[state])
5 years ago by Divya