This tutorial will walk you through using the react-router and react-transition-group modules to create multi-page React applications with page transition animations.
Preparing the React App
Installing the create-react-app Package
If you’ve ever had the chance to try React, you’ve probably heard about the create-react-app package, which makes it super easy to start with a React development environment.
In this tutorial, we will use this package to initiate our React app.
So, first of all, make sure you have Node.js installed on your computer. It will also install npm for you.
In your terminal, run npm install -g create-react-app
. This will globally install create-react-app on your computer.
Once it is done, you can verify whether it is there by typing create-react-app -V
.
Creating the React Project
Now it’s time to build our React project. Just run create-react-app multi-page-app
. You can, of course, replace multi-page-app
with anything you want.
Now, create-react-app will create a folder named multi-page-app. Just type cd multi-page-app
to change directory, and now run npm start
to initialize a local server.
That’s all. You have a React app running on your local server.
Now it’s time to clean the default files and prepare our application.
In your src
folder, delete everything but App.js
and index.js
. Then open index.js
and replace the content with the code below.
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render(, document.getElementById('root'));
I basically deleted the registerServiceWorker
related lines and also the import './index.css';
line.
Also, replace your App.js
file with the code below.
import React, { Component } from 'react'; class App extends Component { render() { return (); } } export default App;
Now we will install the required modules.
In your terminal, type the following commands to install the react-router and react-transition-group modules respectively.
npm install react-router-dom --save
npm install [email protected] --save
After installing the packages, you can check the package.json
file inside your main project directory to verify that the modules are included under dependencies.
Router Components
There are basically two different router options: HashRouter and BrowserRouter.
As the name implies, HashRouter uses hashes to keep track of your links, and it is suitable for static servers. On the other hand, if you have a dynamic server, it is a better option to use BrowserRouter, considering the fact that your URLs will be prettier.
Once you decide which one you should use, just go ahead and add the component to your index.js
file.
import { HashRouter } from 'react-router-dom'
The next thing is to wrap our
component with the router component.
So your final index.js
file should look like this:
import React from 'react'; import ReactDOM from 'react-dom'; import { HashRouter } from 'react-router-dom' import App from './App'; ReactDOM.render(, document.getElementById('root'));
If you’re using a dynamic server and prefer to use BrowserRouter, the only difference would be importing the BrowserRouter and using it to wrap the
component.
By wrapping our
component, we are serving the history object to our application, and thus other react-router components can communicate with each other.
Inside Component
Inside our
component, we will have two components named and
. As the names imply, they will hold the navigation menu and displayed content respectively.
Create a folder named “components” in your src
directory, and then create the Menu.js
and Content.js
files.
Menu.js
Let’s fill in our Menu.js
component.
It will be a stateless functional component since we don’t need states and life-cycle hooks.
import React from 'react' const Menu = () =>{ return(
- Home
- Works
- About
Here we have a
- tag with
- tags with the
component.
The
component is essentially a react-router component acting like an
tag, but it does not reload your page with a new target link.
Also, if you style your
a
tag in CSS, you will notice that thecomponent gets the same styling.
Note that there is a more advanced version of the
component, which is
. This offers you extra features so that you can style the active links.Now we need to define where each link will navigate. For this purpose, the
component has a
to
prop.import React from 'react' import { Link } from 'react-router-dom' const Menu = () =>{ return(
- Home
- Works
- About
Content.js
Inside our
component, we will define the Routes to match the Links.We need the
Switch
andRoute
components from react-router-dom. So, first of all, import them.import { Switch, Route } from 'react-router-dom'
Second of all, import the components that we want to route to. These are the
Home
,Works
andAbout
components for our example. Assuming you have already created those components inside the components folder, we also need to import them.import Home from './Home'
import Works from './Works'
import About from './About'
Those components can be anything. I just defined them as stateless functional components with minimum content. An example template is below. You can use this for all three components, but just don’t forget to change the names accordingly.
import React from 'react' const Home = () =>{ return(
Home) } export default HomeSwitch
We use the
component to group our
components. Switch looks for all the Routes and then returns the first matching one.Route
Routes are components calling your target component if it matches the
path
prop.The final version of our
Content.js
file looks like this:import React from 'react' import { Switch, Route } from 'react-router-dom' import Home from './Home' import Works from './Works' import About from './About' const Content = () =>{ return(
Notice that the extra
exact
prop is required for the Home component, which is the main directory. Usingexact
forces the Route to match the exact pathname. If it’s not used, other pathnames starting with/
would also be matched by the Home component, and for each link, it would only display the Home component.Now when you click the menu links, your app should be switching the content.
Animating the Route Transitions
So far, we have a working router system. Now we will animate the route transitions. In order to achieve this, we will use the react-transition-group module.
We will be animating the mounting state of each component. When you route different components with the Route component inside Switch, you are essentially mounting and unmounting different components accordingly.
We will use react-transition-group in each component we want to animate. So you can have a different mounting animation for each component. I will only use one animation for all of them.
As an example, let’s use the
component.First, we need to import CSSTransitionGroup.
import { CSSTransitionGroup } from 'react-transition-group'
Then you need to wrap your content with it.
Since we are dealing with the mounting state of the component, we enable
transitionAppear
and set a timeout for it. We also disabletransitionEnter
andtransitionLeave
, since these are only valid once the component is mounted. If you are planning to animate any children of the component, you have to use them.Lastly, add the specific
transitionName
so that we can refer to it inside the CSS file.import React from 'react' import { CSSTransitionGroup } from 'react-transition-group' import '../styles/homeStyle.css' const Home = () =>{ return(
HomeWe also imported a CSS file, where we define the CSS transitions.
.homeTransition-appear{ opacity: 0; } .homeTransition-appear.homeTransition-appear-active{ opacity: 1; transition: all .5s ease-in-out; }
If you refresh the page, you should see the fade-in effect of the Home component.
If you apply the same procedure to all the other routed components, you will see their individual animations when you change the content with your Menu.
Conclusion
In this tutorial, we covered the react-router-dom and react-transition-group modules. However, there’s more to both modules than we covered in this tutorial. Here is a working demo of what was covered.
So, to learn more features, always go through the documentation of the modules you are using.
Over the last couple of years, React has grown in popularity. In fact, we have a number of items in the marketplace that are available for purchase, review, implementation, and so on. If you’re looking for additional resources around React, don’t hesitate to check them out.
tags, which will be our links.
Now add the following line to your Menu component.
import { Link } from 'react-router-dom'
And then wrap the content of the
Powered by WPeMatico