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 }) => (
<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] Stream Props to React Children with RxJS的更多相关文章

  1. [Recompose] Configure Recompose to Build React Components from RxJS Streams

    Recompose provides helper functions to stream props using an Observable library of your choice into ...

  2. React中props.children和React.Children的区别

    在React中,当涉及组件嵌套,在父组件中使用props.children把所有子组件显示出来.如下: function ParentComponent(props){ return ( <di ...

  3. 对React children 的深入理解

    React的核心为组件.你可以像嵌套HTML标签一样嵌套使用这些组件,这使得编写JSX更加容易因为它类似于标记语言. 当我刚开始学习React时,当时我认为“使用 props.children 就这么 ...

  4. React.Children详解

    React.Children提供了处理this.props.children的工具,this.props.children可以任何数据(组件.字符串.函数等等).React.children有5个方法 ...

  5. react children技巧总结

    在使用该技巧时,建议先看一下相关的知识,点我查看 假如使用该属性时,想把父组件的所有属性及部分方法传递给子组件,该怎么办呢?看代码 const Child = ({ doSomething, valu ...

  6. [React] Compound Component (React.Children.map & React.cloneElement)

    Imaging you are building a Tabs component. If looks like: <Tabs> <TabList> <Tab> o ...

  7. [React] Understand React.Children Utilities

    The data contained in this.props.children is not always what you might expect. React provides React. ...

  8. React源码 React.Children

    children是什么意思呢?就是我们拿到组件内部的props的时候,有props.children这么一个属性,大部分情况下,我们直接把 props.children 渲染到 JSX 里面就可以了. ...

  9. React Children 使用

    React 有一个特殊的属性children, 主要用于组件需要渲染内容,但它并不知道具体要渲染什么内容,怎么会有这种使用场景?确实比较少,但并不是没有,比如弹出框.当你写一个弹出框组件的时候,你知道 ...

随机推荐

  1. [NOIP2011提高组]Mayan游戏

    题目:洛谷P1312.Vijos P1738.codevs1136. 题目大意:在一个7行5列的棋盘(左下角坐标0,0)上,有一些不同颜色的棋子. 规定某一时刻,连续三个横排或竖列的棋子颜色相同,则它 ...

  2. 3步简单实现SSH无密码登录

    下面简单的做下实验: 一:准备2台服务器: 192.168.10.101 2 192.168.10.102 二:关键字解释 ssh-keygen :  产生公钥与私钥对 ssh-copy-id :将本 ...

  3. CAS-ERR Cannot create a session after the response has been committed

    现象: 当cas 登录人数较少时候没有错误,但是用户过多时候出现下列err May-2016 18:09:11.932 SEVERE [http-nio-8080-exec-52] org.apach ...

  4. ios xcode真机调试获取屏幕截屏

    非常多时候我们须要在调试的过程中把手机屏幕截图发给其它人看,在android开发中我们能够非常方便的截图保存.而xcode开发ios的时候发现这个需求却如此困难.网上大部分都是介绍的以下这个方案.可是 ...

  5. POJ1502 MPI Maelstrom Dijkstra

    题意 给出图,从点1出发,求到最后一个点的时间. 思路 单源最短路,没什么好说的.注意读入的时候的技巧. 代码 #include <cstdio> #include <cstring ...

  6. Android Application 类共享全局数据

    android系统会为每一个程序执行时创建一个Application类的对象且仅创建一个.所以Application能够说是单例模式的一个类.且application对象的生命周期是整个程序中最长的, ...

  7. JNI 资源释放

    JNI 编程实现了 native code 和 Java 程序的交互,因此 JNI 代码编程既遵循 native code 编程语言的编程规则,同时也遵守 JNI 编程的文档规范.在内存管理方面,na ...

  8. nyoj--1233--差值(贪心模拟+大数)

    差值 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 输入一个整数数组,将它们连接起来排成一个数,找出能排出的所有数字中最大,最小的两个,输出两个数的差值.例如输入数组{ ...

  9. Traversal with a for loop

    A lot of computations involve processing a string one character at a time. Often they start at the b ...

  10. 4.STL六大组件

    代码示例 #include <vector> #include <list> #include <iostream> #include <algorithm& ...