react-router使用redux时,同层级route的state不能关联

<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! 为什么呢?

阅读 2.9k
2 个回答

可能的原因是你在PCIndex 中已经dispatch的action,被reducer处理时有错误,新返回的state不包含logStatus。你可以在reducer处理的地方打个断点看看返回的state到底是什么

你看看你在PCIndex中发送action时,Store中的logStatus数据有没变化,正常是变了的

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题