简体   繁体   English

ReactJs子组件未通过道具更新

[英]ReactJs Child Component is not getting updated through Props

Goal: Re-render only Child component , by sending props from parent's non-state variable. 目标:通过从父级的非状态变量发送道具,仅重新渲染子级组件。

I want to re-render only child component without parent render . 我只想重新渲染子组件,而无需父渲染。 Below is example code I wrote . 以下是我编写的示例代码。 cRoot is parent passing props and CButton Child component needs to be rendered upon change of Visibility variable. cRoot是传递父项的道具,并且需要在更改Visibility变量时呈现CButton Child组件。

  • In child component , I haven't used state because it onetime and I don't want to keep that value in state . 在子组件中,我并没有使用状态,因为它曾经一次,而且我不想将该状态保持在状态中。 Also Child I don't want to create as Pure component . 还有孩子,我不想创建为Pure组件。
  • In parent , I want to use variable (submitBtnVisibility)to send as props . 在父级中,我想使用变量(submitBtnVisibility)作为props发送。

Can you please explain why child component is not getting updated as for child component point of view props are getting updated ? 您能否解释一下为什么子组件未更新,因为子组件的观点道具正在更新? or point to me where I went wrong ? 还是指着我哪里出问题了?

Thanks a lot in advance for helping . 预先非常感谢您的帮助。

// Parent Component 

class Root extends React.Component {
  constructor(props) {
    super(props);
    this.state = { refresh: true };
    // Parents non-state variable.
    this.submitBtnVisibility = { visibility1: 1, visibility2: 2 };

  }

  componentDidMount() {
    console.log("Root componentDidMount ");
  }

  componentDidUpdate(prevProps, prevState) {
    console.log("Root componentDidUpdate ", prevProps, prevState);
  }

  handleonclick = () => {
    console.log(" Root Btn Clicked");
    //this.setState({ var1: 5 });  -> If I enable this line , child is getting rendered
    this.submitBtnVisibility.visibility1 = 5;
  };
  render() {
    console.log(" Root Render ");

    return (
      <div className={classes.uploadContainerArea}>
        <Btncomponent visibility={this.submitBtnVisibility} />
        <button className={classes.btnroot} onClick={this.handleonclick}>
          {" "}
          Root Btn{" "}
        </button>
      </div>
    );
  }
}

// Child Component 

class Btncomponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { Btnprops: this.props.visibility };
  }

  componentDidMount() {
    console.log("Btncomponent componentDidMount ");
  }

  componentDidUpdate(prevProps, prevState) {
    console.log("Btncomponent componentDidUpdate ", prevProps, prevState);
  }

  static getDerivedStateFromProps(props, state) {
    console.log(" getDerivedStateFromProps ");
    return null;
  }
  shouldComponentUpdate(nextProps, nextState) {
    console.log(" shouldComponentUpdate ");
    return true;
  }
  getSnapshotBeforeUpdate(prevProps, prevState) {
    console.log(" getSnapshotBeforeUpdate ");
    return null;
  }
  render() {
    console.log(" Btncomponent Render ", this.props);

    return <button type="button"> {this.props.visibility.visibility1} </button>;
  }
}

Think about it, how can your Child component re-render? 考虑一下,您的Child组件如何重新渲染? If its props or state changes. 如果其道具或状态发生变化。 OK, now think about that how your parent component pass its changed prop to your child component? 好的,现在考虑一下您的父组件如何将其更改后的prop传递给子组件? When it rerenders of course. 当它当然会退货。

How does parent component rerender? 父组件如何重新呈现? When its state changes (or props but for the parent, there is not any prop for now) of course. 当然,当它的状态改变时(或者是道具,但对于父项,现在没有任何道具)。 So, you are not changing parent's state and it does not re-render. 因此,您不会更改父级的状态,并且不会重新渲染。 So, your child does not re-render. 因此,您的孩子不会重新渲染。 Simple as it is. 就这么简单。

You can't render a child component unless it gets new props above parent. 您不能渲染子组件,除非它在父组件之上获得了新的道具。 The parent does not re-render unless its state (or props) changes. 除非其状态(或道具)发生变化,否则父级不会重新渲染。 Here, you are not doing any of this. 在这里,您不执行任何操作。

Update after comments 评论后更新

You can't render a child unless you render the parent component. 除非渲染父组件,否则无法渲染子组件。 No change in parent then no rerender for the child. 父母没有变化,孩子就没有重新投降。

Do not afraid of rendering so much. 不要害怕渲染太多。 Rendering itself is not so expensive, DOM changes are. 渲染本身并不那么昂贵,而DOM更改却很昂贵。 React compares the DOM's (virtual one with the real one) and if nothing changed it does not unmount/remount the component. React将DOM(虚拟的和真实的)进行比较,如果没有任何变化,它不会卸载/重新安装组件。

Also, I don't know why you don't want to use PureComponent but it provides the functionality of what you want. 另外,我不知道您为什么不想使用PureComponent但是它提供了您想要的功能。 Other than that you can use shouldComponentUpdate maybe, but most people do not suggest using it since it has other disadvantages and if not used properly it decreases the performance instead of decreasing it. 除了可以使用shouldComponentUpdate之外,还可以使用shouldComponentUpdate ,但是大多数人不建议使用它,因为它还有其他缺点,如果使用不当,则会降低性能而不是降低性能。

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

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