The useEffect React hook will run the passed in function on every change. This can be optimized to let it call only when the desired properties change.
What if I want to call an initialization function from componentDidMount
and not call it again on changes? Let's say I want to load an entity, but the loading function doesn't need any data from the component. How can we make this using the useEffect
hook?
class MyComponent extends React.PureComponent { componentDidMount() { loadDataOnlyOnce(); } render() { ... } }
With hooks this could look like this:
function MyComponent() { useEffect(() => { loadDataOnlyOnce(); // this will fire on every change :( }, [...???]); return (...); }
Here is an example of how you would call a function to fetch data only once - when the component mounts. Copied! import {useEffect, useState} from 'react'; const App = () => { const [data, setData] = useState({data: []}); const [err, setErr] = useState(''); useEffect(() => { // 👇️ this only runs once console.
Side Effect Runs Only Once After Initial Render You do not want to make this API call again. You can pass an empty array as the second argument to the useEffect hook to tackle this use case. useEffect(() => { // Side Effect }, []); In this case, the side effect runs only once after the initial render of the component.
If you have just made a new project using Create React App or updated to React version 18, you will notice that the useEffect hook is called twice in development mode. This is the case whether you used Create React App or upgraded to React version 18.
If your application is behaving strangely after updating to React 18, the default behavior of useEffect changed to run it 2 times.
If you only want to run the function given to useEffect
after the initial render, you can give it an empty array as second argument.
function MyComponent() { useEffect(() => { loadDataOnlyOnce(); }, []); return <div> {/* ... */} </div>; }
TL;DR
useEffect(yourCallback, [])
- will trigger the callback only after the first render.
Detailed explanation
useEffect
runs by default after every render of the component (thus causing an effect).
When placing useEffect
in your component you tell React you want to run the callback as an effect. React will run the effect after rendering and after performing the DOM updates.
If you pass only a callback - the callback will run after each render.
If passing a second argument (array), React will run the callback after the first render and every time one of the elements in the array is changed. for example when placing useEffect(() => console.log('hello'), [someVar, someOtherVar])
- the callback will run after the first render and after any render that one of someVar
or someOtherVar
are changed.
By passing the second argument an empty array, React will compare after each render the array and will see nothing was changed, thus calling the callback only after the first render.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With