react-bits:Using Pure Components

前端之家收集整理的这篇文章主要介绍了react-bits:Using Pure Components前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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}/>
  }
}
})
原文链接:https://www.f2er.com/react/304193.html

猜你在找的React相关文章