Pure Components默认在shouldComponentUpdate进行浅对比,避免props和state未改变时的重复渲染
Recompose提供了高阶函数pure用于生成Pure Components
bad
export default (props,context) => {
// ... do expensive compute on props ...
return <SomeComponent {...props} />
}
good
import { pure } from 'recompose';
// This won't be called when the props DONT change
export default pure((props,context) => {
// ... do expensive compute on props ...
return <SomeComponent someProp={props.someProp}/>
})
better
// This is better mainly because it uses no external dependencies.
import { PureComponent } from 'react';
export default class Example extends PureComponent {
// This won't re-render when the props DONT change
render() {
// ... do expensive compute on props ...
return <SomeComponent someProp={props.someProp}/>
}
}
})