小编Den*_*pov的帖子

Redux 不会立即更新状态

我对 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
    };
Run Code Online (Sandbox Code Playgroud)

实例: https : //codesandbox.io/s/yk1jzjv6l1

在此处输入图片说明

我怀疑这与异步有关,因为如果我将状态包装在setTimeout()新状态中就会出现。

我很高兴听到解释为什么以及如何将值写入商店并立即从中读取它们。

javascript reactjs redux react-redux

2
推荐指数
1
解决办法
1784
查看次数

标签 统计

javascript ×1

react-redux ×1

reactjs ×1

redux ×1