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?
...
For further actions, you may consider blocking this person and/or reporting abuse
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