How to install routing in npm in react

WBOY
Release: 2022-04-21 15:43:05
Original
1794 people have browsed it

In react, npm can use "npm i react-router-dom -S" to install routing; the parameter i is the abbreviation of install, which will detect the npm package version number that best matches the current version, and the parameter "-S" It is the abbreviation of "--save", which will write the module into packages.json.

How to install routing in npm in react

The operating environment of this tutorial: Windows 10 system, react17.0.1 version, Dell G3 computer.

How to install routing in npm in react

1. React routing installation

npm i react-router-dom@5.0 -S
Copy after login

After the installation is completed, enter App.js for reference

Import routing related components (the first letter must be capitalized)

Import hash route alias router

Route routing page

NavLink navigation link

import { HashRouter as Router, Route, NavLink} from 'react-router-dom'
Copy after login

2. Use of react routing

Example:

import {HashRouter as Router , Route , NavLink} from 'react-router-dom'
function App(){
return (<Router >
{/* 导航 */}
<div>
{/*exact 默认显示*/}
<NavLink to=&#39;/&#39; exact>首页</NavLink>
<NavLink to=&#39;/about&#39;>关于</NavLink>
</div>
{/* 路由页面 */}
<Route  path="/" exact component={Home}></Route >
<Route  path="/about" component={About}></Route >
</Router >)
}
export default App;
function Home(){
return <div>首页页面</div>
}
function About(){
return <div>关于页面</div>
}
Copy after login

This is achieved, updating the view but not re-requesting the page. If you want to know more, you can click to view the official documentation.

Recommended learning: "react video tutorial"

The above is the detailed content of How to install routing in npm in react. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template