I'm having an issue where when I try to update an array in the store by using .push(newvalue). When I push the new value the state is updated from an array to being the value of the length of the array.
State before update: ['asd', 'sdf', 'wer']
State after: 4
action:
export function updateLocalCompliments(newCompliment) {
return ({
type: LOCAL_COMPLIMENT,
payload: newCompliment
})
}
reducer: (state.compliments is the actual array)
const INITIAL_STATE = {
compliments: []
}
function compliments(state=INITIAL_STATE, action) {
switch (action.type) {
case LOCAL_COMPLIMENT:
return (
Object.assign({}, state, {
compliments: state.compliments.push(action.payload)
})
)
default:
return state;
}
}
pushdoes not return the mutated array but its updated length. You will need to handle this differently: developer.mozilla.org/de/docs/Web/JavaScript/Reference/…pushshouldn't be used with react-redux as it mutates the array