useEffect cleanup function is not called on component unmount
See original GitHub issueI am using useEffect
in my test component.
const Cmp = () => {
React.useEffect(() => {
console.log(1);
return () => console.log(2);
}, []);
return <span>Test</span>;
};
And in test: shallow(<Cmp />).unmount()
.
And there is no log emitted in the cleanup function
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Why is the cleanup function from `useEffect` called on every ...
React performs the cleanup when the component unmounts. I'm not sure where you read this but this statement is incorrect.
Read more >Understanding React's useEffect cleanup function
React's useEffect cleanup function saves applications from unwanted behaviors like memory leaks by cleaning up effects.
Read more >Demystifying useEffect's clean-up function - Max Rozen
Component re-renders, displaying id as 2 in the UI; useEffect clean-up function fires, calling console.log and prints id: 1; useEffect runs, calling console.log ......
Read more >wrapper.unmount not calling useEffect cleanup #12 - GitHub
It would seem like they are triggered. Example below is an example where the console log never gets called, and the toHaveBeenCalledTimes is...
Read more >Why you should always Cleanup Side Effects in React ...
The cleanup function in useEffect is a handy function that gives us as much power as class components. There's the componentWillUnmount ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kvet ,
That’s an interesting proposal! I’ll look into it. It might be difficult to implement
cleanupReactHooks(forWrapper)
, since the library has no knowledge of components on Enzyme wrappers. But I can certainly add a method for firing all cleaning up functions.👍 for this
In my case, I’m trying to test an event listener attached to
document
in an effect. Without cleanup on unmount, every test case will add another listener todocument
which will never be cleaned up. MakestoHaveBeenCalledTimes()
really unpredictable.