AnimatePresence exit animations cancelled on ancestor unmount in Framer Motion 11
0 reputation · 17 Oct 2024, 03:27 UTC
0 reputation · 17 Oct 2024, 03:27 UTC
What is the correct pattern for ensuring AnimatePresence exit animations complete before DOM removal when a parent component unmounts the AnimatePresence wrapper during rapid mount/unmount cycles?
AnimatePresence only waits for exit animations while the child remains mounted in React's tree. If an ancestor unmounts the wrapper, React may batch removal before the exit animation starts, cancelling it. mode="wait" sequences siblings but does not protect against ancestor unmount, and no force-exit or defer-unmount API is documented in Framer Motion 10.x–11.x.
Version assumption: Framer Motion 11.x with React 18. Hydration contexts and older @types packages remain unverified.
35775 reputation · 17 Oct 2024, 06:09 UTC
AnimatePresence wrapper at a persistent ancestor level.Exit animations in Framer Motion are strictly tied to the child component remaining within the React tree managed by AnimatePresence. If an ancestor component unmounts the wrapper, React initiates the DOM removal process, which preempts the completion of the exit animation lifecycle, regardless of the mode="wait" setting.
mode="wait" manages sibling sequencing but does not guard against ancestor-driven unmounting.forceExit or deferUnmount) to halt ancestor unmounting until animation completion.<AnimatePresence> component resides within a parent component that is *not* subject to rapid mounting/unmounting cycles.visible) passed to the child, rather than conditionally rendering the child within the wrapper itself.To confirm if this pattern resolves the issue, please verify: Is the <AnimatePresence> wrapper component itself being unmounted, or is only the child element being conditionally rendered within a stable wrapper?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.