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.

  1. import React from "react"
  2. import { render } from "react-dom"
  3. import { Observable } from "rxjs"
  4. import rxjsConfig from "recompose/rxjsObservableConfig"
  5. import {
  6. setObservableConfig,
  7. componentFromStream
  8. } from "recompose"
  9.  
  10. setObservableConfig(rxjsConfig)
  11.  
  12. const personById = id =>
  13. `https://swapi.co/api/people/${id}`
  14.  
  15. const Card = props => (
  16. <div>
  17. <h1>{props.name}</h1>
  18. <h2>{props.homeworld}</h2>
  19. </div>
  20. )
  21.  
  22. const loadById = id =>
  23. Observable.ajax(personById(id))
  24. .pluck("response")
  25. .switchMap(
  26. response =>
  27. Observable.ajax(response.homeworld)
  28. .pluck("response")
  29. .startWith({ name: "" }),
  30. (person, homeworld) => ({
  31. ...person,
  32. homeworld: homeworld.name
  33. })
  34. )
  35.  
  36. const CardStream = componentFromStream(props$ =>
  37. props$
  38. .switchMap(props => loadById(props.id))
  39. .map(Card)
  40. )
  41.  
  42. const App = () => (
  43. <div>
  44. <Card name="John" homeworld="Earth" />
  45. <CardStream id={1} />
  46. <CardStream id={12} />
  47. <CardStream id={10} />
  48. <CardStream id={24} />
  49. </div>
  50. )
  51.  
  52. 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. @GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping

    @GetMapping.@PostMapping.@PutMapping.@DeleteMapping.@PatchMapping  @GetMapping是一个组合注解,是@RequestMappi ...

  2. tar 命令man说明

    TAR(1) User Commands TAR(1) NAME tar - manual page for tar 1.26 SYNOPSIS tar [OPTION...] [FILE]... D ...

  3. java 对象 拆箱装箱 编译和反编译的验证

    创建对象 package 创建对象的个数; public class main { public static void main(String[] agrs){ Check c1=new Check ...

  4. Camera Calibration 相机标定:原理简介(三)

    3 绝对圆锥曲线 在进一步了解相机标定前,有必要了解绝对圆锥曲线(Absolute Conic)这一概念. 对于一个3D空间的点x,其投影空间的坐标为:x~=[x1,x2,x3,x4]T.我们定义无穷 ...

  5. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  6. HDU 1532||POJ1273:Drainage Ditches(最大流)

    pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  7. vim visual操作备忘

    visual模式下: jkl选中行 "+y 复制选中行

  8. SQL Server 运行计划操作符具体解释(3)——计算标量(Compute Scalar)

    接上文:SQL Server 运行计划操作符详细解释(2)--串联(Concatenation ) 前言: 前面两篇文章介绍了关于串联(Concatenation)和断言(Assert)操作符,本文介 ...

  9. 支持并发的httpclient(基于tcp连接池以及netty)

    闲来无事,将曾经自己写的一个库放出来吧. . 有的时候会有这样子的需求: (1)serverA通过HTTP协议来訪问serverB (2)serverA可能会并发的像B发送非常多HTTP请求 类似于上 ...

  10. CSDN博客2014年4月24日清理缓存

    亲爱的CSDN博主们.我们将于今天(2014年4月24日)对CSDN博客频道缓存进行清理,假设您登录后发现自己的文章总数.积分.评论数.訪问数出现异常,请不要慌张.您的数据并没有丢失.将会在缓存清理完 ...