How to prevent react from re-rendering the whole component?

后端 未结 1 554
野的像风
野的像风 2021-01-07 17:37

I adapted the following component definition from here as shown below. However, unlike the example my component re-renders every time I move the mouse on it.

The re

相关标签:
1条回答
  • 2021-01-07 18:02

    Pure components defined as function will always re-render.

    Convert the component to a class and prevent the re-render in shouldComponentUpdate() returning false.

    The signature is shouldComponentUpdate(nextProps, nextState). Say, you prevent re-render by verifying that componet's params haven't changed:

    shouldComponentUpdate(nextProps, nextState){
       return !equals(nextProps, this.props); // equals() is your implementation
    }
    
    0 讨论(0)
提交回复
热议问题