作用
高阶组件中的withRouter, 作用是将一个组件包裹进Route里面, 然后react-router的三个对象history, location, match就会被放进这个组件的props属性中.
例如我们需要用到点击跳转
- 当前组件不是一个Router,需要引入
import { withRouter } from "react-router-dom";
{adminRoutes.map(route => {
return (
<Menu.Item
key={route.path}
onClick={p => props.history.push(p.key)}
>
icon={<SmileOutlined/>}
{route.title}
</Menu.Item>
)
})}
然后使用
export default (withRouter(Index));