如何使用immutable.js在reducer中编写给定状态?

时间:2019-02-02 14:48:34

标签: javascript redux immutability reducers

我将如何使用Immutable JS进行转换。

fromJS包装初始状态和setIn方法以处理嵌套案例。

const initialState = {
    allMovies: [] 
};

const movieReducer = (state = initialState, action) => {
   const {type, payload} = action;
    switch (type) {
       // some actions...
       case movieActionTypes.FETCH_MOVIES_SUCCESS: {
           return {
               ...state,
               allMovies: {
                   ...state.allMovies,
                   [payload.movie.id]: {
                       ...state.allMovies[payload.movie.id],
                       ...payload.movie,
                       ready: false,
                   },
               },
           };
       }
       default:
           return state;
    }
};

0 个答案:

没有答案