React.PureComponent — это специальный тип компонента в React, который оптимизирует рендеринг за счет автоматического выполнения поверхностного сравнения пропсов и состояния. В отличие от обычного
React.Component, он реализует метод shouldComponentUpdate с поверхностным сравнением пропсов и состояния. Пример использования:
PureComponent.
class PureComp extends React.PureComponent {
render() {
console.log('PureComponent render');
return <div>{this.props.value}</div>;
}
}
Родительский компонент.
class ParentComponent extends React.Component {
state = {
value: 0
};
componentDidMount() {
setInterval(() => {
this.setState({ value: this.state.value + 1 });
}, 1000);
}
render() {
return (
<div>
<RegularComponent value={this.state.value} />
<PureComp value={this.state.value} />
</div>
);
}
}
👉 @sWebDev
