简体   繁体   English

我应该如何在redux存储中存储isFetching / isLoading数据?

[英]How should I store isFetching/isLoading data in my redux store?

I am looking to create a generic way of storing isFetching for entities/data in redux. 我正在寻找一种在redux中为实体/数据存储isFetching的通用方法。 Currently I use normalizr and store all my entity data in an entities hash. 目前,我使用normalizr并将所有实体数据存储在实体哈希中。 For example 例如

{
   people: { ... },
   orders: { ... }
}

I was thinking of defining a property on each entity (ie people, orders ...) object to specify whether or not data is being fetched. 我当时正在考虑在每个实体(即人,订单...)对象上定义一个属性 ,以指定是否要获取数据。 The only problem with this is if you do something like Object.keys you will return a list of ids + isFetching in your array. 唯一的问题是,如果您执行Object.keys之类的操作,则将在数组中返回ID + isFetching的列表。 To overcome this I want to set the property to not being enumerable. 为了克服这个问题,我想将属性设置为不可枚举。

My data is fairly complex, I don't just have 2 entities, I have just simplified for the example. 我的数据相当复杂,我不仅有2个实体,还为示例进行了简化。 I am looking for a solution that can survive in my codebase in the long term. 我正在寻找一种可以长期保存在我的代码库中的解决方案。

Is this a good solution? 这是一个好的解决方案吗? I didn't want to create a new object just to store meta data (although this is a potential solution). 我不想创建一个仅用于存储元数据的新对象(尽管这是一个潜在的解决方案)。 Is there a better or more common approach? 有更好或更通用的方法吗?

I know this is slightly opinionated, but the redux issue guidelines pointed me to stackoverflow ... 我知道这有点自以为是,但是redux问题指南将我指向了stackoverflow ...

Note I am aware of compatibility issues with Object.defineProperty. 注意我知道与Object.defineProperty的兼容性问题。 This is an internal application and everyone is on the latest version of chrome. 这是一个内部应用程序,每个人都使用最新版本的chrome。

UPDATE 更新

Potential implementation in the redux store Redux存储中的潜在实现

let initialState = {
  entities: {
    customers: null,
    orders: null
  }
};

Object.defineProperty(initialState.entities.customers, 
  'isFetching',
  {
    value: false,
    writable: true,
    enumerable: false
  });

Object.defineProperty(initialState.entities.orders, 
  'isFetching',
  {
    value: false,
    writable: true,
    enumerable: false
  });


export default initialState;

I think defining non-enumerable properties will make your code base harder to maintain, because the fact that a property is not enumerable, is hidden most of the time when debugging. 我认为定义不可枚举的属性将使您的代码库更难维护,因为在调试时,大多数情况下都隐藏了不可枚举的属性。

You could just follow the examples from normalizr and have meta data sit next to the entities . 您可以按照normalizr的示例进行操作, normalizr元数据位于entities旁边。 Eg: 例如:

{
  customers: {
    entities: {/** contains your normalized customer list*/},
    isFetching: false
  },
  orders: {
    entities: {/** contains your normalized order list*/},
    isFetching: false
  }
}

This would also allow for further extension of meta data in the future. 这也将允许将来进一步扩展元数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM