Loading data using RxJS is simple using Observable.ajax. This lesson shows you how to take the ajax response and pass it along the stream to use as props in a React Component.

import React from "react"
import { render } from "react-dom"
import { Observable } from "rxjs"
import rxjsConfig from "recompose/rxjsObservableConfig"
import {
setObservableConfig,
componentFromStream
} from "recompose" setObservableConfig(rxjsConfig) const personById = id =>
`https://swapi.co/api/people/${id}` const Card = props => (
<div>
<h1>{props.name}</h1>
<h2>{props.homeworld}</h2>
</div>
) const loadById = id =>
Observable.ajax(personById(id))
.pluck("response")
.switchMap(
response =>
Observable.ajax(response.homeworld)
.pluck("response")
.startWith({ name: "" }),
(person, homeworld) => ({
...person,
homeworld: homeworld.name
})
) const CardStream = componentFromStream(props$ =>
props$
.switchMap(props => loadById(props.id))
.map(Card)
) const App = () => (
<div>
<Card name="John" homeworld="Earth" />
<CardStream id={1} />
<CardStream id={12} />
<CardStream id={10} />
<CardStream id={24} />
</div>
) render(<App />, document.getElementById("root"))

SwitchMap has second param which is a selector function, take two params, first is outter observable, in the example refers to Person response, second is inner observable, in the example refer to homeworld response. Then combine to one single response.

[Recompose] Stream a React Component from an Ajax Request with RxJS的更多相关文章

  1. [Recompose] Stream Props to React Children with RxJS

    You can decouple the parent stream Component from the mapped React Component by using props.children ...

  2. 学习React系列(一)——React.Component 生命周期

    挂载中(只执行一次) 以下方法在组件实例正被创建和插入到DOM中时调用 constructor()一般用于初始化state和方法的this绑定 componentWillMount() render( ...

  3. [Recompose] Make Reusable React Props Streams with Lenses

    If you hard-code a stream of props to target a specific prop, it becomes impossible to reuse that st ...

  4. React.js Tutorial: React Component Lifecycle

    Introduction about React component lifecycle. 1 Lifecycle A React component in browser can be any of ...

  5. 使用 jest 测试 react component 的配置,踩坑。

    首先安装依赖 npm i jest -g npm i jest babel-jest identity-obj-proxy enzyme enzyme-adapter-react-15.4 react ...

  6. 转载 React.createClass 对决 extends React.Component

    先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...

  7. storm报错:Exception in thread "main" java.lang.RuntimeException: InvalidTopologyException(msg:Component: [mybolt] subscribes from non-existent stream: [default] of component [kafka_spout])

    问题描述: storm版本:1.2.2,kafka版本:2.11.   在使用storm去消费kafka中的数据时,发生了如下错误. [root@node01 jars]# /opt/storm-1. ...

  8. 002-and design-dva.js 知识导图-01JavaScript 语言,React Component

    一.概述 参看:https://github.com/dvajs/dva-knowledgemap react 或 dva 时会不会有这样的疑惑: es6 特性那么多,我需要全部学会吗? react ...

  9. React.Component 与 React.PureComponent(React之性能优化)

    前言 先说说 shouldComponentUpdate 提起React.PureComponent,我们还要从一个生命周期函数 shouldComponentUpdate 说起,从函数名字我们就能看 ...

随机推荐

  1. 2,HTTP请求应答返回码

    200 OK 请求成功,一般用于Get和Post请求 300 多种选择.请求的资源可包括多个位置,响应的返回一个资源特征与地址的列表用于浏览器(client)选择 Multiple Choices 3 ...

  2. SpringBoot学习笔记(5)----SpringBoot中异常处理的三种方法

    对于异常的处理,Spring Boot中提供默认的一个异常处理界面,如下图: 但是在实际的运用开发中,这样的页面显然是不友好的,Spring Boot也提供了自定义异常处理的方式,如下总结三种一场处理 ...

  3. sass的用法小结(一)

    1. 使用变量; sass让人们受益的一个重要特性就是它为css引入了变量.你可以把反复使用的css属性值 定义成变量,然后通过变量名来引用它们,而无需重复书写这一属性值.或者,对于仅使用过一 次的属 ...

  4. Vim配置及使用

    Vim配置 1.打开~/.vimrc,将以下内容考入文件.vimrc中 "行号" set nu "高亮" syntax enable syntax on &qu ...

  5. win系统安装node出现这个2503和2502解决办法

    一: 今天在公司的新电脑要安装appium,所以要搭建appium的环境,所以在安装到node的时候,出现了内部错误2503和2502,安装中断. 这种错误可能是权限不足导致,一般“.exe”程序可以 ...

  6. java实现文件的上传与下载

    (一)文件的上传:在这一部分,我要将execl文件的内容上传到数据库中,完成这一功能.需要分为两步: 1:将文件上传到tomcat下 文件格式如下: 2:读取execl表中的内容到数据库中 首先:下载 ...

  7. Windows下从源代码编译Skia

    在PPAPI里面画图,能够结合第三方的图形库.比方Cairo.Skia. Google Chrome.Chromium和Android都使用Skia作为画图引擎.我也来试试Skia,先过编译关. fo ...

  8. elasticsearch中的几个概念总结

    1.Geo spatial search : 地理空间搜索,可以在搜索查询中指定的某一距离内查找所要的内容.也可以返回以当前为圆心,逐渐添加圆的半径.直到找到所匹配到的内容. 參考:http://ww ...

  9. springMVC之拦截器

    有两种方法配置spring的拦截器 1. 实现接口: HandleInterceptor public class MyInterceptor1 implements HandlerIntercept ...

  10. struts2标签#、%、$取值

    转自:https://blog.csdn.net/kosum/article/details/21375635 首先了解下OGNL的概念: OGNL是Object-Graph Navigation L ...