【发布时间】:2018-07-09 13:42:23
【问题描述】:
我对 Redux 有问题,更可能不是问题而是误解。 如果我在一个函数中调度并在存储中写入一个新值,那么我不能立即从存储中获取一个新值。
示例:
testFunc = () => {
console.log('in func: before', this.props.count); //state store
this.props.onButtonIncrementClick(); //dispatch a new state in the store
console.log('in func: after', this.props.count); //get the old state store
};
实例: https://codesandbox.io/s/yk1jzjv6l1
我怀疑这与异步有关,因为如果我将状态包装在 setTimeout() 中,则会出现新状态。
我很高兴听到关于为什么以及如何将值写入存储并立即从中读取它们的解释。
【问题讨论】:
-
“我很高兴听到关于为什么以及如何将值写入存储并立即从中读取的解释。”听起来像XY-problem。一般可以使用
componentDidUpdate方法获取新值。
标签: javascript reactjs redux react-redux