组件中缺少历史记录
history missing in components
我刚才观察到我的某些组件有历史记录,而有些则没有。
我的组件是 comp1, comp2, comp3
comp1 是 comp2 的父级,comp2 是 comp3 的父级
comp1
|____comp2
|_____comp3
假设所有组件都是有状态的。而访问comp1
是通过路由。
我的观察表明那些通过路由器直接连接的组件只有 this.props.history
可用。
Q. Can any one throw some light here on this fact?
Q. Can we access history on remaining components?
component
组件的 component
组件将被赋予 route props. If you want a component deeper down in your app to have access to them as well, you can use the withRouter
HOC。
例子
class Component3 extends React.Component {
render () {
console.log(this.props.history);
return <h3> Test </h3>;
}
}
export default withRouter(Component3);
我刚才观察到我的某些组件有历史记录,而有些则没有。
我的组件是 comp1, comp2, comp3
comp1 是 comp2 的父级,comp2 是 comp3 的父级
comp1
|____comp2
|_____comp3
假设所有组件都是有状态的。而访问comp1
是通过路由。
我的观察表明那些通过路由器直接连接的组件只有 this.props.history
可用。
Q. Can any one throw some light here on this fact?
Q. Can we access history on remaining components?
component
组件的 component
组件将被赋予 route props. If you want a component deeper down in your app to have access to them as well, you can use the withRouter
HOC。
例子
class Component3 extends React.Component {
render () {
console.log(this.props.history);
return <h3> Test </h3>;
}
}
export default withRouter(Component3);