0

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?)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering. Turns out it variable typo :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.