繁体   English   中英

执行操作时redux出错:未捕获类型错误:无法读取未定义的属性“类型”

[英]Error in redux when executing an action: Uncaught type error: cannot read property 'type' of undefined

刚认真对待React。 我想这是一个基本问题,但我无法理解为什么reducer不会被解雇或更新状态:

我的HomeView.js:

     ....
          const { localeChange, counter, locale } = this.props
            return (
               <div>
                  <button onClick={() => increment(7)}>Increment</button>
              <input type='text' value = {counter}/>
               .....
            </div>
        )

    const mapStateToProps = (state) => ({
      locale: state.locale,
      counter: state.counter
    })
    export default connect(mapStateToProps, {localeChange, increment})(HomeView)

我的reducer(同一文件中的常量,动作和减速器):

export const COUNTER_INCREMENT = 'COUNTER_INCREMENT'

export function increment (text) {
  return { type: COUNTER_INCREMENT, text }
}

export default function counterIncrement (state = 0, action) {
  switch (action.type) {
    case 'INCREMENT':
      return state + action.text
    default:
      return state
  }
}

最后我的“父母”减速器:

import { combineReducers } from 'redux'
import { routeReducer as router } from 'react-router-redux'
import locale from './modules/locale'
import counter from './modules/counter'

export default combineReducers({
  locale,
  router,
  counter
})

我的理解:

在我的州,我有一个名为counter的字段(它使用redux dev工具)。 当我点击按钮时我调度增量动作,所以我真的应该发出这样的动作:{type:COUNTER_INCREMENT,text:7}

但是,counterIncrement函数(reducer)获取未定义的操作: Uncaught type error: cannot read property 'type' of undefined 我能以任何方式正确调试吗? 我在reducer counterIncrement中放了一个断点但是没有被触发。

Redux验证您触发的每个操作实际上都包含一个type属性。

我的猜测是你用undefined调用dispatch() 您的示例代码不包含任何调度调用,因此我将无法进一步提供帮助 - 但我的猜测是,弄清楚它将是微不足道的。 您使用错误的参数调用dispatch() ,或者您的操作创建者不返回具有type属性的对象。

旁注(与您的问题无关),但您的动作创建者类型标签与减速器内的标签不匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM