Error in redux when executing an action: Uncaught type error: cannot read property 'type' of undefined
Error in redux when executing an action: Uncaught type error: cannot read property 'type' of undefined
刚刚接触 React。我想这是一个基本问题,但我不明白为什么减速器没有被解雇或更新状态:
我的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)
我的减速器(常量、动作和减速器在同一个文件中):
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
}
}
最后是我的 "parent" 减速器:
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 开发工具)。
当我单击按钮时,我会发送增量操作,所以我真的应该发送这样的操作:
{类型:COUNTER_INCREMENT,文本:7}
但是 counterIncrement 函数(reducer)得到的动作是未定义的:Uncaught type error: cannot read property 'type' of undefined
。
有什么办法可以正确调试吗?我在 reducer counterIncrement 中放置了一个断点,但没有被触发。
Redux 验证您触发的每个操作实际上都包含一个类型 属性。
我的猜测是您在调用 dispatch()
时未定义。您的示例代码不包含任何调度调用,因此我无法提供进一步的帮助 - 但我的猜测是弄清楚这将是微不足道的。要么你用错误的参数调用 dispatch()
,要么你的动作创建者没有 return 带有 type
属性.
的对象
旁注(与您的问题无关),但您的 action creator 类型标签与 reducer 中的标签不匹配。
刚刚接触 React。我想这是一个基本问题,但我不明白为什么减速器没有被解雇或更新状态:
我的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)
我的减速器(常量、动作和减速器在同一个文件中):
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
}
}
最后是我的 "parent" 减速器:
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 开发工具)。 当我单击按钮时,我会发送增量操作,所以我真的应该发送这样的操作: {类型:COUNTER_INCREMENT,文本:7}
但是 counterIncrement 函数(reducer)得到的动作是未定义的:Uncaught type error: cannot read property 'type' of undefined
。
有什么办法可以正确调试吗?我在 reducer counterIncrement 中放置了一个断点,但没有被触发。
Redux 验证您触发的每个操作实际上都包含一个类型 属性。
我的猜测是您在调用 dispatch()
时未定义。您的示例代码不包含任何调度调用,因此我无法提供进一步的帮助 - 但我的猜测是弄清楚这将是微不足道的。要么你用错误的参数调用 dispatch()
,要么你的动作创建者没有 return 带有 type
属性.
旁注(与您的问题无关),但您的 action creator 类型标签与 reducer 中的标签不匹配。