简体   繁体   English

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

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

Just new to React. 刚认真对待React。 I guess It's a basic question but I can't get why the reducer doesn't get fired or update the state: 我想这是一个基本问题,但我无法理解为什么reducer不会被解雇或更新状态:

My HomeView.js: 我的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)

My reducer (constant, action and reducer in the same file): 我的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
  }
}

And finally my "parent" reducer: 最后我的“父母”减速器:

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
})

What I understand: 我的理解:

In my state I have a field named counter (it's there using redux dev tools). 在我的州,我有一个名为counter的字段(它使用redux dev工具)。 When I click the button I dispatch increment action, so I really should dispatch an action like this: {type: COUNTER_INCREMENT, text: 7} 当我点击按钮时我调度增量动作,所以我真的应该发出这样的动作:{type:COUNTER_INCREMENT,text:7}

However counterIncrement function (reducer) gets action as undefined: Uncaught type error: cannot read property 'type' of undefined . 但是,counterIncrement函数(reducer)获取未定义的操作: Uncaught type error: cannot read property 'type' of undefined Any way I could debug this properly? 我能以任何方式正确调试吗? I put a breakpoint in the reducer counterIncrement but doesn't get fired. 我在reducer counterIncrement中放了一个断点但是没有被触发。

Redux validate that every action you trigger actually contains a type property. Redux验证您触发的每个操作实际上都包含一个type属性。

My guess is that you're calling dispatch() with undefined. 我的猜测是你用undefined调用dispatch() Your example code doesn't contains any dispatch calls, so I won't be able to help further - but my guess is that it'll be trivial to figure out. 您的示例代码不包含任何调度调用,因此我将无法进一步提供帮助 - 但我的猜测是,弄清楚它将是微不足道的。 Either you call dispatch() with the wrong arguments, or your action creator doesn't return an object with a type property. 您使用错误的参数调用dispatch() ,或者您的操作创建者不返回具有type属性的对象。

Side note (unrelated to your question), but your action creator type label doesn't match the one inside your reducer. 旁注(与您的问题无关),但您的动作创建者类型标签与减速器内的标签不匹配。

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

相关问题 未捕获的类型错误:无法读取未定义的属性 - Uncaught type error: cannot read property of undefined Chrome 未捕获类型错误:无法读取未定义的属性 - Chrome Uncaught Type Error: Cannot read property undefined 未捕获的类型错误:无法在greetGenerically读取未定义的属性“ sayHello” - Uncaught Type Error: Cannot read property 'sayHello' of undefined at greetGenerically 无法读取未定义的属性“样式”——未捕获的类型错误 - Cannot read property 'style' of undefined -- Uncaught Type Error 未捕获的类型错误:无法读取未定义的属性“ajax” - Uncaught type error: Cannot read property 'ajax' of undefined 未捕获的类型错误:无法读取未定义的“发送”属性 - Uncaught type error: Cannot read property of 'send' undefined 未捕获的类型错误无法读取未定义的属性“ length” - uncaught type error cannot read property 'length' of undefined 未捕获的类型错误无法读取未定义的属性“事件” - Uncaught type error cannot read property 'event' of undefined 骨干js-未捕获的类型错误:无法读取未定义的属性“ on” - Backbone js - Uncaught type error: Cannot read property 'on' of undefined Uncaught TypeError:无法读取未定义的React / Redux的属性&#39;type&#39; - Uncaught TypeError: Cannot read property 'type' of undefined React/Redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM