motion icon indicating copy to clipboard operation
motion copied to clipboard

[BUG] exitBeforeEnter breaking when children have no exit animation

Open KondakovArtem opened this issue 6 years ago • 10 comments

1. Read the FAQs 👇

2. Describe the bug I have problem with using Redirect in React router. I use standart example with react-router and add Redirect component.

When i open url "/test", it correctly redirected to "/" but animation not started, and nothing happened

https://codesandbox.io/s/framer-motion-x-react-router-mu294

Steps to reproduce the behavior:

  1. Go to '/test'
  2. Page redirected to "/"
  3. Nothing happens

5. Expected behavior Start animation of main page

KondakovArtem avatar Feb 16 '20 12:02 KondakovArtem

I've run into this same issue today.

m4tta avatar Feb 27 '20 10:02 m4tta

+1 here

evstinik avatar Mar 02 '20 10:03 evstinik

I found a trick:

const MotionRedirect: React.FC<RedirectProps> = ({ children, ...props }) => (
    <motion.div exit="undefined">
        <Redirect {...props} />
    </motion.div>
)

demo: https://codesandbox.io/s/react-router-framer-motion-dr4u6


v2 demo: https://codesandbox.io/s/framer-motion-v2-react-router-kisy4

lightyen avatar Mar 09 '20 01:03 lightyen

same problem

deebov avatar Jun 08 '20 20:06 deebov

I'm having this same issue in a case where a component will conditionally render a redirect. In a case where you directly navigate to the page by URL, the default usecase will cause the condition to return the redirect, and it has the same issue, once redirected the component doesn't show.

Using the workaround above solved this.

psibean avatar Jun 23 '20 07:06 psibean

It is also an urgent problem for my case. Would be good if you guys can investigate the issue 🙏 @InventingWithMonster

eugenebuhor avatar Jul 20 '20 22:07 eugenebuhor

Thanks @lightyen !! Your answer was really helpful. I also was having issues with the <Redirect /> and <AnimatePresence /> components. And wrapping the <Redirect /> component into a <motion /> component with exit="undefined" did the trick. In my code, where I had defined my app routes, what I did was:

import { Fragment } from "react";

<AnimatePresence initial={false} exitBeforeEnter>
    <Switch location={location} key={location.key}>
        <Route exact path="/" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact" component={Contact} />
        <Route path="/not-found" component={NotFound} />
        <motion.Fragment exit="undefined">
            <Redirect to="/not-found" />
        </motion.Fragment>
    </Switch>
</AnimatePresence>

enzozerega avatar Aug 18 '20 22:08 enzozerega

Yeah looking at it, it seems exitBeforeEnter is causing AnimatePresence to wait for an exit animation that never arrives before mounting the incoming route.

mattgperry avatar Oct 28 '21 08:10 mattgperry

My suspicion was incorrect, we do handle this case, so we're likely looking at something more specific to Redirect

mattgperry avatar Oct 29 '21 12:10 mattgperry

Ah I've just seen this seems to be working in 5.0.1

mattgperry avatar Nov 01 '21 15:11 mattgperry