question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useEffect cleanup function is not called on component unmount

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mikeborozdincommented, Oct 6, 2020

@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.

0reactions
MilosRasiccommented, Aug 12, 2020

👍 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 to document which will never be cleaned up. Makes toHaveBeenCalledTimes() really unpredictable.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found