<Provider store={this.store}>
<Router history={history}>
<Switch>
<Route path='/details/:urlkey' component={PCNewsDetails}></Route>
<Route extra path='/'>
<div>
<MediaQuery query='(min-device-width:997px)'>
<PCIndex />
</MediaQuery>
<MediaQuery query='(max-device-width:997px)'>
<MobileIndex />
</MediaQuery>
</div>
</Route>
</Switch>
</Router>
</Provider>
我的实现是,访问localhost:8080/
能够加载PCIndex(主页),然后我在PCIndex中使用了<Link to="/detail/123"></Link>
,以此跳转到PCNewsDetails(详情页面),此时的地址应该是localhost:8080/detail/123
。
然后问题来了,我在PCIndex 中使用mapStateToProps 绑定的数据 和 PCNewsDetails 里面绑定的数据不是互相关联的?
比如我在PCIndex 中使用了
const mapStateToProps = (state) => {
console.log(state);
return {
logStatus: state.reducer4log.logStatus,
}
}
又在PCNewsDetails中绑定了一样的数据
const mapStateToProps = (state) => {
console.log(state);
return {
logStatus: state.reducer4log.logStatus,
}
}
为什么这两个logStatus 值会不同? 我在PCIndex 中已经dispatch了action(用于登录请求的异步action),并通过dispatch 将logStatus 变成了true(这时候应该是出与已经登录状态);然后我通过点击上面的<Link> 跳转到了PCNewsDetails;
我以为PCNewDetails中的logStatus 也是true, 然而确实undefined! 为什么呢?
可能的原因是你在PCIndex 中已经dispatch的action,被reducer处理时有错误,新返回的state不包含logStatus。你可以在reducer处理的地方打个断点看看返回的state到底是什么