If the props for a child component are unchanged, does React still re-render it?

百般思念 提交于 2019-12-03 13:14:30

When React re-renders ParentComponent it will automatically re-render ChildComponent. The only way to get around is to implement shouldComponentUpdate in the ChildComponent. You should compare this.props.a, this.props.b and this.props.c and ChildComponents own state to decide to re-render or not. If you are using immutable data than you can just compare previous and next state and props using strict equality ===.

There are a few thing to note with your code

  1. You dont need to forceUpdate when you setState. React automatically does it for you.
  2. You probably meant:

    <ChildComponent a={this.props.a} b={this.props.b} c={this.props.c}/>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!