motiondivision / motion

A modern animation library for React and JavaScript
https://motion.dev
MIT License
25.67k stars 843 forks source link

[BUG] `safeToRemove` Callback Recreated on Parent Re-render #2801

Closed custardcream98 closed 1 month ago

custardcream98 commented 2 months ago

1. Read the FAQs 👇

✅

2. Describe the bug

The usePresence hook provides a safeToRemove callback. However, if the parent re-renders during the exit animation and I add safeToRemove to the useEffect dependency array, the function is recreated, causing the effect to trigger unnecessarily.

This happens despite #2431 wrapping the callback with the useCallback hook.

After looking into it, I found that the onExitComplete function in PresenceContext is being recreated unnecessarily. Take a look at the PR I'll be submitting.

3. IMPORTANT: Provide a CodeSandbox reproduction of the bug

https://codesandbox.io/p/sandbox/safetoremove-re-create-issue-nzvmfy

4. Steps to reproduce

Steps to reproduce the behavior:

  1. Go to codesandbox.
  2. Run the app and check the console log.

5. Expected behavior

The safeToRemove function should remain the same, regardless of whether the parent is re-rendered or not.