When clicking the Show/Hide button, the console output is
index.js:12 Button clicked. Going to show the web component
index.js:30 Rendering MyComponent
index.js:33 MyComponent mounted
index.js:12 Button clicked. Going to hide the web component
As you see, the useEffect is called when mounting, but when unmounting, the cleanup function is never called.
When removing a web component element from the DOM, the underlying react component does not receive the unmount event:
Here is a demo to reproduce (also here https://codesandbox.io/s/relaxed-shamir-9ni71?file=/src/index.js):
When clicking the Show/Hide button, the console output is
As you see, the useEffect is called when mounting, but when unmounting, the cleanup function is never called.