首页 > 解决方案 > Normalizr to redux store - 多个 API 请求的最佳实践

问题描述

我正在使用带有 normalizr 的 redux。假设我们要加载客户的数据,但只能通过选择特定客户来加载,但最终可能会加载一些客户:

    const foo = new schema.Entity("order");
    const bar = new schema.Entity("infos");
    const customer_data = new schema.Entity(
     "cust_data",
      {
       orders: [foo],
       infos: [bar]
      }
     {idAttribute: "custId"}
    );
    const normaled = normalize(dataFromOneRequest, customer_data);

假设服务器使用来自单个客户的 JSON 响应,我们将result只有一个custId和规范化的entities.

假设应用程序的一部分显示客户列表,您可以深入了解细节。处理后续客户数据请求的最佳做法是什么?一旦第二个请求返回,来自第一个请求的数据已经被规范化。

将您的 redux 存储构建为[{customer: {normalizedData}}]我们有点失去顶级result数组的好处,或者以某种方式将结果和多个请求的实体添加在一起更好?

更新:当前策略

...
const myreducer = (state, action) =>
 produce(state, draft => {
  switch(action.type) {
   case NEW_CUST_FETCH_SUCCESS:
    _.mergeWith({}, draft.cust_data, action.newNormalizedReqResponse)

只是合并,但我认为它会导致不必要的重新渲染

标签: reduxnormalizr

解决方案


推荐阅读