简体   繁体   English

反应生命周期事件-子组件继承旧状态道具

[英]React Lifecycle Events - Child Component Inherits Old State Props

Im probably getting something not right with my understanding of states & props in React. 我对React中的状态和道具的理解可能对我来说是不对的。 This is using React, Redux based app. 这是使用基于Redux的React应用。

Scenario: 场景:

I have a global SVG component, which gets the dimensions of the viewport from the app within the componentDidMount method. 我有一个全局SVG组件,该组件从componentDidMount方法中的应用程序获取视口的尺寸。

The default (upon initialisation of the app) props of the SVG in the state are: 缺省值(在该应用程序的初始化)的道具SVGstate是:

dimension : {
 width : 0,
 height : 0
}

The componentDidMount retrieves the width & height values from the DOM and dispatches the values to the state. componentDidMount从DOM中检索widthheight值,并将这些值分派给状态。

Now I have a child component of SVG component, which needs the updated svg width & height values to calculate the default viewbox & update the state again. 现在,我有了SVG组件的子组件,该组件需要更新的svg宽度和高度值来计算默认的视图框并再次更新状态。 This needs to be executed only once upon mounting (important point) , hence the calcuation of the viewbox is in the componentDidMount of the child component. 这需要在安装时(重点)只执行一次,因此的测算viewboxcomponentDidMount子组件的。

Whats Happening: 发生了什么:

However, I guess because of React batch updating the DOM, the props passed to the child component are the default initial width & height values, not the updated state after SVG component's componentDidMount . 但是,我猜是因为React批处理更新了DOM,传递给子组件的属性是默认的初始宽度和高度值,而不是SVG组件的componentDidMount之后的更新状态。

Question: 题:

How can I pass the updated state to componentDidMount of the child component. 如何传递更新的状态componentDidMount子组件的。 Note: 注意:

  • I cannot use componentDidUpdate , which would mean, that everytime the component is updated, it will calculate the default values & update the view again, thereby, over-riding the user position. 我不能使用componentDidUpdate ,这意味着,每次更新组件时,它将计算默认值并再次更新视图,从而覆盖了用户位置。

If you set the state in componentDidMount() than it will re-render the component. 如果在componentDidMount()中设置状态,则它将重新呈现组件。

If you want to use the props , you should work in componentWillReceiveProps(nextProps) , that in the React life-cycle is called each time the component receave props from parent component. 如果要使用道具 ,则应在componentWillReceiveProps(nextProps)中工作,每次组件从父组件接收道具时,都会在React生命周期中调用该组件。

That seems what you're trying to achieve so far. 到目前为止,这似乎是您想要达到的目标。

What happens in your case is that before the componentDidMount of the parent component executes, your child component is rendered and hence it receives the default props. 您遇到的情况是,在执行父组件的componentDidMount之前,将渲染您的子组件,因此它将接收default道具。 So the props that get updated in the componentDidMount of the parent will reflect in your child as the updated props 因此,在父级的componentDidMount中更新的道具将在您的孩子中反映为已更新的道具

You can make use of componentWillReceiveProps function in the child component to set the state based on the updated props. 您可以使用子组件中的componentWillReceiveProps函数根据更新的道具设置状态。 However this will be executed everytime the props get updated from the parent, so what you can do is that pass some prop from the parent which is being set with a particular value in the componentDidMount of parent and later has a diffrent value. 但是,这将在每次从父级更新道具时执行,因此您可以做的是从父级传递一些道具,该道具在父级的componentDidMount中设置为特定值,然后具有不同的值。 Based on this props in componentWillReceiveProp you can set the initial state of the child component. 基于componentWillReceiveProp此道具,您可以设置子组件的初始状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM