【问题标题】:'action' is not defined in reducer - React, Redux'action' 没有在 reducer 中定义 - React, Redux
【发布时间】:2018-11-08 07:45:06
【问题描述】:

我已经设置了一个减速器,但是控制台抱怨“动作”没有定义为 no-undef

import { getName } from '../actions';
let defaultState={
 name: "Sam"
}
const mainReducer=(state = defaultState, simpleAction)=>{
if (action.type === "CHANGE_NAME") {
return{
  ...state,
  name: action.name
}

} else {
return{
  ...state
}
}
}

export default mainReducer

【问题讨论】:

  • simpleAction
  • 应该是(state = defaultState, action)

标签: reactjs redux


【解决方案1】:

只需将simpleAction 更改为action

【讨论】:

    【解决方案2】:

    这样做:

    import { getName } from '../actions';
    let defaultState = { name: "Sam" }
    const mainReducer = (state = defaultState, action) => {
        if (action.type === "CHANGE_NAME") {
            return { ...state, name: action.name }
        }
        else {
            return { ...state }
        }
    }
    export default mainReducer
    

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 2021-08-11
      • 2016-08-16
      • 2022-01-13
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 2017-03-17
      相关资源
      最近更新 更多