可以't访问上下文.带有react路由器redux的路由器
Can't access context.router with react-router-redux
我目前正在一个项目中使用react router redux,除了我不知道如何访问react router-router之外,一切都很好。
@connect((state, props) => {
return {
notification:state.getIn(['_display', 'notification']),
loadingScreen:state.getIn(['_display', 'loadingScreen']),
route:props.location.pathname
};
})
export default class Consultation extends React.Component {
constructor(props,context) {
super(props, context);
console.log(context.router);
}
componentDidMount(){
const { router } = this.context;
.....
}
render(){
....
}
}
Consultation.contextTypes = {
router: React.PropTypes.object.isRequired,
store:React.PropTypes.object
};
this.context.router总是未定义的,我已经尝试了很多事情,但都没有成功
我正在使用react 0.14.6、redux 3.0.2、react router 2.0.0、react router redux 4.0.0
您将.contextTypes
放在connect()
返回的组件上,而不是放在您自己的组件上。这就是为什么上下文不可用于组件的原因。
试试这个:
class Consultation extends React.Component {
constructor(props, context) {
super(props, context);
console.log(context.router);
}
componentDidMount(){
const { router } = this.context;
.....
}
render(){
....
}
}
Consultation.contextTypes = {
router: React.PropTypes.object.isRequired,
store:React.PropTypes.object
}
export default connect(...)(Consultation)
请注意,我们首先指定contextTypes
,并导出连接的组件,而不是将contextTypes
分配给从@connect
装饰器获得的已连接组件。顺便说一句,这是避免装饰师的另一个原因!
相关文章:
- 如何使用带有Preact的React路由器
- 如何在react路由器的组件中使用参数
- React路由器服务器端渲染和ajax获取数据
- Mocha react本地路由器编译错误
- react路由器使用简单的javascript路由器配置来处理不匹配的路径
- React路由器错误-'无法调用方法'getRouteAtDepth'的未定义'
- 有条件地与react路由器链接
- react-带有路由器的路由器不插入路由器
- 如何使用服务器上的react路由器处理所有可能的路由
- React Router一直给我一个警告:你不能更改<路由器路由>
- React.js-React路由器内容未更改
- 使用react路由器隐藏组件
- React 路由器 activeClassName 不会为子路由设置活动类
- react路由器(-redux)未匹配路由
- 从使用react路由器的网络应用程序解析物理URL
- React 路由器和任意查询参数:页面在加载时无意中刷新
- 如何使用react路由器的正常锚链接
- 未捕获的类型错误:hook.apply不是在react路由器中使用onEnter时的函数
- 使用Redux路由器+React路由器向应用程序添加基本URL
- 为什么'this.props.children;null用于react路由器(react Redux)