Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

533
Views
React-router-dom (v6) with Framer Motion (v4)

I am trying to update my react-router-dom to v6, but it seems to be causing issues with framer motions AnimatePresence, specifically the exit transition.

In App.js:

import { Routes, Route } from "react-router-dom";
import {AnimatePresence} from "framer-motion";  

import Home from "./Components/Home";
import About from "./Components/About";
function App() {
  return (
    <div className="App">

      {/* globals such as header will go here  */}

      <AnimatePresence exitBeforeEnter>
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="about" element={<About />} />
        </Routes>
      </AnimatePresence>
    </div>
  );
}

export default App;

And then in my About/Home components I have:

import {Link} from "react-router-dom";
import {motion} from "framer-motion";  

    function About() {
    
        const pageMotion = {
            initial: {opacity: 0, x: 0},
            animate: {opacity: 1, x: 50, transition: {duration: 2}},
            exit: {opacity: 0, x:0, transition: {duration: 2}}
        };
    
        return (
            <div className="about">
                <motion.div initial="initial" animate="animate" exit="exit" variants={pageMotion}>about page</motion.div>
                <Link to="/">Go to home page</Link>
            </div>
        )
    }
    
    export default About

The "initial" and "animate" work fine, but exit is ignored, and immedietly jumps to the relevant page (rather than animating out first).

Note: I also have had to downgrade to framer-motion v4, as v5 does not work with Create-react-app.

Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to provide to Routes the key and location prop like this:

AnimatedRoutes.js

const AnimatedRoutes = () => {
  const location = useLocation();

  return (
    <AnimatePresence exitBeforeEnter>
      <Routes location={location} key={location.pathname}>
        <Route path="/" element={<Home />} />
        <Route path="about" element={<About />} />
      </Routes>
    </AnimatePresence>
  );
};

And since the component that is calling useLocation must be be wrapped in BrowserRouter:

App.js

function App() {
  return (
    <BrowserRouter>
      <AnimatedRoutes />
    </BrowserRouter>
  );
}

Working CodeSandbox.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!