简体   繁体   English

Redux-减速器已成功更新,但连接的组件的componentWillReceiveProps仍显示旧状态

[英]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: 您的生命周期代码本质上是在说:

  1. Tell me my old props (prior to the connector making me rerender) 告诉我我的旧道具(在连接器使我重新渲染之前)
  2. If there wasn't a loaded script, and now, after the reducer, there is: a. 如果没有已加载的脚本,现在在化简器之后,有: If the script load succeeded, reset map options and reinitialize b. 如果脚本加载成功,请重置映射选项并重新初始化b。 If the script load failed, log an error to stdout 如果脚本加载失败,则将错误记录到stdout

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.

相关问题 React 组件没有得到由 reducer 设置的 redux 的初始 state - React component not getting redux's initial state set by reducer 在redux中更新了状态,以接收新道具,但是由于未重新渲染组件而未在componentWillReceiveProps中设置状态 - State updated in redux receiving new props but state is not setting in componentWillReceiveProps due to which component doesn't re-render Redux状态已更新,但未更新组件 - Redux state is updated but not the component Redux开发人员工具状态图显示减速器正在更新另一个减速器 - Redux dev tool state chart shows that reducer is updating another reducer Redux存储正在更新,但是由setInterval调用的连接函数仍然看到旧状态 - Redux store is updating but connected function that is called by setInterval still sees the old state 状态更改后,React-Redux连接的组件不会更新 - React-Redux connected component doesn't get updated after state is changed 了解Redux的Reducer存储状态 - Understanding Redux's Reducer storing State 在Redux Reducer中读取Store的初始状态 - Read Store's Initial State in Redux Reducer Redux状态已更新,但仍收到未定义的道具 - Redux state updated but props still received as undefined 在 Redux Reducer 或组件的 ShouldComponentUpdate 中进行深度比较? - Deep Compare in Redux Reducer or in Component's ShouldComponentUpdate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM