Introduction to React Router Transition
As we are aware that using animated transitions makes an application attractive and draws the attention of users, a react-router transition is used to provide animated transitions in a react native application. It provides two main components responsible for providing animations to react applications, the two main components are AnimatedSwitch and AnimatedRoute.
Syntax:
Given below is the basic syntax of how components animated switch and animated Route works in react:
// import browser router from react router dom
import { BrowserRouter as Router, Route } from 'react-router-dom';
// import animated switch from react router transition
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="/courses/" component={Courses Offered}/>
<Route path="/contactus/" component={Contact Us}/>
</AnimatedSwitch>
</Router>
)
The above syntax shows how to use animated switch in react native. It first requires importing browser router and animated switch into our code after we add dependency of react router dom and react router transition into our project respectively. As we can see from above syntax, we have used router as the root tag, within which we have defined animated switch tag having different properties which include animated switch class-name, atEnter, atLeave, AtActive. Route tag shown above mentions different routes that needs to be animated.
How does React Router Transition work?
In order to use react-router we need to add dependency into our react native project.
Dependency can be added by running the below command:
Code:
npm install --save react-router-transition react-router-dom
After the above command runs successfully, we can import dependency into our project and use it as per our requirement.
The following parameters are available in react animated switch:
Property Name | Is Required/Optional | Description |
atEnter, atLeave, atActive | Required | The value set corresponding to these parameters states the nature of react component during mounting, mounted and unmounting phases of a react component. |
mapStyles | Optional | This is an optional function for transforming values. |
runOnMount | Optional | This is a Boolean flag used to signal whether a transformation needs to be applied to a child component after it is being applied to parent component. |
Class-name | Required | The specified class name will be applied to root node of the transition. |
Example of React Router Transition
Given below is the example:
Code:
import React from'react'
import ReactDOM from'react-dom'
//import React, { Component } from 'react'
import { BrowserRouter, Switch, Route} from 'react-router-dom'
import Home from './Home.js'
import Contact from './Contact'
import About from './About'
import './styles.css'
function Example(){
return(
<BrowserRouter>
<div className="root">
<Switch>
<Route exact path = "/" component={Home} />
<Route path = "/contact" component={Contact} />
<Route path = "/about" component={About} />
</Switch>
</div>
</BrowserRouter>)
}
const rootElement = document.getElementById('root')
ReactDOM.render(<Example />, rootElement)
The above code involves the use of three different screens whose source code is like:
Home.js
import React from 'react'
function Home(){
return(
<>
<h1>Home</h1>
<p>
Welcome to Educba.
</p>
</>
)
export default Home;
About.js
import React from 'react'
function About(){
return(
<>
<h1>About</h1>
<p>
Educba aims at offering training in Software development
</p>
</>
)
}
export default About
Contact.js
import React from 'react'
function Contact(){
return(
<>
<h1>Contact</h1>
<p>
Feel Free to reach out us.
</p>
</>
)
}
export default Contact
The above example shows how to use react animated switch in react. After executing above code we will see three different switched on click of which we can see different screens. When our code first executes we will see the below output:
Output:
After user clicks on second switch we will see the below output:
On clicking last switch we will see the below output:
Conclusion
The above article provides a clear understanding about the react-router transition. There are several third-party libraries that can be used to provide more customizations to router transitions with more animations in react. Apart from the above-mentioned Animated Switch, there are many more components in the library.
Recommended Articles
This is a guide to React Router Transition. Here we discuss the introduction, syntax, and working of React Router Transition along with an example. You may also have a look at the following articles to learn more –