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

后端 未结 1 1517
甜味超标
甜味超标 2021-02-14 08:41

Suppose I have the following pairing of parent and child components in React:

var ChildComponent = React.createClass({
    getDefaultProps: function(){
        r         


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 09:31

    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:

    0 讨论(0)
提交回复
热议问题