Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs redux操作和手动操作不是';不要改变状态_Reactjs_Redux - Fatal编程技术网

Reactjs redux操作和手动操作不是';不要改变状态

Reactjs redux操作和手动操作不是';不要改变状态,reactjs,redux,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) => "" }

我有一堆鸭子风格的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中看到了操作,但是状态没有改变

您正在将带有错误键的map传递给
handleActions
。您不希望键是
SET
CLEAR
,而是希望它们的值(
error/SET
error/CLEAR
)。为此,您必须将它们括在方括号中:

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