question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AnimatePresence exit not working for me...

See original GitHub issue

Dunno if this is a bug or if i’m doing something wrong, but the exit prop does not seem to be working for me. Here is my code:

<AnimatePresence exitBeforeEnter>
  <Container
    {...props}
    initial={{
      opacity: 0
    }}
    animate={{
      opacity: 1,
      transition: { 
        delay: 3,
        duration: 5
      }
    }}
    exit={{
      opacity: 0,
      transition: { 
        delay: 3,
        duration: 5
      }
    }}
  >
    {props.children}
  </Container>
</AnimatePresence>

…based on the code above, i am expecting the component to wait 3 seconds, then fade out over 5 seconds, then wait 3 seconds, then fade in over five seconds.

what actually happens is that the component fades out instantly, then about six seconds pass, then it fades in over 5 seconds…so, the piece that does not seem to be working is the fade out over 5 seconds as the component exits…

do you think this might be a bug or m i doing something incorrect here?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

43reactions
mattgperrycommented, Apr 24, 2020

@amcc The direct child(ren) of AnimatePresence need to be either changing key or mounting/unmounting to fire exit animations. Here, the direct child is Container, which never does either. So this isn’t going to work.

Additionally, AnimatePresence needs to be rendered consistently to track changes to its children. This is an unavoidable constraint of React. Although Transition here is being rendered by Layout in a consistent way, Layout itself is being mounted/unmounted every render. You can see this by adding a useEffect:

const Layout = props => {
  React.useEffect(() => {
    console.log("mount")
  }, [])

This will fire every time a page changes. This means that each AnimatePresence component is an entirely new instance of the component, so it has no connection to any other AnimatePresence components that may have been rendered in the past.

Your usage in the example above is correct - as long as Transition itself isn’t remounting. Which it seems to be here. I don’t know enough about the Gatsby template system to know if there’s a way around this.

3reactions
ymoon715commented, Jul 8, 2020

you are doing something wrong. AnimatePresence will detect which children has been mounted or unmounted. You have Container that is ALWAYS rendered as a children. It will never animate because Container is always mounted; it never unmounts, so you wont get any animation

On top of that, you also have to give “key” prop to each children, so that AnimatePresence can detect them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Animate Presence exit not working framer motion
The reason that this is not working is that you have to explicitly specify the key of the child that you are conditionally...
Read more >
Exit animation with `framer-motion` demystified
Re-render when all exit animation are done​​ The final step is to re-render the AnimatePresence component when all the exit animation are ...
Read more >
Framer Motion exit animation is not working : r/reactjs - Reddit
Hello, I am trying to use framer-motion and want to animate an exit, this is how I am defining the motion div: {isOpen(point)...
Read more >
Page Transitions In React
Thankfully, Framer Motion solves this problem with Animate Presence. Coupled with exitBeforeEnter , it allows developers to create amazing page transitions. It ...
Read more >
framer motion | enter and exit animations - YouTube
I was really surprised by this video, it was a bit hard to find (videos on the first few pages not having what...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found