首页 > 移动平台 > 详细

[Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream

时间:2017-12-22 20:16:22      阅读:173      评论:0      收藏:0      [点我收藏+]

Rather than using Components to push streams into other Components, mapPropsStream allows you to create functions that can wrap components to create shareable behaviors across components.

 

Using componentFromStream:

import React from "react"
import { render } from "react-dom"
import { Observable } from "rxjs"
import config from "recompose/rxjsObservableConfig"
import {
  setObservableConfig,
  componentFromStream,
  createEventHandler,
  mapPropsStream
} from "recompose"

setObservableConfig(config)
//#endregion

const Counter = props => <h1>{props.count}</h1>

const CounterWithInterval = componentFromStream(
  props$ => props$.switchMap(
    props => Observable.interval(1000),
    (props, count) => ({count, ...props})
  )
  .map(Counter)
)


const App = () => (
  <div>
    <CounterWithInterval />
  </div>
)

render(<App />, document.getElementById("root"))

 

MapPropsStream allows you to create functions that will decorate your component, rather than creating a component itself. I‘m going to create an interval here using MapPropsStream.

A mapProposStream version can be like this:

import React from "react"
import { render } from "react-dom"
import { Observable } from "rxjs"
import config from "recompose/rxjsObservableConfig"
import {
  setObservableConfig,
  componentFromStream,
  createEventHandler,
  mapPropsStream
} from "recompose"

setObservableConfig(config)
//#endregion

const Counter = props => <h1>{props.count}</h1>

const interval = mapPropsStream(props$ => props$.switchMap(
  props => Observable.interval(1000),
  (props, count) => ({ count, ...props })
))

const CounterWithInterval = interval(Counter)


const App = () => (
  <div>
    <CounterWithInterval />
  </div>
)

render(<App />, document.getElementById("root"))

 

As you can see, we take the hightlighted part as own function, wrapped with ‘mapPropsStream‘. 

[Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream

原文:http://www.cnblogs.com/Answer1215/p/8087464.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!