Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

React Router Transition

Introduction to React Router Transition

As we are aware that using animated transitions makes an application attractive and draws attention of users, 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 () => (

atEnter={{ opacity: 0 }}
atLeave={{ opacity: 0 }}
atActive={{ opacity: 1 }}
className="switch-wrapper"
>





)

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 React Router Transition works?

In order to use react router we need to add react router transition 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:

importReactfrom'react'
importReactDOMfrom'react-dom'
//import React, { Component } from 'react'
import{BrowserRouter,Switch,Route}from'react-router-dom'
importHomefrom'./Home.js'
importContactfrom'./Contact'
importAboutfrom'./About'
import'./styles.css'
functionExample(){
return(








)
}
constrootElement=document.getElementById('root')
ReactDOM.render(,rootElement)

The above code involves use of three different screens whose source code is like:

Home.js

importReactfrom'react'
functionHome(){
return(

Home



Welcome to Educba.


>
)
exportdefaultHome;

About.js

importReactfrom'react'
functionAbout(){
return(

About



Educba aims at offering training in Software development


>
)
}
exportdefaultAbout

Contact.js

importReactfrom'react'
functionContact(){
return(

Contact



Feel Free to reach out us.


>
)
}
exportdefaultContact

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 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 library.

Recommended Articles

This is a guide to React Router Transition. Here we discuss the introduction to React Router Transition, how the transition works along with example. You may also have a look at the following articles to learn more –

  1. React Native Fetch
  2. React Native Search Bar
  3. react native dropdown
  4. React Native Splash Screen

The post React Router Transition appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

React Router Transition

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×