ReactJS effect hook demystified
What is the effect hook?
The effect hook functions in the same way as the React life cycle methods componentDidMount, componentDidUpdate and componentWillUnmount for class components. The effect hook can be used for managing side effects in a functional component. The effect hook runs after React has rendered the functional component.
The example code below shows the difference between using React class lifecycle methods and effect hook in a functional component:
Functional component example:
In the provided code 'useEffect' method is used to pass in a callback. This callback will be executed when the component is mounted like 'componentDidMount' and when it is updated like 'componentDidUpdate'. So the document title is changed first when the component mounts then each time when component updates.
The functional component can skip the effect conditionally after the first render by passing a second argument to the 'useEffect'. This second argument which is an array will be compared from previous variables that were passed and only execute the effect if the variables have different values.
In the sample code above the 'useEffect' is passed in a second argument which is an array containing 'price' variable. So after the first render this effect will only execute the callback if the value of prices changes over each render.
Cleanup vs non-cleanup:
The code inside 'useEffect' callback shown previoulsy changes the document title which doesn't cause any memory leaks and it doesn't need to be cleaned up in any way after the component unmounts. But in certain cases it may be necessary to cleanup code after each render to avoid memory leaks or bugs. For this reason a function can be returned from the 'useEffect' callback that will execute when the component unmounts and after each render.
In the sample code above each time the component is rendered it calls 'setInterval'. The callback inside 'setInterval' will populate the 'enemyLocations' array every 500 milliseconds. The 'enemyLocations' array is part of the functional component. But if we don't clear the interval after every render then 'setInterval' will never stop and multiple 'setInterval' callbacks will add 1000 locations every 500 milliseconds which will cause memory leak. So by returning a function from 'useEffect' we can clear the interval after every render and when the component unmounts.
- The 'useEffect' API can be used to manage side-effects similar to componentWillMount, componentDidUpdate and componentWillUnmount life cycle methods.
- The effect can be executed conditionally by passing in a second argument to 'useEffect' containing an array of variables to monitor.
- The code inside 'useEffect' can be cleanedup by returning a function which will cleanup any subcribtions or memory after every render and when the component unmounts.