如何使用带有两个对象的initialState为减速器赋值?

时间:2017-06-18 04:34:26

标签: reactjs redux react-redux

我有以下减速器,用户提交评级并获得用户的评分(your_ratings)......

发生LOAD_YOURRATINGS_SUCCESS时,正在移除两个级别的initalState,your_ratings正在变为store.rating的值,当我希望它为store.rating.your_ratings时。 / p>

下面我做错了什么?感谢

import * as types from '../actions/actionTypes';

const initialState = {
  rating: {},
  your_ratings: {}
}

export default function ratingReducer(state = initialState, action) {
  switch (action.type) {
    case types.CREATE_RATING_SUCCESS:
      return action.rating
    case types.LOAD_YOURRATINGS_SUCCESS:
      return action.your_ratings
    default:
      return state;
  }
}

1 个答案:

答案 0 :(得分:2)

你需要更新你的状态而不是覆盖它,你可以在传播运营商的帮助下做到这一点,比如

export default function ratingReducer(state = initialState, action) {
  switch (action.type) {
    case types.CREATE_RATING_SUCCESS:
      return {...state, rating: action.rating}
    case types.LOAD_YOURRATINGS_SUCCESS:
      return {...state, your_ratings: action.your_ratings}
    default:
      return state;
  }
}