react 路由 router
程序员文章站
2022-03-26 14:01:27
...
1、先安装路由
npm i react-router-dom -S
2、在 app.js 里接入
import React from 'react'
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom'
3、接入路由组件
const About = () => (
<div>
<h2>About</h2>
</div>
)
const Home = () => (
<div>
<h2>Home</h2>
</div>
)
//使用 link Route
const BasicExample = () => ( <Router> <div> <ul> <li><Link to="/">Home 导航</Link></li> <li><Link to="/about">About 导航</Link></li> </ul> <hr/>
<Route exact path="/" component={Home}/> <Route path="/about" component={About}/>
或者 routes 是数组 ,sidebar 是组件名称
{routes.map((route, index) => ( <Route key={index} path={route.path} exact={route.exact} component={route.sidebar} /> ))}
</div> </Router>)
export default BasicExample