monsterooo / blog

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

recompose branch #19

Open monsterooo opened 6 years ago

monsterooo commented 6 years ago

branch 介绍

branch方法接收三个函数参数,第一个函数返回一个布尔值如果为true则调用branch第二个函数,如果为false则调用branch第三个函数。第二个和第三个为高阶函数,在函数里面可以为组件增强功能。

branch Flow Type

branch(
  test: (props: Object) => boolean,
  left: HigherOrderComponent,
  right: ?HigherOrderComponent
): HigherOrderComponent

branch 实例

const { compose, branch, withProps } = Recompose;

const Foo = compose(
  branch(
    props => props.isShow,
    withProps({ title: '我被显示了' }),
    withProps({ title: '你看不到我' }),
  ),
)(({ title }) => (
  <div>{ title }</div>
))

在线DEMO

codepen在线预览