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

use*_*996 7 javascript redux

刚认真对待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)
Run Code Online (Sandbox Code Playgroud)

我的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
  }
}
Run Code Online (Sandbox Code Playgroud)

最后我的"父母"减速器:

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
})
Run Code Online (Sandbox Code Playgroud)

我的理解:

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

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

Sim*_*ias 5

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

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

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

  • 乌普斯,是的。它不在我上传的代码中,但我正在编写 import increment 而不是 import {increment} (2认同)