【发布时间】:2016-10-16 09:59:01
【问题描述】:
我遇到了这个奇怪的问题,我的 react-redux 存储正在更新,但在调用操作的函数中没有更新。
this.props.active是undefined,然后我用this.props.actions.activeSet(activeProc)将它设置为整数,但它仍然是undefined并进入下一个if条件。
我知道我的应用程序正在运行,因为其他一切都适用于具有正确值的 this.props.active。
这应该发生吗?
编辑:
经过一些测试,onClick 函数内部的状态似乎保持不变。
在 onClick 函数中对console.log(this.props) 的所有调用均未显示状态更改,但在末尾添加setTimeout(() => {console.log(this.props)}, 1) 进行测试表明状态正在更新。
应用的其他部分正在按预期工作,状态更改会立即应用。
但我还是不明白发生了什么。
组件功能代码
() => {
console.log(this.props.active); // undefined
if (this.props.active === undefined && this.props.readyQueue.length > 0) {
let activeProc = this.props.readyQueue[0];
this.props.actions.readyPop();
this.props.actions.activeSet(activeProc); // set to an integer
this.props.actions.execStateSet("Running");
}
console.log(this.props.active); // still remains undefined
if (this.props.active === undefined) {
this.props.actions.execStateSet("Idle");
}
}
function mapStateToProps(state, props) {
return {
active: state.ProcessReducer.active,
};
}
操作代码
export const activeSet = (procId) => {
return {
type: 'ACTIVE_SET',
procId
}
}
reducer 代码
case 'ACTIVE_SET':
return Object.assign({}, state, {
active: action.procId
});
【问题讨论】:
-
您确定
active属性与props相关联,而不是其他对象吗? - 检查mapStateToProps() -
@ebramtharwat 我编辑了我的问题以包括 mapStateToProps()。我想我已经正确映射了它。
-
控制台。在reducer中记录状态,看看它是否设置正确,你也可以使用chrome的redux扩展来调试
标签: javascript reactjs redux