React Router 提供了多种页面跳转的实现方式,主要包括以下三种。
方式一:使用 withRouter 高阶组件
import { withRouter } from 'react-router-dom';
class Index extends Component {
changePage = () => {
const { history } = this.props;
history.push({ pathname: '/Home' });
}
}
export default withRouter(Index);
方式二:使用自定义 history 实例
// history.js
import { createHistory } from 'history';
export default createHistory();
// Index.js
import history from './history';
export default class Index extends Component {
changePage = () => {
history.push({ pathname: '/Home' });
}
}
方式三:使用 NavLink 组件
import React, { Component } from 'react';
{ } ;
{
() {
(
);
}
}
;

