The element vanishes at once instead of animating out
The element disappears instantly and the exit prop never runsShort answer
AnimatePresence plays an exit animation only when a direct child with a unique key leaves the tree while AnimatePresence itself stays mounted. Move the condition inside it, give the child a stable key, put an exit prop on the motion component, and use mode wait when swapping a single child.
Fix it fast
1Find out which cause you have
Look at the JSX around the element that leaves. Note where the condition is, what the keyed element is, and which package is imported.
grep -rn "AnimatePresence" src npm ls motion framer-motion2Match what you see, then jump to its fix
- 1
The code reads show && <AnimatePresence> ...The condition wraps AnimatePresence instead of sitting inside itFix - 2
Lists use the array index as key; or a page swap has no key at allThe child has no key, or an unstable oneFix - 3
A plain div or fragment sits between AnimatePresence and the motion componentThe keyed element is nested, so it is not a direct childFix - 4
The new element appears while the old one is still on screen, or both overlapmode is not suited to swapping one view for anotherFix
- 1
What does the error look like?
The element disappears instantly and the exit prop never runsA list item or modal has no leaving animation, though the entering animation works
Cause 1 of 4
The condition wraps AnimatePresence instead of sitting inside it
1Check
The docs say AnimatePresence must be outside the code that unmounts the element. When the whole thing unmounts, nothing remains to play the exit.
grep -rn -B2 "<AnimatePresence" src | grep "&&\|?"2Fix
Keep AnimatePresence always rendered and put the condition inside it.
<AnimatePresence>
{isVisible && (
<motion.div key="modal" exit={{ opacity: 0 }} />
)}
</AnimatePresence>3Verify
Toggle the condition and watch the fade.
npm run devConfirmed by motion.dev
Cause 2 of 4
The child has no key, or an unstable one
1Check
Direct children need a unique key so the component can tell which one left. Index keys break when items reorder.
grep -rn "key={index}\|key={i}" src2Fix
Use an id that belongs to the item.
<AnimatePresence>
{items.map((item) => (
<motion.li key={item.id} exit={{ opacity: 0 }} />
))}
</AnimatePresence>3Verify
Remove the first item in the list and check that it animates out.
npm run devConfirmed by motion.dev
Cause 3 of 4
The keyed element is nested, so it is not a direct child
1Check
AnimatePresence watches its direct children. The motion element can sit inside your own component, but that component must be the child that carries the key.
grep -rn -A3 "<AnimatePresence" src2Fix
Put the key on the immediate child, and the exit prop on the motion component inside it.
<AnimatePresence>
<Slide key={activeItem.id} />
</AnimatePresence>3Verify
Change activeItem and confirm the old slide leaves with its exit animation.
npm run devConfirmed by motion.dev
Cause 4 of 4
mode is not suited to swapping one view for another
1Check
The default mode is sync, so the entering and leaving elements share the screen. wait mode supports one child at a time.
grep -rn "mode=" src2Fix
Set mode to wait when only one element should be visible at a time.
<AnimatePresence mode="wait">
<motion.div key={route} exit={{ opacity: 0 }} />
</AnimatePresence>3Verify
Switch views; the new one enters after the old one has left.
npm run devConfirmed by motion.dev
How do I stop it coming back?
- Treat AnimatePresence as a permanent wrapper and put every condition inside it.
- Use database or list ids as keys, never the index.
- Import from one package only: motion/react for the current Motion package.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix