PureComponent

陷阱

我们建议使用函数式组件而非类式组件。查看如何迁移

PureComponent 类似于 Component,但是当 props 和 state 与之前保持一致时会跳过重新渲染。React 仍然支持类式组件,但我们不建议在新代码中使用。

class Greeting extends PureComponent {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}

参考

PureComponent

为了在 props 和 state 相同时跳过重新渲染,类式组件应该继承 PureComponent 而不是 Component

import { PureComponent } from 'react';