Reputation: 2837
I'm using react native with Redux and in my reducer using Immutability Helper what's the correct way to push a new item into my array? Here is my code
State
const initialState = {
photos: [],
comments:[],
hasData:false,
};
Reducer
switch (action.type) {
case actionTypes.INSERT_PHOTO:
return update(state, {$push: [action.data]});
case actionTypes.CLEAR_PHOTOS:
return [];
default:
return state;
}
I have tried update(state.photos, {$push: [action.data]});
But that does not work
Upvotes: 1
Views: 395
Reputation: 7872
You are not accessing nested properly properly, try :
update(state, {
photos: {$push: [action.data]}
});
You syntax returns the photos
array, not the state, it most likely break redux state.
Upvotes: 1