ReactJS, Calling setState with same parameter

前端 未结 3 1033
旧巷少年郎
旧巷少年郎 2021-01-07 16:45

I have been reading the React docs and came across shouldComponentUpdate(). My understanding is that everytime setState() is called, a re-render of

3条回答
  •  天命终不由人
    2021-01-07 17:19

    The official React documentation states:

    The default behavior is to re-render on every state change...

    https://reactjs.org/docs/react-component.html#shouldcomponentupdate

    This means that by default, render() will be executed if any of a component's state or props values changes.

    You can override this default behavior using shouldComponentUpdate(). Here's an example that only updates if a state value changes.

    shouldComponentUpdate(nextProps, nextState) {
        return this.state.someValue !== nextState.someValue;
    }
    

    Note: this example completely ignores props. So, any changes to props will not trigger render().

提交回复
热议问题