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.
The operating environment of this tutorial: Windows 10 system, react17.0.1 version, Dell G3 computer.
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='/' exact>首页</NavLink> <NavLink to='/about'>关于</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!