React Router | Installation

Installation

This document describes the most park ways people use react Router with versatile tools in the React Ecosystem .

Basic Installation

Most advanced React projects manage their dependencies using a software director like npm or Yarn. To add React Router to an existing project, the beginning thing you should do is install the necessary dependencies with the joyride of your choice :
npm

 $ npm install react-router-dom @ 6

Yarn

 $ yarn add react-router-dom @ 6 

pnpm

 $ pnpm add react-router-dom @ 6

Create React App

Follow the instructions in the React documentation to set up a new project with Create React App, then follow the installation instructions above to install React Router in your stick out. once your project is set up and React Router is installed as a dependence, open the src/index.js in your textbook editor program. Import BrowserRouter from react-router-dom near the top of your file and wrap your app in a :

 

import

*

as

React

from

``

react

`` ;

import

ReactDOM

from

``

react-dom/client

`` ;

import

{

BrowserRouter

}

from

``

react-router-dom

`` ;

import

``

./index.css

`` ;

import

App

from

``

./App

`` ;

import

reportWebVitals

from

``

./reportWebVitals

`` ;

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

( ``

root

`` ) ) ;

root

.

render

( <

React.StrictMode

> <

BrowserRouter

> <

App

/ > < p BrowserRouter>

< p React.StrictMode>
) ; nowadays you can use react Router anywhere in your app ! For a simpleton case, open src/App.js and replace the default markup with some routes :

 

import

*

as

React

from

``

react

`` ;

import

{

Routes

,

Route

,

Link

}

from

``

react-router-dom

`` ;

import

``

./App.css

`` ;

function

App

( ) {

return

( <

div

className

=

``

App

`` > <

h1

> Welcome to React Router ! < phosphorus h1>

<

Routes

> <

Route

path

=

/

element

=

{

<

Home

/ >

}

/ > <

Route

path

=

about

element

=

{

<

About

/ >

}

/ > < phosphorus Routes>
< phosphorus div>
) ; } now, still in src/App.js, create your route components :

 

// App.js

function

Home

( ) {

return

( < > <

main

> <

h2

> Welcome to the home page ! < phosphorus h2>

<

p

> You can do this, I believe in you. < phosphorus phosphorus>
< phosphorus main>
<

nav

> <

Link

to

=

/about

“ > About < p Link>
< phosphorus nav>
) ; }

function

About

( ) {

return

( < > <

main

> <

h2

> Who are we ? < phosphorus h2>
<

p

> That feels like an existential question, do n’t you think ? < phosphorus p>
< p main>
<

nav

> <

Link

to

=

/

“ > Home < p Link>
< p nav>
) ; } Go ahead and start your app by running npm start, and you should see the Home path when your app starts running. Click the “ About ” radio link to see your route, and voilà ! You ‘ve successfully set up React Router using Create React App ! 🥳 When it ‘s clock to deploy your app to production, be sure to follow Create React App ‘s instructions on deploying with React Router to be indisputable your server is configured correctly .

Parcel

Follow the instructions in the Parcel documentation to set up a modern project, then follow the initiation instructions above to install React Router in your undertaking. In your undertaking ‘s package.json, add a start script so you can open your project in a browser during development .

 `` 

scripts

`` : { ``

start

`` : ``

parcel index.html

`` }

once the project is set up and your dependencies are installed, create a new .babelrc file at the root of your project :

 { `` 

presets

`` : [ ``

@babel/preset-react

`` ] }

Go to the index.js file in your project and import the necessary functions from react, react-dom, and react-router-dom and mount a React app in a div with the ID of root :

 

// index.js

import

*

as

React

from

``

react

`` ;

import

ReactDOM

from

``

react-dom/client

`` ;

import

{

BrowserRouter

}

from

``

react-router-dom

`` ;

import

App

from

``

./App.js

`` ;

const

root

=

ReactDOM

.

createRoot

(

document

.

getElementById

( ``

root

`` ) ) ;

root

.

render

( <

React.StrictMode

> <

BrowserRouter

> <

App

/ > < phosphorus BrowserRouter>

< p React.StrictMode>
) ; In your index.html, create the root div in the document soundbox above the script tag. It’s besides helpful to provide a noscript disengagement message for users who may have disabled JavaScript, unless you plan on server-rendering your app belated .

 < 

body

> <

noscript

> You need to enable JavaScript to run this app. < phosphorus noscript

> <

div

id

= ''

root

`` > < phosphorus div>
<

script

src

= ''

./index.js

`` > < phosphorus script>
< phosphorus consistency>
immediately that React and React Router are set up create a new charge App.js and add some routes and components :

 

// App.js

import

*

as

React

from

``

react

`` ;

import

{

Routes

,

Route

,

Link

}

from

``

react-router-dom

`` ;

function

App

( ) {

return

( <

div

> <

header

> <

h1

> Welcome to React Router ! < p h1>

< p heading>
<

Routes

> <

Route

path

=

``

/

``

element

=

{

<

Home

/ >

}

/ > <

Route

path

=

``

about

``

element

=

{

<

About

/ >

}

/ > < p Routes>
< phosphorus div>
) ; }

function

Home

( ) {

return

( < > <

main

> <

h2

> Welcome to the home page ! < p h2>
<

p

> You can do this, I believe in you. < phosphorus p>
< p main>
<

nav

> <

Link

to

=

``

/about

`` > About < phosphorus Link>
< phosphorus nav>
) ; }

function

About

( ) {

return

( < > <

main

> <

h2

> Who are we ? < p h2>
<

p

> That feels like an existential wonder, do n't you think ? < phosphorus p>
< p main>
<

nav

> <

Link

to

=

``

/

`` > Home < p Link>
< p nav>
) ; }

export

default

App

; now start your app by running npm start, and you should see the Home path when your app starts running. Click the `` About '' link to see your About route, and voilà ! You successfully set up React Router using Parcel ! 🥳

Webpack

Follow the instructions in the webpack documentation to set up a raw project, then follow the initiation instructions above to install React Router in your project. Setting up a new React project in webpack is a bit more involve than Parcel or Create React App. Because webpack is a subordinate tool that allows you to fine-tune your construct to your like, you may want to read the webpack software documentation or check out webpack configurations in other repos to understand how to build your own. once you have webpack configured and the necessity dependencies installed, somewhere in your code ( credibly towards the root of your React component corner ) you can import the modules you need from react-router-dom .

 

import

{

BrowserRouter

,

Routes

,

Route

, }

from

``

react-router-dom

`` ;

function

App

( ) {

return

( <

BrowserRouter

> <

div

> <

h1

> Hello, React Router ! < phosphorus h1>

<

Routes

> <

Route

path

=

``

/

``

element

=

{

<

Home

/ >

}

/ > < p Routes>
< phosphorus div>
< phosphorus BrowserRouter

>
) ; } One of the quickest ways to add React and React Router to a web site is to use good ol '

informant : https://coinselected
Category : coin 4u

Leave a Reply

Your email address will not be published.