Skip to content Skip to sidebar Skip to footer

My MapStateToProps Is Not Called After Adding Custom Object Array To Redux State

I am trying for few hours but can't figure out why my state is not called after adding an array of custom object. // In my component... const myRemoteArray = getRemoteArray() // Is

Solution 1:

You were pretty close in the comments you added in the reducer, but neither of them were 100% accurate.

For Redux to notice that your array has changed, you need the property adItems of your new state to return an entirely new array. You can do it like this:

adItems: [...action.adItems]

With this code you'll be creating a new array, and then adding a copy of the items of the old one into it.

The reason why your current implementation (adItems: action.adItems) is not working is that action.adItems is actually a reference to an array in memory. Even though the array contents have changed, the value of action.adItems is still the same, a pointer to where the array is currently stored. This is the reason why your store is not being updated: as Redux does not check the values of the array itself but the reference to where the array is stored, the new state you're returning is exactly the same, so Redux is not aware of any changes.


Solution 2:

As LonelyPrincess says, I was making this issue elsewhere, if you doing that xArray = yArra it means call by reference and not by value.


Post a Comment for "My MapStateToProps Is Not Called After Adding Custom Object Array To Redux State"