DEV Community

Discussion on: How to use componentWillUnmount with Functional Components in React

Collapse
 
amahecool profile image
amahecool • Edited

the returned function fires on component Re-render also. How to you check if its an unmount or just a re-render ?
Basically its just a clean up function which runs every time the useEffect is used (except the first time)

Collapse
 
nodejsdeveloperskh profile image
node.js.developers.kh • Edited

Are you sure? AFAIK it should not be the case, it's only triggered once the component is unmounting or remounting itself. So maybe you can check that out.

Also you might have other useEffect who are defined sooner and as you might know the order of defining useEffect hooks matter

Collapse
 
robmarshall profile image
Robert Marshall

I suppose you could use a useRef to track the render (it will not be cleaned on a rerender). Depends on what you need it to do.