Redux needs a deep copy

The object below action.datahas a nested objectaddress

{
    name: 'Ben',
    address: {
        country: 'Australia',
        state: 'NSW'
    }
}

How do I handle it in the gearbox?

const rootReducer = (state = initState, action) {
    switch(action.type) {
        switch RECEIVE_DATA:
            return {...state, data: action.data}
    }
}

Can I do this as above? that I just assigned the whole object datawithout copying?

or

const rootReducer = (state = initState, action) {
    switch(action.type) {
        switch RECEIVE_DATA:
            const address = {...action.data.address}
            const data = {...action.data, address}
            return {...state, data}
    }
}

Or do I need to make a deep copy of the object and assign it data? thank

+4
source share
2 answers

The “correct” way of processing updates to embedded data consists of several small copies, one for each level of nesting. It is also possible to create a new object that completely replaces one field in accordance with your first example.

" Redux" , , FAQ Redux .

+7

Redux:

Redux: . : - , !

+2

Source: https://habr.com/ru/post/1673752/


All Articles