react-router-transition
painless transitions built for react-router, powered by react-motion
Top Related Projects
Declarative routing for React
Open source, production-ready animation and gesture library for React
An easy way to perform animations when a React component enters or leaves the DOM
Quick Overview
React Router Transition is a library that provides smooth transitions between routes in React applications using React Router. It allows developers to create animated transitions when navigating between different pages or views, enhancing the user experience with fluid and visually appealing route changes.
Pros
- Easy integration with existing React Router setups
- Customizable animations and transitions
- Supports both CSS and JavaScript animations
- Lightweight and performant
Cons
- Limited documentation and examples
- May require additional setup for complex animations
- Not actively maintained (last update was in 2019)
- Potential compatibility issues with newer versions of React Router
Code Examples
- Basic usage with CSS transitions:
import { RouteTransition } from 'react-router-transition';
<RouteTransition
pathname={location.pathname}
atEnter={{ opacity: 0 }}
atLeave={{ opacity: 0 }}
atActive={{ opacity: 1 }}
>
{children}
</RouteTransition>
- Using custom easing functions:
import { spring } from 'react-router-transition';
const bounceTransition = {
atEnter: {
opacity: 0,
scale: 0.8,
},
atLeave: {
opacity: spring(0, { stiffness: 200, damping: 22 }),
scale: spring(0.8, { stiffness: 200, damping: 22 }),
},
atActive: {
opacity: spring(1, { stiffness: 200, damping: 22 }),
scale: spring(1, { stiffness: 200, damping: 22 }),
},
};
<RouteTransition
pathname={location.pathname}
{...bounceTransition}
>
{children}
</RouteTransition>
- Animating child routes:
import { AnimatedSwitch } from 'react-router-transition';
<AnimatedSwitch
atEnter={{ opacity: 0 }}
atLeave={{ opacity: 0 }}
atActive={{ opacity: 1 }}
className="switch-wrapper"
>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</AnimatedSwitch>
Getting Started
-
Install the package:
npm install react-router-transition
-
Import and use the
RouteTransition
component:import { RouteTransition } from 'react-router-transition'; import { BrowserRouter as Router, Route } from 'react-router-dom'; function App() { return ( <Router> <RouteTransition pathname={location.pathname} atEnter={{ opacity: 0 }} atLeave={{ opacity: 0 }} atActive={{ opacity: 1 }} > <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> </RouteTransition> </Router> ); }
-
Add necessary CSS for positioning:
.switch-wrapper { position: relative; } .switch-wrapper > div { position: absolute; width: 100%; }
Competitor Comparisons
Declarative routing for React
Pros of React Router
- More comprehensive routing solution with a larger ecosystem and community support
- Offers advanced features like nested routes, dynamic routing, and programmatic navigation
- Regularly updated and maintained by a dedicated team
Cons of React Router
- Steeper learning curve due to its extensive feature set
- Potentially overkill for simple applications that only need basic routing
Code Comparison
React Router:
import { BrowserRouter, Route, Switch } from 'react-router-dom';
<BrowserRouter>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
</BrowserRouter>
React Router Transition:
import { RouteTransition } from 'react-router-transition';
<RouteTransition
pathname={location.pathname}
atEnter={{ opacity: 0 }}
atLeave={{ opacity: 0 }}
atActive={{ opacity: 1 }}
>
{/* Your routes here */}
</RouteTransition>
React Router Transition focuses specifically on adding smooth transitions between routes, while React Router provides a complete routing solution. React Router Transition can be used in conjunction with React Router or other routing libraries to enhance the user experience with animated transitions.
Open source, production-ready animation and gesture library for React
Pros of motion
- More comprehensive animation library with a wider range of features
- Better performance optimization, especially for complex animations
- Larger community and more frequent updates
Cons of motion
- Steeper learning curve due to more complex API
- Larger bundle size, which may impact load times for smaller projects
Code Comparison
react-router-transition:
<AnimatedSwitch
atEnter={{ opacity: 0 }}
atLeave={{ opacity: 0 }}
atActive={{ opacity: 1 }}
>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</AnimatedSwitch>
motion:
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</Switch>
</motion.div>
Summary
motion offers a more powerful and flexible animation solution with better performance, but comes with a steeper learning curve and larger bundle size. react-router-transition provides a simpler, more focused approach for route transitions, which may be sufficient for smaller projects or those primarily concerned with route animations.
An easy way to perform animations when a React component enters or leaves the DOM
Pros of react-transition-group
- More flexible and can be used for various transition scenarios, not limited to routing
- Actively maintained with regular updates and a larger community
- Provides more granular control over transition states (enter, exit, etc.)
Cons of react-transition-group
- Requires more setup and configuration for route-specific transitions
- May have a steeper learning curve for beginners
- Not specifically optimized for route transitions, which might require additional code
Code Comparison
react-transition-group:
<CSSTransition
in={inProp}
timeout={300}
classNames="fade"
unmountOnExit
>
<div>Transitioning content</div>
</CSSTransition>
react-router-transition:
<AnimatedSwitch
atEnter={{ opacity: 0 }}
atLeave={{ opacity: 0 }}
atActive={{ opacity: 1 }}
className="switch-wrapper"
>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
</AnimatedSwitch>
Summary
react-transition-group offers more flexibility and active development but requires more setup for route transitions. react-router-transition provides an easier solution specifically for route animations but with less granular control. The choice depends on project requirements and developer preferences.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
React Router Transition
Painless transitions for React Router, powered by React Motion. Example site.
Requirements
To use the latest version of this package (2.x
), you'll need to use a version
of React compatible with hooks, as well as version 5.x
of react-router-dom
.
Installation
npm install --save react-router-transition react-router-dom
Example Usage
import { BrowserRouter as Router, Route } from 'react-router-dom';
import { AnimatedSwitch } from 'react-router-transition';
export default () => (
<Router>
<AnimatedSwitch
atEnter={{ opacity: 0 }}
atLeave={{ opacity: 0 }}
atActive={{ opacity: 1 }}
className="switch-wrapper"
>
<Route exact path="/" component={Home} />
<Route path="/about/" component={About}/>
<Route path="/etc/" component={Etc}/>
</AnimatedSwitch>
</Router>
)
.switch-wrapper {
position: relative;
}
.switch-wrapper > div {
position: absolute;
}
Docs
Limitations
This library has some obvious limitations, the most marked of which are:
- no staggering or sequencing of animations
- no durations or timing functions
Top Related Projects
Declarative routing for React
Open source, production-ready animation and gesture library for React
An easy way to perform animations when a React component enters or leaves the DOM
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot