R
R
RomanSS2017-01-12 10:42:47
JavaScript
RomanSS, 2017-01-12 10:42:47

Is it the right way to update data in a reducer?

There is such a nesting in the reducer. key1_id1 and subkey1_id1 are id values, there can be many such values

{
    key1_id1: {
       subkey1_id1: {
           bd: {
               isLoading: false,
               data: {}
           }
       }
    }
}

There was a problem with updating the value of bd, so that data from data is not erased when updating isLoading, you have to do a lot of nesting with Object.assign. Approximately how it looks:
action.payload.key='key1_id1';
action.payload.subkey='subkey1_id1';
action.payload.data="какой-то объект";

case UPDATE_DATA:
return Object.assign({},state,
    {
        [action.payload.key]: Object.assign(
            {},
            state[action.payload.key],
            {[action.payload.subkey]: 
                Object.assign(
                    {}, 
                    (state[action.payload.key])?state[action.payload.key][action.payload.subkey]:{},
                    action.payload.data
                 )
            }
        )
    });

It seems that one more nesting is needed, but this is not the question, how to be in such situations?
1) Is it correct to update the data with such nestings? Will it greatly affect performance if there are a lot of objects?
2) Is there a better way to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shelomanovd, 2017-01-12
@shelomanovd

reducer is state management?

O
Oleg Drapeza, 2017-01-18
@SuperOleg39ru

Make the reducer decomposition a little smaller.
link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question