react-hooks-library icon indicating copy to clipboard operation
react-hooks-library copied to clipboard

[hooks] useEffectAfterMount does not in React 18's strict mode in development environment

Open ArtemGolovko opened this issue 3 years ago • 2 comments

useEffectAfterMount does not in React 18's strict mode in development environment, because react first mounts component, which sets isMounted.current to true, then unmounts component and mounts again. The problem is that isMounted.current is true on second mount.

Solution - reset isMounted.current on unmount.

ArtemGolovko avatar Dec 23 '22 08:12 ArtemGolovko

@ArtemGolovko The fact that react 18 strict mode behaviour breaks this hook makes me think that this hook shouldn't even exist. It's a hack to begin with since I created this to deal with next/router having undefined data query params on first render.

Having isMounted false after unmount may cause other issues. useEffect's clean-up runs after the next render, before the next useEffect. This means it will be reset to false on every render. causing the effect to never execute.

heyitsarpit avatar Jan 01 '23 06:01 heyitsarpit

I will be marking this hook as deprecated for the next release and remove it later on a major release.

heyitsarpit avatar Jan 12 '23 15:01 heyitsarpit