Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React Router

React Router

React routing is usually implemented using the react-router library, a powerful library for implementing client-side routing in React applications. Below is a detailed explanation of how to use React Router in React:

Installing React Router

First, you need to install the react-router-dom package, which is used to implement routing in a browser environment.



npm install react-router-dom

Basic Usage

Below are the steps and example code for setting up basic routing with React Router.

1. Create a basic routing component

First, create some simple components that will be rendered by the routes.

Example

import

React from

'react'

;

const

Home

=

(

)

=>

{

 

return

<

h2

Home

h2

;

}

;

export

default

Home

;

// About.js

import

React from

'react'

;

const

About

=

(

)

=>

{

 

return

<

h2

About

h2

;

}

;

export

default

About

;

// Contact.js

import

React from

'react'

;

const

Contact

=

(

)

=>

{

 

return

<

h2

Contact

h2

;

}

;

export

default

Contact

;

2. Set up routes

In your main application component, use components from react-router-dom to set up routes.

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom/client'

;

import

{

  BrowserRouter as Router

,

  Routes

,

  Route

,

  Link

}

from

'react-router-dom'

;

import

Home from

'./Home'

;

import

About from

'./About'

;

import

Contact from

'./Contact'

;

const

App

=

(

)

=>

{

 

return

(

   

<

Router

<

div

<

nav

<

ul

<

li

<

Link to

=

"/"

Home

Link

           

li

           

<

li

<

Link to

=

"/about"

About

Link

           

li

           

<

li

<

Link to

=

"/contact"

Contact

Link

           

li

         

ul

       

nav

       

<

Routes

<

Route path

=

"/"

element

=

{

<

Home

/>

}

/>

<

Route path

=

"/about"

element

=

{

<

About

/>

}

/>

<

Route path

=

"/contact"

element

=

{

<

Contact

/>

}

/>

Routes

div

Router

)

;

}

;

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<

App

/>

)

;

Nested Routing

React Router allows you to create nested routes, so that more routes can be rendered within a component.

Example

// Dashboard.js

import

React from

'react'

;

import

{

Link

,

Outlet

}

from

'react-router-dom'

;

const

Dashboard

=

(

)

=>

{

 

return

(

   

<

div

<

h2

Dashboard

h2

<

nav

<

ul

<

li

<

Link to

=

"profile"

Profile

Link

         

li

         

<

li

<

Link to

=

"settings"

Settings

Link

         

li

       

ul

     

nav

     

<

Outlet

/>

div

)

;

}

;

export

default

Dashboard

;

Example

// Profile.js

import

React from

'react'

;

const

Profile

=

(

)

=>

{

 

return

<

h2

Profile

h2

;

}

;

export

default

Profile

;

Example

// Settings.js

import

React from

'react'

;

const

Settings

=

(

)

=>

{

 

return

<

h2

Settings

h2

;

}

;

export

default

Settings

;

Set up nested routes in the main application component:

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom/client'

;

import

{

  BrowserRouter as Router

,

  Routes

,

  Route

,

  Link

}

from

'react-router-dom'

;

import

Home from

'./Home'

;

import

About from

'./About'

;

import

Contact from

'./Contact'

;

import

Dashboard from

'./Dashboard'

;

import

Profile from

'./Profile'

;

import

Settings from

'./Settings'

;

const

App

=

(

)

=>

{

 

return

(

   

<

Router

<

div

<

nav

<

ul

<

li

<

Link to

=

"/"

Home

Link

           

li

           

<

li

<

Link to

=

"/about"

About

Link

           

li

           

<

li

<

Link to

=

"/contact"

Contact

Link

           

li

           

<

li

<

Link to

=

"/dashboard"

Dashboard

Link

           

li

         

ul

       

nav

       

<

Routes

<

Route path

=

"/"

element

=

{

<

Home

/>

}

/>

<

Route path

=

"/about"

element

=

{

<

About

/>

}

/>

<

Route path

=

"/contact"

element

=

{

<

Contact

/>

}

/>

<

Route path

=

"/dashboard"

element

=

{

<

Dashboard

/>

}

<

Route path

=

"profile"

element

=

{

<

Profile

/>

}

/>

<

Route path

=

"settings"

element

=

{

<

Settings

/>

}

/>

Route

Routes

div

Router

)

;

}

;

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<

App

/>

)

;

Dynamic Route

Paths can contain dynamic parameters.

Example

// User.js

import

React from

'react'

;

import

{

useParams

}

from

'react-router-dom'

;

const

User

=

(

)

=>

{

 

const

{

userId

}

=

useParams

(

)

;

 

return

<

h2

User ID

:

{

userId

}

h2

;

}

;

export

default

User

;

Set up dynamic routes in the main application component:

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom/client'

;

import

{

  BrowserRouter as Router

,

  Routes

,

  Route

,

  Link

}

from

'react-router-dom'

;

import

Home from

'./Home'

;

import

About from

'./About'

;

import

Contact from

'./Contact'

;

import

Dashboard from

'./Dashboard'

;

import

Profile from

'./Profile'

;

import

Settings from

'./Settings'

;

import

User from

'./User'

;

const

App

=

(

)

=>

{

 

return

(

   

<

Router

<

div

<

nav

<

ul

<

li

<

Link to

=

"/"

Home

Link

           

li

           

<

li

<

Link to

=

"/about"

About

Link

           

li

           

<

li

<

Link to

=

"/contact"

Contact

Link

           

li

           

<

li

<

Link to

=

"/dashboard"

Dashboard

Link

           

li

           

<

li

<

Link to

=

"/user/1"

User

1

Link

           

li

           

<

li

<

Link to

=

"/user/2"

User

2

Link

           

li

         

ul

       

nav

       

<

Routes

<

Route path

=

"/"

element

=

{

<

Home

/>

}

/>

<

Route path

=

"/about"

element

=

{

<

About

/>

}

/>

<

Route path

=

"/contact"

element

=

{

<

Contact

/>

}

/>

<

Route path

=

"/dashboard"

element

=

{

<

Dashboard

/>

}

<

Route path

=

"profile"

element

=

{

<

Profile

/>

}

/>

<

Route path

=

"settings"

element

=

{

<

Settings

/>

}

/>

Route

<

Route path

=

"/user/:userId"

element

=

{

<

User

/>

}

/>

Routes

div

Router

)

;

}

;

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<

App

/>

)

;

404 Page

You can use the Navigate component provided by React Router to implement 404 page redirection.

Example

import

React from

'react'

;

import

ReactDOM from

'react-dom/client'

;

import

{

  BrowserRouter as Router

,

  Routes

,

  Route

,

  Link

,

  Navigate

}

from

'react-router-dom'

;

import

Home from

'./Home'

;

import

About from

'./About'

;

import

Contact from

'./Contact'

;

import

Dashboard from

'./Dashboard'

;

import

Profile from

'./Profile'

;

import

Settings from

'./Settings'

;

import

User from

'./User'

;

const

NotFound

=

(

)

=>

{

 

return

<

h2

404

Page Not Found

h2

;

}

;

const

App

=

(

)

=>

{

 

return

(

   

<

Router

<

div

<

nav

<

ul

<

li

<

Link to

=

"/"

Home

Link

           

li

           

<

li

<

Link to

=

"/about"

About

Link

           

li

           

<

li

<

Link to

=

"/contact"

Contact

Link

           

li

           

<

li

<

Link to

=

"/dashboard"

Dashboard

Link

           

li

           

<

li

<

Link to

=

"/user/1"

User

1

Link

           

li

           

<

li

<

Link to

=

"/user/2"

User

2

Link

           

li

         

ul

       

nav

       

<

Routes

<

Route path

=

"/"

element

=

{

<

Home

/>

}

/>

<

Route path

=

"/about"

element

=

{

<

About

/>

}

/>

<

Route path

=

"/contact"

element

=

{

<

Contact

/>

}

/>

<

Route path

=

"/dashboard"

element

=

{

<

Dashboard

/>

}

<

Route path

=

"profile"

element

=

{

<

Profile

/>

}

/>

<

Route path

=

"settings"

element

=

{

<

Settings

/>

}

/>

Route

<

Route path

=

"/user/:userId"

element

=

{

<

User

/>

}

/>

<

Route path

=

"*"

element

=

{

<

Navigate to

=

"/"

replace

/>

}

/>

       

Routes

     

div

   

Router

 

)

;

}

;

const

root

=

ReactDOM.

createRoot

(

document.

getElementById

(

'root'

)

)

;

root.

render

(

<

App

/>

)

;

Summary

By using React Router, you can easily implement routing in React applications, including basic routing, nested routing, dynamic routing, and 404 pages. In this way, you can create a powerful and user-friendly single-page application (SPA).