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 in Node-js by Ashutosh
• 19,190 points
62 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 by Kavya

Related Questions In Node-js

0 votes
1 answer

How can you preload data for a route in React?

Preloading Data for a Route in React ...READ MORE

answered Feb 24 in Node-js by Kavya
87 views
0 votes
1 answer

How do you handle nested dynamic routes with React-Router?

Handling Nested Dynamic Routes in React Router ...READ MORE

answered Feb 24 in Node-js by Kavya
73 views
0 votes
1 answer

How do you structure a scalable Express.js project with multiple route modules?

Implementation Steps: Initialize Express App (app.js) const express = ...READ MORE

answered Feb 25 in Node-js by Navya
48 views
0 votes
1 answer

How can I use an http proxy with node.js http.Client?

Hello @kartik, You can use request, I just found ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,840 points
10,779 views
0 votes
1 answer
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 in Node-js by Kavya
59 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