C
C
Cyril2020-07-20 13:11:09
React
Cyril, 2020-07-20 13:11:09

How to use more than one React component at the same time if only one Reducer?

Let's say we have a React page. It currently contains only one React component "Catalogue", which is a catalog of some items, with pagination, sorting and other "settings".

But there was a need to place another such catalog below on the same page, but just with slightly different settings.

What is the correct way to organize the Reducer in this case, if I store the loaded catalog elements, the sort direction, the pagination page and other settings associated with the catalog in the reducer?

After all, there will now be two directories, and the reducer is calculated that only one directory will be initialized on the page. How would you do it? The data structure in the reducer is as follows:

{
    items: [],
    page: 1,
    sort: null,
    filter: null,
    category: null
}


This is how it is at the moment. If the reducer is adapted for the simultaneous use of several directories, then I think you can enter subsections into the object. What are your thoughts?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-07-20
@dimoff66

If the data sort is specific to the current component, it can be stored in the component's state. In redux, it makes sense to store only the data that is used by several components or that needs to be saved between sessions. If, nevertheless, there is a need to store sortings and selections in the redax storage, then simply create some keys that are unique for the component and store them in them

{
  items: [],
  settings: {
    key1: { filter: {}, sort: {}, page: 2 },
    key2: { filter: {}, sort: {}, page: 1 },
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question