博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Recompose] Stream Props to React Children with RxJS
阅读量:6462 次
发布时间:2019-06-23

本文共 1927 字,大约阅读时间需要 6 分钟。

You can decouple the parent stream Component from the mapped React Component by using props.children instead. This process involves mapping the stream to React’s cloneElement with the children then passing the props in 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 }) => (  

{value}

)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 = () => (
)render(
, document.getElementById("root"))

 

Now inside we use:

We want pass child into it:

const App = () => (  
)

 

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)      )  });

 

 

转载地址:http://aahzo.baihongyu.com/

你可能感兴趣的文章
开篇,博客的申请理由
查看>>
Servlet 技术全总结 (已完成,不定期增加内容)
查看>>
[JSOI2008]星球大战starwar BZOJ1015
查看>>
centos 7 部署LDAP服务
查看>>
揭秘马云帝国内幕:马云的野心有多大
查看>>
iOS项目分层
查看>>
一个action读取另一个action里的session
查看>>
IntelliJ IDEA 注册码
查看>>
String字符串的截取
查看>>
DynamoDB Local for Desktop Development
查看>>
用javascript验证哥德巴赫猜想
查看>>
Shell编程-环境变量配置文件
查看>>
[Unity3d]DrawCall优化手记
查看>>
Struts2和Spring MVC的区别
查看>>
理解Javascript参数中的arguments对象
查看>>
p2:千行代码入门python
查看>>
bzoj1106[POI2007]立方体大作战tet*
查看>>
spring boot configuration annotation processor not found in classpath问题解决
查看>>
团队项目测试报告与用户反馈
查看>>
Mysql中文字符串提取datetime
查看>>