Almog
Almog

Reputation: 2837

React Native Redux Reducer with Immutability Helper correct way to push an array

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

Answers (1)

Anthony Raymond
Anthony Raymond

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

Related Questions