Warm tip: This article is reproduced from serverfault.com, please click

What should be structure of Redux Store?

发布于 2020-10-31 10:18:19

Redux.org tells to normalize your state but it creates some confusion.

It tells that we should have State in following format:

{
    simpleDomainData1: {....},
    simpleDomainData2: {....},
    entities : {
        entityType1 : {....},
        entityType2 : {....}
    },
    ui : {
        uiSection1 : {....},
        uiSection2 : {....}
    }
}

I can achieve this by two ways.

case 1: I have 3 pages, home, create, feeds page. Hence I can create homeReducer.js, createReducer.js, feedsReducer.js and each reducer will have simpleDomainData1, simpleDomainData2, entities , ui.

case 2: I can create separate reducers for each field like simpleHomeReducer.js, simpleCreateReducer.js, simpleFeedsReducer.js, entitiesReducer, uiReducer.js.

But I am failing to understand, which approach is right, and why ?

Questioner
Kiran
Viewed
0
Mrjadeja 2020-12-02 13:56:31

Hey kiran as you mentioned in question that you have 2 approaches to structure your reducer.

But, i will give you a new approach.

First of all it's sounds tricky but once you think a little it is piece of cake for this project and future once also.

You should use combineReducers to combine your reducers to make it easy to use.

1. uiReducer

First you should create reducer for uiSection1 and in it you have all your logic from home Component, create Component and feeds Component for the uiSection1 only.

Just like that you create uiSection2 reducer and in it all your Component logic of your all pages related to uiSection2.

Now combine this 2 reducers to one reducer uiReducer.

2. entityReducer

Now, same thing do with entityType. Create 2 entityType reducer and combine them to one enitityReducer.

3. domainDataReducer

Now create each reducer for domain data and combine it to 1 reducer domainDataReducer

Now you have 3 reducers ui, entity and domainData.

4. rootReducer

Now, combine this 3 reducers to 1 reducer rootReducer and pass it down to index.js

And one last thing, you should do a seperate logic for all your reducer action. And in this reducer action you can do api call to backend as well.

This is the link to youtube video by TheNetNinja