码迷,mamicode.com
首页 > 其他好文 > 详细

[Recompose] Make Reusable React Props Streams with Lenses

时间:2017-12-28 11:56:53      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:spec   interval   hat   splay   back   name   app   mda   box   

If you hard-code a stream of props to target a specific prop, it becomes impossible to reuse that stream with any other components. Configuring your props stream with lenses will allow you to reuse your stream with any React component.

 

Checkout: lensProp, lensPath.

const personNameLens = R.lensPath([
  "person",
  "name"
])

const typewriter = lens =>
  mapPropsStream(props$ =>
    props$.switchMap(
      props =>
        Observable.zip(
          Observable.from(R.view(lens, props)),
          Observable.interval(100),
          letter => letter
        ).scan((acc, curr) => acc + curr),
      (props, name) => R.set(lens, name, props)
    )
  )

 

const DateDisplay = props => <h1>{props.date}</h1>
const dateLens = R.lensProp("date")
const DateTypewriter = typewriter(dateLens)(
  DateDisplay
)

 

 

[Recompose] Make Reusable React Props Streams with Lenses

标签:spec   interval   hat   splay   back   name   app   mda   box   

原文地址:https://www.cnblogs.com/Answer1215/p/8131542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!