Redux 反规范化器

Redux denormalizr

我正在使用 thunk 从 API 获取数据异步,这是这样的嵌套数据:

{
  person: {
      id: <id>
      services: [{
        id: <id>,
        leader: {
          id: <id>,
          name: <name>
        }
      },...]
  }
}

现在,这被归一化为(假设我们有一堆人和东西):

{
  result: <person-id-1>,
  entities: {
    person: {
      <person-id-1> : { 
        services: [<service-id-1>,...]
      },...
    },
    service: {
      <service-id-1>: {
        leader: <leader-id-1>
      }
    },
    leader: {
      <leader-id-1>: {
        name: <name>
      }
    }
  }
}

好的,现在我必须将这个实体完全放入我的组件中,问题是,最好的方法是什么?

谢谢!

你应该检查 this video made by the Redux author and the shopping cart example in redux repo. reselect helps you to create an memoized, composable selector functions. read more here。希望对你有所帮助。

是的,标准方法是在其 mapState 函数中对给定组件进行任何数据整形,使用基于 Reselect 的选择器来记住该过程。您可能还想查看 Redux-ORM 库,它提供了一个非常好的抽象层来管理 Redux 存储中的规范化数据。我将它用于 thunks 和 mapState 函数中的数据 de-normalization/selection,以及 reducer 中的不可变数据更新。

https://medium.com/@adamrackis/querying-a-redux-store-37db8c7f3b0f 上也有一篇关于此主题的好文章。