[英]Redux - reducer is successfully updated, but connected component's componentWillReceiveProps still shows the old state
I'm firing off an action when I get the user's current location to find markers within that location. 当我获得用户的当前位置以在该位置查找标记时,我将执行一项操作。 I get the data, update my reducer. 我得到了数据,更新了我的减速器。
However, when I console.log(this.props)
in my componentWillReceiveProps
, I still see the old state. 但是,当我在componentWillReceiveProps
console.log(this.props)
时,我仍然看到旧状态。 If I navigate to another route, and then come back, I can see the updated props from the console log from componentWillReceiveProps
. 如果导航到另一条路线,然后返回,则可以从componentWillReceiveProps
的控制台日志中看到更新的道具。
UPDATE: This was due to lack of understanding on my part of the component lifecycle. 更新:这是由于我对组件生命周期的了解不足。 Look at the answer below :) . 看下面的答案:)。
That is the intended behavior! 那是预期的行为! That function is called before the component rerenders, so that you can compare old state to 'current' state. 在组件重新渲染之前会调用该函数,以便您可以将旧状态与“当前”状态进行比较。
The reducer updates the state tree, which tells the connected component to rerender, and at that point this.props
should show you the old state of the component. reducer更新状态树,该状态树告诉所连接的组件重新渲染,这时此this.props
应该显示组件的旧状态。
After the console.log, the component will continue to rerender unless directed otherwise, which is why when you navigate back you see the 'new props'. 在console.log之后,除非另有说明,否则该组件将继续重新渲染,这就是为什么当您向后导航时会看到“新道具”的原因。
The values that get passed through componentWillReceiveProps are the next props. 通过componentWillReceiveProps传递的值是下一个道具。
Your lifecycle code is essentially saying: 您的生命周期代码本质上是在说:
The doc's on the lifecycle are pretty excellent and can be found here 生命周期中的文档非常出色,可以在这里找到
If that doesn't help out, where is the intended behavior of the component misfiring? 如果这样不能解决问题,那么组件的预期行为会在哪里失灵?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.