【问题标题】:How does a container component update its child component through react redux?容器组件如何通过 react redux 更新其子组件?
【发布时间】:2016-12-28 10:26:15
【问题描述】:

查看 react redux 文档中的 reddit example,我很难看到帖子列表组件如何根据选择器的状态进行自我更新。是不是通过容器方法中的componentWillReceiveProps 生命周期方法导致了这种情况?这就是我看到数据流和状态变化的方式:

  1. 用户选择reddit
  2. 存储状态已更改
  3. store 是容器组件的 props,所以实际上它的 props 被改变了
  4. componentWillReceiveProps 被触发并调度 fetchPostsIfNeeded
  5. 帖子列表已更改

这对吗?

【问题讨论】:

    标签: reactjs redux state react-redux


    【解决方案1】:

    这个 Redux 示例通过 react-redux libraries connect method 将 AsyncApp 容器及其子组件 Picker & Posts 组件连接到 Redux 存储区。 connect 方法采用 mapStateToProps 和 mapDispatchToProps 函数,使容器能够将 redux 状态和操作作为 props 传递给子组件。

    现在子组件可以访问这些道具,例如。 const { selectedSubreddit, posts, isFetching, lastUpdated } = this.props。它还可以调度这些操作来更新 redux 存储,进而更新它连接到的那些 props。

    Redux 是一种管理应用程序状态的方法,它不关心你的 UI 或你是否使用 React。 React-redux 是一个库,它提供从 React 组件到 Redux 存储的绑定。

    【讨论】:

    • 所以你是说列表组件会自我更新,因为它从连接到商店的父组件接收道具?那么 componentWillReceiveProps 方法服务的目的是什么?
    • 组件渲染方法将在状态/存储更新任何连接的道具时再次运行,并且是的,组件上的道具将更新。 componentWillReceiveProps 是一种 React 生命周期方法 - 您通常会使用此方法在组件重新渲染之前对其进行一些其他更改。你真的需要阅读 React 文档,特别是:facebook.github.io/react/docs/component-specs.html
    猜你喜欢
    • 2017-07-06
    • 2017-10-18
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2021-01-28
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多