monsterooo / blog

and make promises by the hours
MIT License
14 stars 1 forks source link

recompose onlyUpdateForKeys #24

Open monsterooo opened 6 years ago

monsterooo commented 6 years ago

onlyUpdateForKeys 介绍

onlyUpdateForKeys接收一个数组字符串,只有当指定的props被修改时才更新。这个api不像shouldUpdate不用自己写比较逻辑,它的内部会自动使用shouldUpdateshallowEqual进行包装。

onlyUpdateForKeys Flow Type

onlyUpdateForKeys(
  propKeys: Array<string>
): HigherOrderComponent

onlyUpdateForKeys 实例

const { compose, pure, onlyUpdateForKeys } = Recompose;

// 只有当 title props 被更改时才会更新组件(render)
const Foo = compose(
  pure,
  onlyUpdateForKeys(['title']) ,
)(({ title }) => (
  <div>{console.log('render')}{title}</div>
))

在线DEMO

codepen在线预览