How can you handle route transitions with animations

0 votes
With the help of proper code can you tell me How can you handle route transitions with animations?
Feb 23, 2025 in Node-js by Ashutosh
• 33,350 points
514 views

1 answer to this question.

0 votes

You can use Framer Motion with React Router to animate route transitions.

Steps to Implement:

Install Framer Motion:

npm install framer-motion

Wrap Routes with AnimatePresence:

import { BrowserRouter as Router, Routes, Route, useLocation } from "react-router-dom";

import { AnimatePresence, motion } from "framer-motion";

import Home from "./Home";

import About from "./About";

const pageVariants = {

  initial: { opacity: 0, y: -10 },

  animate: { opacity: 1, y: 0, transition: { duration: 0.5 } },

  exit: { opacity: 0, y: 10, transition: { duration: 0.5 } },

};

const AnimatedRoutes = () => {

  const location = useLocation();

  return (

    <AnimatePresence mode="wait">

      <Routes location={location} key={location.pathname}>

        <Route path="/" element={<motion.div {...pageVariants}><Home /></motion.div>} />

        <Route path="/about" element={<motion.div {...pageVariants}><About /></motion.div>} />

      </Routes>

    </AnimatePresence>

  );

};

function App() {

  return (

    <Router>

      <AnimatedRoutes />

    </Router>

  );

}

export default App;

answered Feb 24, 2025 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you handle route transitions and animations in React applications using React Router?

To handle route transitions and animations in ...READ MORE

answered Apr 24, 2025 in Node-js by anonymous
765 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How can you implement multi-step forms with route-based navigation in React Router?

To implement a multi-step form where each ...READ MORE

answered Apr 22, 2025 in Node-js by anonymous
666 views
0 votes
1 answer
0 votes
1 answer

How would you implement a chat application using MongoDB’s data model patterns?

To implement a chat application using MongoDB’s ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
572 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do you handle nested routes with complex layouts?

To manage nested routes with complex layouts ...READ MORE

answered Feb 24, 2025 in Node-js by Kavya
489 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP