Avoid UseEffect in react to execute on every updates

React virtual DOM is one of the most important features of #reactjs which is has high performance impact on react applications , but this feature is kind of knife that could be awful if misused .

A misused case of that ,wouldn’t be care of when components should be updated (#class or #functional) or made react to unlimited rerender in a loop .

useEffect (()=>{},[]) hook came to take care of effects and changes in functional component , this hook would be executed in two different senario :

1-Execue once a time when component mounted if pass empty array to second argument

2-Execute each time something get change like props or other hooks or even parent reRender in some conditions

But if we need just know about a particular changes like a particular props or variable we have to pass that into second argument like this

const [count,setCount]=useState(0);

useEffect (()=>{do something},[count])

Leave a Comment