Today I Learned

Redux suggested store structure.

Redux itself is not opinionated about how we design the store structure, but having good structure allows us to write simpler, less bug prone code. Based on redux style guide, state normalization and own experiences I would like to suggest some data shape for single resource. Depending on the specific needs of the project the structure should be adjusted, but it should be sufficient for standard use-cases.

const resource = {
  // normalized data for resource
  byId: {
    id1: {id: "id1", ...rest_of_item1_data},
    id2: {id: "id2", ...rest_of_item2_data},
  },

  // default list of items (key 'list' is just example)
  list: {
    // ids of users in the list
    // ids could be sorted, filtered
    // so there is no need to calculate them in selectors
    ids: ["userId1", "userId2"],
    // status of list operation
    // can be idle, loading, loaded, updating, etc.
    status: 'idle',
    // error data for the whole list operations (when appears)
    error: false,
    // metadata for the whole list
    // loadedAt and state are just example properties
    meta: {
      loadedAt: 7305798374957,
      stale: false,
    }
  },

  // when there is a need to manage another list
  someOtherList: {
    ids: ["userId4", "userId5"],
    status: 'loading',
    error: false,
    meta: {}
  },

  // default singular resource (key 'single' is just example)
  // for CRUD operations on single resource
  single: {
    id: 'id1',
    status: 'idle',
    error: false,
    meta: {}
  },

  // when there is a need to manage another singular resource
  anotherSingle: {
    id: 'id1',
    status: 'idle',
    error: false,
    meta: {}
  }
}