redux-actions handleActions不会改变状态

时间:2017-02-11 04:38:27

标签: reactjs redux

所以我有一堆鸭子样式的redux模块看起来像这样:

import { createAction, handleActions } from 'redux-actions'

const SET = "error/SET"
const CLEAR = "error/CLEAR"

export default handleActions({
  SET: (error, action) => action.payload,
  CLEAR: (error, action) => ""
}, "")

export const setError = createAction(SET)
export const clearError = createAction(CLEAR)

然后,在reducers.js中我这样做:

import error from './error'

export default combineReducers({
  error,
  ...
})

然而,当我发送(setError(" ERROR"))时,我看到redux devtools中的动作,但状态没有改变

1 个答案:

答案 0 :(得分:1)

您正在将包含错误密钥的地图传递给handleActions。您不希望密钥为SETCLEAR,而是取其值(error/SETerror/CLEAR)。为此,你必须将它们包在方括号中:

export default handleActions({
  [SET]: (error, action) => action.payload,
  [CLEAR]: (error, action) => ""
}, "")