animation routes with framer motion

1.3k Views Asked by At

Hi i try to animate 2 routes form opacity 1 to 0 on exit. But for some reason on <Link> click the routes will change(ex. '/' => '/chat?name=bob&room=here') but the page content stay the same and it wont render the component of that specific route. Also the animation is working great once i use the "Go back button" from browser.

Anyone have idea why is not working on button click?

App.js

return (
    <div>
      <Router>
        <AnimatePresence exitBeforeEnter>
          <Switch location={location} key={location.key}>
            <Route path="/" exact component={Login}></Route>
            <Route path="/chat" component={ChatRoom}></Route>
          </Switch>
        </AnimatePresence>
      </Router>
    </div>
  );

Login.js

return (
    <motion.div
      initial="out"
      animate="in"
      exit="out"
      variants={pageTransition}
      className="Login-Container"
    >

//same code
      
        <Link
          onClick={(event) => (!name || !room ? event.preventDefault() : null)}
          to={`/chat?name=${name}&room=${room}`}
        >
          <button
            
            type="submit"
            className="Login-Button"
          >
            Sign In
          </button>
        </Link>
    </motion.div>
  );

ChatRoom.js

return (
    <motion.div
      initial="out"
      animate="in"
      exit="out"
      variants={pageTransition}
      className="ChatRoom-Container"
    >
      <div className="ChatRoom-NavBar">
        <NavLink to="/">
          <div  
            className="ChatRoom-BackButton"
            style={{ backgroundImage: `url(${arrow})` }}
          ></div>
        </NavLink>

        </div>
    </motion.div>
  );
};
1

There are 1 best solutions below

0
On BEST ANSWER

Your initial and exit prop on motion.div seem to be using the same key i.e "out" try giving the exit prop a different key. Dont forget to adjust your variants as well when you make the change.