版本说明
REACT ROUTER:5.1.2
ReactJS :16.12
1. 安装
npm install -S react-router-dom history
# OR
yarn add -S react-router-dom history
2. 引入配置
app.jsx
import React from "react";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import HomePage from '_pages/HomePage'
import AboutPage from '_pages/AboutPage'
import UsersPage from '_pages/UsersPage'
export default function App() {
return (
<Router>
<div>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/users">Users</Link></li>
</ul>
</nav>
{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Switch>
<Route path="/about" component={AboutPage} />
<Route path="/users" component={UsersPage}/>
<Route path="/" component={HomePage} />
</Switch>
</div>
</Router>
);
}
3. 路由跳转
3.1 Link、NavLink、Redirect
<Link />
参数: to: string\object\function、replace: bool、innerRef: function 、innerRef: RefObject
to: string/object/function
// string
import { Link } from "react-router-dom";
<Link to="/about">About</Link>
// to: object
<Link
to={{
pathname: "/courses",
search: "?sort=name",
hash: "#the-hash",
state: { fromDashboard: true }
}}
/>
// to: function
<Link to={location => ({ ...location, pathname: "/courses" })} />
<Link to={location => `${location.pathname}?sort=name`} />
replace: bool
import { Link } from "react-router-dom";
<Link to="/about" replace />
innerRef: Function/RefObject
// Function
import { Link } from "react-router-dom";
<Link
to="/"
innerRef={node => {
// `node` refers to the mounted DOM element
// or null when unmounted
}}
/>
// RefObject
let anchorRef = React.createRef()
<Link to="/" innerRef={anchorRef} />
<NavLink />
import { NavLink } from "react-router-dom";
<NavLink exact to={path} activeClassName="selected" ></NavLink>
// activeStyle
<NavLink
to="/faq"
activeStyle={{
fontWeight: "bold",
color: "red"
}}
>
FAQs
</NavLink>
<Redirect />
import { Redirect } from "react-router-dom";
<Redirect push to={path} ></Redirect>
3.2 history
import { createBrowserHistory } from 'history'
const history = createBrowserHistory();
<Router history={history}>
...
</Router>
// history.push(path, [state])
// history.replace(path, [state])
// history.go(n)
// history.goBack() 等价于 history.go(-1)
// history.goForward() 等价于 history.go(1)
3.3 withRouter
import React from "react";
import PropTypes from "prop-types";
import { withRouter } from "react-router";
// A simple component that shows the pathname of the current location
class ShowTheLocation extends React.Component {
static propTypes = {
match: PropTypes.object.isRequired,
location: PropTypes.object.isRequired,
history: PropTypes.object.isRequired
};
render() {
const { match, location, history } = this.props;
return(<div>
<div onClick={()=> history.push('/some/Path')}>history push</div>
<div>You are now at {location.pathname}</div>
</div>
)
}
}
// Create a new component that is "connected" (to borrow redux
// terminology) to the router.
const ShowTheLocationWithRouter = withRouter(ShowTheLocation);
4. 路由传参
4.1 通过Query传参
// 传送
<Link to="/account?name=netflix">Netflix</Link>
// 接收
const { name } = this.props.location.query
4.2 通过Params传参
// 传送
<Link to="/account?name=netflix">Netflix</Link>
// 接收
const { name } = this.props.params
4.3 通过State传参
// Link
<Link to={{ pathname : ' /account' , state : { name: 'netflix' }}}>
// 事件 - history.js
history.push('/account', { name: 'netflix' });
// 事件 - this.props.history
this.props.history.push({
pathname: '/account',
state: { name: 'netflix' }
})
// 接收 - history.js
// import { createBrowserHistory } from 'history';
// const history = createBrowserHistory();
// Get the current location.
const location = history.location;
const { name } = location.state
// 接收 - this.props.history
const { name } = this.props.location.state
5 Hooks [React >= 16.8 ]
6. Routers
6.1 < BrowserRouter >
常规URL,如使用服务端渲染,需要使用此模式。为微信平台开发用到一些回调地址等,也需要此模式,哈希模式,#之后部分(包括#)不会发送到服务端,会造成URL不匹配等类似麻烦。
6.1 < HashRouter >
哈希模式,如:http://example.com/#/your/page