In this post We'll prove that in 90% of our components we don't need to write a useEffect ...
First question, When does useEffect run?
const User = ({name}) => { // 1
const [initials, setInitials] = useState(""); // 2
useEffect(() => {
setInitials(name.split(" ").map(str => str.charAt(0))); // 4
}, [name]);
return (
<p>{initials}</p> // 3
);
};
React will render this component in this sequence:
- Read the prop name, let's say it's value is "John Doe".
- Define the state called initials and give it the empty string as initial value.
- Return the JSX with this initial value, So initials will be an empty string.
- Run the function passed to the useEffect hook which sets the name to be "John Doe"
- Return the new JSX which now has initials as JD
So our component already had the value "John Doe" from beginning, But useEffect made it render twice! ... Let's get rid of this additional render ...
const User = ({name}) => { // 1
const initials = name.split(" ").map(str => str.charAt(0)); // 2
return (
<p>{initials}</p> // 3
);
};
Now React will
- Read the value of name
- Calculate initials
- Return the JSX with the right value, Without the need of rendering the component again!
What if the previous component has more renders due to change in other props, It'll recalculate the initials in every new render, right? Yes, But instead of using a useEffect and putting the name in the dependency array ... We can easily use the useMemo hook not to recalculate the value with every new render!
const User = ({name, age, email}) => { // 1
const initials = useMemo(() => {
name.split(" ").map(str => str.charAt(0)); // 2
}, [name]);
return (
<p>{initials}</p> // 3
);
};
Component will not recalculate initials when age or email changes!
So let's put this rule in our minds whenever We're creating a React component ...
Anything that could be calculated from props or state, Shouldn't be calculated inside a useEffect
We've all misused the useEffect hook to achieve things that could be easily achieved without using it and without the need of having additional renders which decrease page performance.
There's only one case in which you have to use useEffect, If you want to do something when component mounts (displayed for the first time), this thing is external (API fetch, subscribing to event, Applying something to a DOM element), Simply it's not depending on state or props ... So it'll be something like this
useEffect(() => {
const fetchedPosts = fetch('....');
}, [])
Conclusion
- useEffect should be your last choice
- useEffect runs after rendering your component, So if it makes any change to state, it'll cause additional renders
- Anything that could be calculated from props or state, shouldn't be calculated inside a useEffect
- You can use useMemo for expensive calculations
- You can use useEffect only if you want to do something external (Ex: API fetch) when component mounts (First render only)
Thank u!
Top comments (13)
The title of this article can be changed from "Don't use useEffect" to "Don't create dependent states on React"... useEffect should be used to handle side effects of some cases, like the
useOutsideClick
hook of the chakra-ui package.Right, I just wanted to say We shouldn't use useEffect when what we want could be achieved without it.
Thank u!
The wrong use of the useEffect is really a great problem in React (and i got some of it in legacy projects that i work :/)
We all did that 😅
Indeed! If you need to set state based on changes in component state/props, you should use the useMemo hook. useEffect is only for triggering side effects outside of the scope of the component. Like adding event listeners to the window for example or interacting with a library that was not specifically written for React.
Exactly, Thank u!
+100,
useEffect()
should only be used for fetch the initial data (and in some extreme extra situations)good post!
Thank u!
useEffect is very useful but it's Excessive use is very harmful in react project.
Yes exactly
Oh oh. I need to make some changes on my code... I'm using useEffect way too much. Thanks for the share !
You're welcome
You can use React.memo to not rerender the component itself in case of no prop change instead of useMsmo just for calculating initials