peter flanagan
peter flanagan

Reputation: 9790

React - Update array in reducer Fill with objects

I am trying to update the state of an item in the store.

This is working but it is not returning the state in the format I am looking for.

The state looks like this:

state : {
   watchList: [
      {
        movie: {
          'name' : 'Batman'
        }
      }
   ]
}

however, I have been attempting to make my state look like this (i.e. not have another object inside the first object, but just an array of objects).

state : {

   watchList: [{'name' : 'Batman'}. {'name': 'Superman'}]

}

My reducer looks like this:

export default (state = [], action) => {

    switch(action.type) {

    case 'MOVIE_ADDED_TO_LIST':
        return [
            ...state,
            {
                movie: movie.event
            }
        ];

    default:
        return state;
    }

};

and my action looks like this:

export const addMovieToList = (movie) => {

    return {
        type: 'MOVIE_ADDED_TO_LIST',
        movie
    };

}; 

And here is how I am mapping stateToProps.

function mapStateToProps(state, props) {

    return {

        WatchListEvents: state.watchList

    }

}

export default connect(mapStateToProps)(WatchList);

Upvotes: 0

Views: 439

Answers (1)

Bartek Fryzowicz
Bartek Fryzowicz

Reputation: 6674

export default (state = [], action) => {

    switch(action.type) {

    case 'MOVIE_ADDED_TO_LIST':
        return [
            ...state,
            movie.event
        ];

    default:
        return state;
    }

};

Upvotes: 1

Related Questions