[Recompose] Stream Props to React Children with RxJS
2017-12-22 19:27
676 查看
You can decouple the parent stream Component from the mapped React Component by using
We have the code below:
Now inside we use:
We want pass child into it:
So now, instead we map to Counter map in the JXS, we want to clone the child elemenet and pass down new props:
props.childreninstead. This process involves mapping the stream to React’s
cloneElementwith the
childrenthen passing the
propsin manually.
We have the code below:
import React from "react" import { render } from "react-dom" import { Observable } from "rxjs" import config from "recompose/rxjsObservableConfig" import { setObservableConfig, componentFromStream, createEventHandler } from "recompose" setObservableConfig(config) const Counter = ({ value, onInc, onDec }) => ( <div> <button onClick={onInc}>+</button> <h2>{value}</h2> <button onClick={onDec}>-</button> </div> ) const CounterStream = componentFromStream( props$ => { const { stream: onInc$, handler: onInc } = createEventHandler(); const { stream: onDec$, handler: onDec } = createEventHandler(); return props$ .switchMap( propos => Observable.merge( onInc$.mapTo(1), onDec$.mapTo(-1) ) .startWith(propos.value) .scan((acc, curr) => acc + curr) .map((value) => ({ value, onInc, onDec }))) .map( Counter ) } ); const App = () => ( <div> <CounterStream value={3} /> </div> ) render(<App />, document.getElementById("root"))
Now inside we use:
<CounterStream value={4} />
We want pass child into it:
const App = () => ( <div> <CounterStream value={3}> <Counter /> </CounterStream> </div> )
So now, instead we map to Counter map in the JXS, we want to clone the child elemenet and pass down new props:
const CounterStream = componentFromStream( props$ => { const { stream: onInc$, handler: onInc } = createEventHandler(); const { stream: onDec$, handler: onDec } = createEventHandler(); return props$ .switchMap( props => Observable.merge( onInc$.mapTo(1), onDec$.mapTo(-1) ) .startWith(props.value) .scan((acc, curr) => acc + curr) .map((value) => ({ ...props, value, onInc, onDec })) ).map(props => cloneElement(props.children, props) ) } );
相关文章推荐
- [Recompose] Refactor React Render Props to Streaming Props with RxJS and Recompose
- [Recompose] Pass a React Prop to a Stream in RxJS
- [Recompose] Stream a React Component from an Ajax Request with RxJS
- [Recompose] Compose Streams of React Props with Recompose’s compose and RxJS
- [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream
- [Recompose] Configure Recompose to Build React Components from RxJS Streams
- [RxJS] Starting a Stream with SwitchMap & switchMapTo
- [Recompose] Handle React Events as Streams with RxJS and Recompose
- [RxJS] Resubscribing to a Stream with Repeat
- [Recompose] Merge RxJS Button Event Streams to Build a React Counter Component
- [RxJS] Handling a Complete Stream with Reduce
- [RxJS] Logging a Stream with do()
- [RxJS] Refactoring CombineLatest to WithLatestFrom
- [RxJS] Toggle A Stream On And Off With RxJS
- [Recompose] Make Reusable React Props Streams with Lenses
- [RxJS] Stopping a Stream with TakeUntil
- React.js--this.props.children
- [RxJS] Stream Processing With RxJS vs Array Higher-Order Functions
- [RxJS] Completing a Stream with TakeWhile
- [React] Use React.cloneElement to Extend Functionality of Children Components