Home >Web Front-end >Front-end Q&A >How to install routing in npm in react

How to install routing in npm in react

WBOY
WBOYOriginal
2022-04-21 15:43:051832browse

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

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'

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>
}

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!

Statement:
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