简体   繁体   English

子组件reactjs的动态道具

[英]Dynamic props to child component reactjs

I have a parent component passing state as props to a child component. 我有一个父组件将状态作为道具传递给子组件。 An API is called on page load using componentDidMount in the parent component and the initial state of the data passed to the child changes. 在页面加载时,使用父componentDidMount中的componentDidMount调用API,并且传递给子组件的数据的初始状态会更改。 The change is reflected in the parent component but not in the child component. 更改反映在父组件中,而不反映在子组件中。

Here is an excerpt from my code: 这是我的代码的摘录:

loan.js (Parent) loan.js(父母)

<ForecloseBtn id={this.state.lead_id} foreclose={this.state.isForeclosed }/>

ForecloseBtn.js (Child) ForecloseBtn.js(儿童)

import React from 'react';
import { render } from 'react-dom';

class ForecloseBtn extends React.Component {
    constructor(props) {
        super(props);
        console.log(this.props);
        this.state = {
            lead_id: this.props.id,
            isForeclosed: this.props.foreclose,
        };
    }

    render() {

        return (
            ......
        )
    }
};

export default ForecloseBtn;

So here, the states lead_id and isForeclosed are changing in the parent component but not in the child component. 因此,在这里, lead_idisForeclosed状态在父组件中更改,但在子组件中没有更改。 How can I make the state update in the child component as well? 如何在子组件中进行状态更新?

This is why in your child component you only ready the props in the constructor and set its state accordingly; 这就是为什么在子组件中仅准备构造函数中的props并相应地设置其状态的原因。 if they change later on, you are not updating the child component state, so no re-render will be done. 如果以后更改,则您不会更新子组件状态,因此不会进行任何重新渲染。

There are several options to solve the issues, the easiest being not using state in the child component but directly props ; 有多种解决方案,最简单的方法是在子组件中不使用state而是直接使用props in this case as soon as the parent component is updated, the new props will trigger a re-render of the child component. 在这种情况下,一旦父组件更新,新的道具将触发子组件的重新渲染。 This is also the cleanest solution, you will only have one source of truth, namely the props passed down from parent to child component. 这也是最干净的解决方案,您将只有一个事实来源,即从父级组件传递到子级组件的道具。

Alternatively, you can use componentDidUpdate in the child and update its state based on the new props received from the parent: 另外,您可以在子级中使用componentDidUpdate并根据从父级收到的新道具来更新其状态:

componentDidUpdate(prevProps) {
  // Typical usage (don't forget to compare props):
  if (this.props.id !== prevProps.id) {
    this.setState({ lead_id: this.props.id });
  }
  if (this.props.foreclose !== prevProps.foreclose) {
    this.setState({ isForeclosed: this.props.foreclose });
  }
}

You have to use the componentDidUpdate method in your child Component : 您必须在子组件Component中使用componentDidUpdate方法:

componentDidUpdate (prevProps) {
  if(prevProps.id !== this.props.id || prevProps.foreclose !== this.props.foreclose){
    this.setState({
      lead_id: this.props.id,
      isForeclosed: this.props.foreclose,
    })
  }
}

Each time your state or props changed, this method is called, and we reset lead_id and isForeClosed only if the props changed. 每次您的状态或道具更改时,都会调用此方法,并且仅在道具更改时才重置lead_id和isForeClosed。

Two possible solutions here: 这里有两种可能的解决方案:

First 第一

Do you really need to store the state that you're sending down as props from the parent as a state on the child? 您是否真的需要将要从父母那里作为props发送的state存储为孩子的state Why don't you use them as simple props as they are? 您为什么不将它们作为简单的props使用呢? Your child will re-render when they change. 您的孩子在改变时会重新渲染。

Second 第二

If you absolutely must store those as state on the child, you could render the child with a key prop that must change whenever you want your child to remount completely, because you would need a way to get your constructor function to run again. 如果您绝对必须将这些state存储为子项的state ,则可以使用一个关键prop来渲染子项,只要您希望完全重新安装子项,该prop就必须更改,因为您将需要一种使构造函数再次运行的方法。

Something like: 就像是:

<ForecloseBtn 
  id={this.state.lead_id} 
  foreclose={this.state.isForeclosed }
  key={this.state.lead_id}
/>

You should read this, though: 您应该阅读以下内容:

You probably don't need derived state... 您可能不需要派生状态...

class ForecloseBtn extends React.Component {
    constructor(props) {
        super(props);
        console.log(props);
        this.state = {
            lead_id: props.id,
            isForeclosed: props.foreclose,
        };
    }

    render() {

        return (
            ......
        )
    }
};

You don't need this before props in child component constructor 在子组件构造函数中的props之前不需要this

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

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