in React and I wrote a reducer function like this
const reducer = (state, action) =>{
switch(action.type){
case "ADD_CONTACT": return {
...state,
contacts: state.contacts.push(action.payload)
}
}
}
Now I know the mistake I did, since I'm trying to add something new to something that is immutable but my doubt is, after this, state.contacts contains the value 4 (there were 3 hardcoded existing contacts)
How does that happen?
Redux state is not immutable by itself, you have to do that yourself
Spread operator it's your friend
push doesn't return the array it pushes to, try this instead: contacts: [ ...state.contacts, action.payload ]
Обсуждают сегодня