Videos
am a junior full stack dev and my experience with react are limited to school projects. i've always use useEffect hooks and everything is great until i heard my senior devs complaining about the team using too many useEffect hooks in our codebase. things like our components get rendered unnecessarily and slowing down performance. ever since then, i'm very conscious about using useEffect.
so question is, are useEffect hooks really that bad and should i avoid using them at all cost? love to hear from yall cuz this is bothering me a lot and i want to be a better engineer
I shall try to explain it in simple terms. useEffect is one of the most important hooks in react and is a way to handle life cycle of the component in which it is present.
useEffect runs on every render of the component (i.e when a state variable changes) and can also run every time a specific value changes that is mentioned in it's dependency array.
useEffect also provides a very useful cleanup function which can be used to remove any active listeners when the component changes
Here are a few examples:
- useEffect without dependency array
useEffect(() => {
/*the code you want to run has to be in here which will keep running again and
again without stopping*/
})
- useEffect with empty dependency array
useEffect(() => {
/*the code you want to run on every render has to be in here, the empty [] means
that the code will run every time this component mounts*/
},[])
- useEffect with state variables in dependency array
useEffect(() => {
/*the code you want to run on every render has to be in here, the dependency
array with state means that the code will run every time this component mounts as
well as when these state variables change and the value will be captured by the
useEffect*/
},[state1,state2])
- useEffect with state variables in dependency array and cleanup
useEffect(() => {
/*the code you want to run on every render has to be in here, the dependency
array with state means that the code will run every time this component mounts as
well as when these state variables change and the value will be captured by the
useEffect*/
/*cleanup is used to remove any unwanted loops, intervals, listeners when the
component unmounts*/
return () => console.log("clean up");
},[state1,state2])
- A complete example for useEffect from w3schools
import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
let timer = setTimeout(() => {
setCount((count) => count + 1);
}, 1000);
return () => clearTimeout(timer)
}, []);
return <h1>I've rendered {count} times!</h1>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Timer />);
If you still don't understand have a look at this
w3schools link for useEffect
react official documentation for useEffect
To understand useEffect, you have to understand React’s three lifestyle methods: Mounting, when the component is first render; Updating, when the component or state changes; and lastly, Unmounting.
useEffect as it says in the name defines a function (an effect) which will run at one of the three points. To differentiate between the three. You have a dependency array which defines when your effect will run.
For when the first page renders you would use an empty dependency array. For when a state changes you would use a dependency array which would look like this [state] and for when the component unmounts you would have an empty dependency array which would have a return statement in it which would run when it unmounts.
It would look like this:
useEffect(() => {}, [])
useEffect(() => {}, [state])
useEffect(() => {return () = {}}, [])