redux在componentDidMount中出现的问题 --- state 不变

  遇到这样一个问题:

    在组件的componentDidMount中,我需要使用到redux中存储的某个状态。 

    但是有趣的是,当我再render中使用相同的状态时,状态会改变,但是在conponentDidMount中使用却不会改变。 

    stackoverflow上有很好的解释: https://stackoverflow.com/questions/33960247/react-componentdidmount-not-updating-the-statet

    即componentDidMount只会在第一次渲染时触发,后面就不会再触发了,即使redux中的数据发生了改变,也不会影响到componentDidMount。 

  解决方法:

    我们可以在 componentDidUpdate 中使用这个变化的 state, 只要其变化, 那么 componentDidUpdate 中的也会变化,需要格外注意。

原文地址:https://www.cnblogs.com/zhuzhenwei918/p/7261153.html