Skip to content

Commit

Permalink
fix(useDidUpdate): useDidUpdate to make it work with Strict Effects (#…
Browse files Browse the repository at this point in the history
…974)

* Add cleanup to make it work with Strict Effects

> With the release of React 18, StrictMode gets an additional behavior to ensure it's compatible with reusable state. When StrictMode is enabled, **React intentionally double-invokes effects (mount -> unmount -> mount) for newly mounted components**. [Source](reactwg/react-18#19)
So, it's essential that the `hasMountedRef` is reset when unmounting otherwise it won't behave desirably when wrapped inside StrictMode.

* Update useDidUpdate.ts
  • Loading branch information
ssmkhrj authored May 20, 2022
1 parent 33093ff commit 7e64d10
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/hooks/useDidUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ function useDidUpdate(callback: () => any, conditions?: any[]): void {
hasMountedRef.current = true;
}
}, conditions);

useEffect(() => {
return () => {
hasMountedRef.current = false;
}
}, [])
}

export { useDidUpdate };

0 comments on commit 7e64d10

Please sign in to comment.