EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

React Router Transition

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » React Native Tutorial » React Router Transition

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 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(
<BrowserRouter>
<divclassName="root">
<Switch>
<Routeexactpath="/"component={Home} />
<Routepath="/contact"component={Contact} />
<Routepath="/about"component={About} />
</Switch>
</div>
</BrowserRouter>)
}
constrootElement=document.getElementById('root')
ReactDOM.render(<Example />,rootElement)

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

Home.js

importReactfrom'react'
functionHome(){
return(
<>
<h1>Home</h1>
<p>
Welcome to Educba.
</p>
</>
)
exportdefaultHome;

About.js

importReactfrom'react'
functionAbout(){
return(
<>
<h1>About</h1>
<p>
Educba aims at offering training in Software development
</p>
</>
)
}
exportdefaultAbout

Contact.js

importReactfrom'react'
functionContact(){
return(
<>
<h1>Contact</h1>
<p>
Feel Free to reach out us.
</p>
</>
)
}
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:

React Router Transition 2

After user clicks on second switch we will see the below output:

React Router Transition 3

On clicking last switch we will see the below output:

on clicking last switch

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

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
React Native Tutorial
  • Basic
    • What is React Native?
    • React Versions
    • React Constructor
    • React Native Architecture
    • React Native Libraries
    • React Components Libraries
    • React Native Components
    • React Component Library
    • React Component Lifecycle
    • React Native Framework
    • React Higher Order Component
    • React Ternary Operator
    • React Native Charts
    • React Native Layout
    • React Native Grid
    • React Native Fetch
    • React Native Modal
    • React Native SVG
    • Button in React Native
    • React List Components
    • React Native Element
    • React Native FlatList
    • React Native SectionList
    • react native dropdown
    • React Native Menu
    • React Native State
    • React State Management
    • React Native Tabs
    • React Native Tab Bar
    • React Format
    • React-Native Switch
    • React Native Firebase
    • React Native Flexbox
    • React Native StatusBar
    • React Native ScrollView
    • React Native ListView
    • React Native TextInput
    • React Native Table
    • React-Native Border Style
    • React Native Search Bar
    • React-Native StyleSheet
    • React Native Vector Icons
    • React Native Login Screen
    • React Native Splash Screen
    • React Native Image Picker
    • React Native Navigation
    • React Native Swift
    • React Controlled Input
    • React Fragment
    • React Native Color
    • React Portals
    • React Refs
    • React shouldComponentUpdate()
    • React ComponentDidMount()
    • React componentWillUpdate()
    • React Native Orientation
    • React Native Animation
    • React Native Form
    • React Props
    • React Native Linear Gradient
    • React Native AsyncStorage
    • React Error Boundaries
    • React Native Progress Bar
    • React-Native Calendar
    • React Native Linking
    • React Native DatePicker
    • React Native Image
    • React Native Drawer
    • React Native Drawer Navigation
    • React Native Fonts
    • React Native Overlay
    • React Native OneSignal
    • React Native Template
    • React Native Router
    • React Router Transition
    • React Dispatcher
    • React Native Redux
    • React JSX
    • React native keyboardavoidingview
    • React Native Permissions
    • React Redux Connect
    • React Native Material
    • React Native Gesture Handler
    • React Native Theme
    • React Native Accessibility
    • React Native Justify Content
    • MobX React Native
    • React Native Authentication
    • React Native UUID
    • React Native Geolocation
    • React Native Debugger
    • React Native Carousel
    • React Native Local Storage
    • React Native TypeScript
    • React Bootstrap
    • React Native SQLite
    • React Native Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More