I am learning Redux. However, I understood that the reducer is a function that receives the current state and an action object, then decides how to update the state, make a copy of it, update the deep copied state and finally returns the new state.
So, the code below is a typical example of it:
const initialState = { value: 0 }
function counterReducer(state = initialState, action) { // Check to see if the reducer cares about this action if (action.type === 'counter/increment') {
// If so, make a copy of `state`
return {
...state,
// and update the copy with the new value
value: state.value + 1
} } // otherwise return the existing state unchanged return state }
However, you realize that it is using the spread operator to make a copy of the state. The spread operator make just a shallow copy of it. So, it means that the state should not be multidimensional object?
Because, as I know, the spread operator does not work with multi-dimensional arrays or objects. If it is multi-dimensional the values are copied by referenced instead of by value. It is the reason that if you really want to make a deep copy of an object or an array you could use Lodash library, structuredClone or even JSON.parse(JSON.stringify("your object/array" here).
So, does someone know if the store is just one level object store in Redux? Then in this case it works without any problem using spread operator. Or it is a spread operator representation that intern the Redux Library makes really a deep clone of it?
Thank you very much in advance!
Cheers, Turtles