为什么在使用redux和react.js时我的道具“未定义”
Why are my props `undefined` when using redux and react.js?
我试图添加一个状态到我的应用程序,只是保存一个布尔当一些事件已经通过。我不知道我哪里做错了。
减速器:
import * as actionTypes from '../constants/action-types.js';
const initialState = [{
eventPassed: false
}];
export default function eventPassed(state = initialState, action) {
switch (action.type) {
case actionTypes.EVENT_PASSED:
return true;
default:
return state;
}
}
行动:
import * as actionTypes from '../constants/action-types';
export function eventPassed(eventPassed) {
return {
type: actionTypes.EVENT_PASSED,
payload: { eventPassed: eventPassed }
};
}
组件周围的容器:
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import Example from '../components/example.jsx';
import { eventPassed } from '../actions/app-actions';
class ExampleContainer extends Component {
render() {
return (
<Example {...this.props} />
);
}
}
const mapDispatchToProps = (dispatch) => ({
actions: bindActionCreators({
eventPassed
}, dispatch)
});
const mapStateToProps = (state) => ({
eventPassed: state.eventPassed
});
export default connect(mapStateToProps, mapDispatchToProps)(ExampleContainer);
组件:
import React, { Component, PropTypes } from 'react';
class Example extends Component {
constructor() {
super();
this.action = this.action.bind(this);
}
componentDidMount() {
this.props.actions.eventPassed(true);
}
action() {
console.log(this.props.eventPassed);
}
render() {
return (
<div>
<button onClick={this.action}>Action</button>
</div>
);
}
}
export default Example;
当我尝试记录"this.props. . "eventPassed"在 <Example />
组件它给我"undefined"。是不是少了什么?这似乎是redux中最简单的存储用法。
为什么this.props.eventPassed被记录为"未定义"?:
您此时试图访问的动作函数(
eventPassed
)在this.props.actions.eventPassed
不存在。它实际上只存在于this.props.actions
这是因为您将操作方法绑定到值'actions'在mapDispatchToProps中。这是轮流提供给访问
eventPassed
通过this.props.actions
的动作由于this.props.actions指向
eventPassed
,通过尝试访问this.props.actions.eventPassed
您正在尝试访问该属性动作eventPassed
上的eventPassed。结果是,当您记录此属性时收到"undefined"
其他必要修改:
mapDispatchToProps
需要返回一个值
mapDispatchToProps = (dispatch) => {
return { actions: bindActionCreators(eventPassed, dispatch) }
}
初始状态是一个包含对象的数组:
const initialState = [{
eventPassed: false
}];
由于您稍后试图引用它作为object { eventPassed: true}
而不是对象[ { eventPassed: true } ]
的数组,它应该是:
const initialState = {
eventPassed: false
};
reducer需要传回正确的更新(但未突变)状态:
export default function eventPassed(state = initialState, action) {
switch (action.type) {
case actionTypes.EVENT_PASSED:
return {
...state,
eventPassed: action.payload
};
default:
return state;
}
}
你最初所做的是返回一个状态,它不再是一个对象(或者在原始情况下是一个对象数组),而只是true
在你的reducer
你初始化你的状态与数组的对象有eventPassed
属性,同时你返回只是布尔值,这是不正确的,因为它取代了初始状态,所以initState可以只是一个对象持有布尔值,你必须返回的状态基于payload
发送的动作发送如下:
import * as actionTypes from '../constants/action-types.js';
const initialState = {
eventPassed: false
};
export default function eventPassed(state = initialState, action) {
switch (action.type) {
case actionTypes.EVENT_PASSED:
return {
...state,
eventPassed: action.payload
};
default:
return state;
}
}
此外,在您的component
中,您可能需要更改:
this.props.eventPassed
到this.props.actions.eventPassed
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- JSON偶尔未定义-我该如何检查
- setInterval调用具有未定义参数的函数
- JSHint 错误:未定义我的应用程序 (W117)
- 在 javascript 中返回未定义值的单选按钮
- 更改 Javascript 对象中所有未定义变量的值
- 使用 RESTful API,如果未定义特定的响应 json 级别,我如何在客户端不出错
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 查找未定义值的所有对象
- 未定义类型的数据没有值
- 具有null或未定义值的JavaScript字符串串联行为
- 检查链接对象形式中未定义属性的最佳实践是什么
- RequireJS 2.0-定义我的Backbone.Return
- 单击未定义数量的对话框
- 网络浏览器正在成为“;严格的“;用于Javascript中未定义变量的操作
- $ 未在我的 JavaScript 控制台中定义
- 函数未定义..我的函数调用不起作用
- 为什么我在chrome中得到未定义函数的错误
- 为什么我得到“未捕获的类型错误:无法读取未定义(...)的属性'长度'".
- 为什么我得到未定义错误的属性