Reputation: 2007
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;
}
}
Upvotes: 2
Views: 1429
Reputation: 1953
It is because the push method modifies the array it is called on and returns the new length. What you want is something like concat().
push: https://www.w3schools.com/jsref/jsref_push.asp
concat: https://www.w3schools.com/jsref/jsref_concat_array.asp
compliments: state.compliments.concat(action.payload)
Upvotes: 4