Sitemap
Chevi

Computing & cooking. Clear and rigorous information with a strong and unwavering ethical commitment. Writing in spanish at bitácora.es since 2017.

Member-only story

Transitions

--

To enhance the experience of a user browsing a React application, we can switch between pages smoothly using a fade-out exit animation and a fade-in entrance animation prepared with the framer-motion package. The first thing we need to do is define and export the transition as a constant, in this case:

export const variantsPage = {
initial: { opacity: 0, transition: { duration: 0.3, delay: 0.3 } },
in: { opacity: 1, transition: { duration: 0.3, delay: 0.3 } },
out: { opacity: 0, transition: { duration: 0.3, delay: 0.3 } }
};
Press enter or click to view image in full size
Photo: Kyle Loftus

Then, in the App.tsx file, we import the framer-motion package and add the AnimatePresence component as a wrapper for the routes of our application:

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

const App = () => {
return (
<div className="App">
<BrowserRouter>
<AnimatePresence>
<Routes>
<Route path={"/page1"} element={<Page1/>}/>
<Route path={"/page2"} element={<Page2/>}/>
<Route path={"/page3"} element={<Page3/>}/>
<Route path={"/page4"} element={<Page4/>}/>
</Routes>
</AnimatePresence>
</BrowserRouter>
</div>
);
}

export default App;

--

--

José Vicente del Valle Fayos
José Vicente del Valle Fayos

Written by José Vicente del Valle Fayos

He could have become a hedgehog tamer or a cava cork collector, but instead, he had the bright idea to study computer science.

Chevi
Chevi

Published in Chevi

Computing & cooking. Clear and rigorous information with a strong and unwavering ethical commitment. Writing in spanish at bitácora.es since 2017.