[Recompose] Stream Props to React Children with RxJS
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的更多相关文章
- [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 ...
- React中props.children和React.Children的区别
在React中,当涉及组件嵌套,在父组件中使用props.children把所有子组件显示出来.如下: function ParentComponent(props){ return ( <di ...
- 对React children 的深入理解
React的核心为组件.你可以像嵌套HTML标签一样嵌套使用这些组件,这使得编写JSX更加容易因为它类似于标记语言. 当我刚开始学习React时,当时我认为“使用 props.children 就这么 ...
- React.Children详解
React.Children提供了处理this.props.children的工具,this.props.children可以任何数据(组件.字符串.函数等等).React.children有5个方法 ...
- react children技巧总结
在使用该技巧时,建议先看一下相关的知识,点我查看 假如使用该属性时,想把父组件的所有属性及部分方法传递给子组件,该怎么办呢?看代码 const Child = ({ doSomething, valu ...
- [React] Compound Component (React.Children.map & React.cloneElement)
Imaging you are building a Tabs component. If looks like: <Tabs> <TabList> <Tab> o ...
- [React] Understand React.Children Utilities
The data contained in this.props.children is not always what you might expect. React provides React. ...
- React源码 React.Children
children是什么意思呢?就是我们拿到组件内部的props的时候,有props.children这么一个属性,大部分情况下,我们直接把 props.children 渲染到 JSX 里面就可以了. ...
- React Children 使用
React 有一个特殊的属性children, 主要用于组件需要渲染内容,但它并不知道具体要渲染什么内容,怎么会有这种使用场景?确实比较少,但并不是没有,比如弹出框.当你写一个弹出框组件的时候,你知道 ...
随机推荐
- servlet关于转发用法
# 1.转发 ## (1)什么是转发? 一个web组件将未完成的处理交给另外一个web组件继续做. 注: web组件(servlet/jsp) 最常见的情况: ...
- 织梦DEDECMS系统中文章内容为空 用SQL语句如何删除?
织梦后台里提供了清空内容为空的文章,可是发现并不好用,有些空文章还是删除不了,而有些文章不是空的,只是采到了几个字,这些无法清除,于是就手动来清除这个文章.开始是一个一个文章找,一个一个来删除,后来觉 ...
- hdu5321 beautiful set(莫比乌斯反演)
设\(cnt[i]\)为权值为i的倍数的数的数量. \(f0[i],f1[i]\)分别为两种方法\(gcd=i\)的贡献是i的多少倍. \(F0[i],F1[i]\)分别为两种方法\(gcd\)为\( ...
- Django模版系统
一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 一.模板语法之变量:语法为 {{ }}: 在 Django 模板中遍历复杂数据结构的关键 ...
- 【转载·】Linux yum 安装 gcc 、gcc-c++
2017年09月29日 22:45:54 上善若水 阅读数:6653更多 个人分类: Linux学习 所属专栏: Linux学习杂技 版权声明:本文为博主原创文章,未经博主允许不得转载. ht ...
- 紫书 例题8-19 UVa 12265 (扫描法+单调栈)
首先可以用扫描法处理出一个height数组, 来保存从当前行开始, 每一个格子可以向上延伸的最大长度. 这种"延伸"的问题用扫描法, 因为往往这个时候可以利用前一次的结果来更新当前 ...
- HTTP——学习笔记(6)https
HTTP+加密+认证+完整性保护=HTTPS HTTP是一种新协议吗?: 不是,HTTPS只是HTTP通信接口部分用SSL和TLS协议代替而已 HTTP中,身处应用层的HTTP直接和TCP通信.而在使 ...
- C语言程序设计:现代方法阅读笔记
第二十六章 atexit函数允许用户“注册”在程序终止时要调用的函数:atexit(func); 在程序终止后,func函数会被自动调用 clock()函数可以计算程序运行时间 time函数返回当前的 ...
- 【codeforces 340B】Maximal Area Quadrilateral
[题目链接]:http://codeforces.com/problemset/problem/340/B [题意] 给你n个点,让你在这里面找4个点构成一个四边形; 求出最大四边形的面积; [题解] ...
- 你的Android应用完全不需要那么多的权限
Android系统的权限从用户的角度来看有时候的确有点让人摸不着头脑.有时候可能你只需要做一些简单的事情(对联系人的信息进行编辑),却申请了远超你应用所需的权限(比如访问所有联系人信息的权限). 这很 ...