dmr07
dmr07

Reputation: 1488

Redux updating entire array

I'm trying to figure out why my components are not updating after Redux's store receives new data. Github issues seem to indicate that I'm mutating the object without returning it, but I haven't been able to find how to do this with entire arrays.

Here's my current code.

initialState = []
... 
switch(action.type) {
   case "SET_STORIES": {
     state = [...action.payload]; // data is an array of objects. 
     // does not work either: state = [...state, action.payload];
     break;
   }
} 
return state; 

In the same command, I 'd also like to overwrite the entire array if there is previous data present.

Is this correct? (console logs logs the updated array, but does this mean its being returned to the components?)

Upvotes: 0

Views: 186

Answers (1)

Davorin Ruševljan
Davorin Ruševljan

Reputation: 4392

It is hard to tell without whole code, this snippet does look OK, at least as long as payload is array of immutable objects. However, if it is array of mutable objects, and for instance your payload carries array of same objects as previous state, but mutated, then your sub-components displaying details of each element might fail to recognize change.

Upvotes: 1

Related Questions