[React] Create a queue of Ajax requests with redux-observable and group the results.
With redux-observable, we have the power of RxJS at our disposal - this means tasks that would otherwise be complicated and imperative, become simple and declarative. In this lesson we’ll respond to an action by queuing up 2 separate Ajax requests that will execute sequentially. Then we’ll group the results from both into an array and produce a single action from our epic that will save the data into the redux store
// action export const FETCH_STORIES = 'FETCH_STORIES';
export const FETCH_STORIES_FULFILLED = 'FETCH_STORIES_FULFILLED'; export function fetchStoriesAction(count = 5) {
return {
type: FETCH_STORIES,
payload: count
}
} export function fetchStoriesFulfilledAction(stories) {
return {
type: FETCH_STORIES_FULFILLED,
payload: stories
}
}
// epics
import {Observable} from 'rxjs';
import {combineEpics} from 'redux-observable';
import {FETCH_STORIES, fetchStoriesFulfilledAction} from "../actions/index";
const topStories = `https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty`;
const url = (id) => `https://hacker-news.firebaseio.com/v0/item/${id}.json?print=pretty`;
function fetchStoriesEpic(action$) {
  return action$.ofType(FETCH_STORIES)
    .switchMap(({payload}) => {
      return Observable.ajax.getJSON(topStories)
        // slice first 5 ids
        .map(ids => ids.slice(0, 5))
        // convert ids -> urls
        .map(ids => ids.map(url))
        // convert urls -> ajax
        .map(urls => urls.map(url => Observable.ajax.getJSON(url)))
        // execute 5 ajax requests
        .mergeMap(reqs => Observable.forkJoin(reqs))
        // results -> store
        .map(stories => fetchStoriesFulfilledAction(stories))
    })
}
export const rootEpic = combineEpics(fetchStoriesEpic);
import {FETCH_STORIES, FETCH_STORIES_FULFILLED} from "../actions/index";
const initialState = {
  stories: [],
  loading: false,
};
export function storiesReducer(state = initialState, action) {
  switch(action.type) {
    case FETCH_STORIES:
      return {
        stories: [],
        loading: true
      };
    case FETCH_STORIES_FULFILLED:
      return {
        stories: action.payload,
        loading: false
      };
    default: return state;
  }
}
export default storiesReducer;
// component import React from 'react';
import {connect} from "react-redux";
import {fetchStoriesAction} from "../actions/index"; export function Stories(props) {
if (props.loading) {
return (
<p>Please wait...</p>
)
}
return (
<div>
<button type="button" onClick={props.loadStories}>Load top 5 stories</button>
<StoryList stories={props.stories} />
</div>
)
} function StoryList(props) {
return (
<ul>
{props.stories.map(story =>
<li key={story.id}>
<a href={story.url}>{story.title}</a>
</li>
)}
</ul>
);
} function mapState(state) {
return state;
} function mapDispatch(dispatch) {
return {
loadStories: () => dispatch(fetchStoriesAction())
}
} export default connect(mapState, mapDispatch)(Stories);
[React] Create a queue of Ajax requests with redux-observable and group the results.的更多相关文章
- 转 Using $.ajaxPrefilter() To Configure AJAX Requests In jQuery 1.5
		Using $.ajaxPrefilter() To Configure AJAX Requests In jQuery 1.5 Posted February 18, 2011 at 6:29 PM ... 
- How to create a site with AJAX enabled in MVC framework.
		How to create a site with AJAX enabled in MVC framework. The Project illustrates how to create a web ... 
- Allow Only Ajax Requests For An Action In ASP.NET Core
		ASP.NET Core offers attributes such as [HttpGet] and [HttpPost] that allow you to restrict the HTTP ... 
- [React] Create and import React components with Markdown using MDXC
		In this lesson I demonstrate how to use the library MDXC to create and import React components with ... 
- [React] Create component variations in React with styled-components and "extend"
		In this lesson, we extend the styles of a base button component to create multiple variations of but ... 
- [React] Create & Deploy a Universal React App using Zeit Next
		In this lesson, we'll use next to create a universal React application with no configuration. We'll ... 
- [React] Create an Animate Content Placeholder for Loading State in React
		We will create animated Content Placeholder as React component just like Facebook has when you load ... 
- [React] Create a Virtualized List with Auto Sizing Cells using react-virtualized and CellMeasurer
		In this lesson we'll use CellMeasurer and CellMeasurerCache to automatically calculate and cache the ... 
- [React] Create an Auto Resizing Virtualized List with react-virtualized
		In this lesson we'll show how to use the AutoSizer component from react-virtualized to automatically ... 
随机推荐
- bzoj1612 Usaco08 Jan 牛大赛
			水题模拟 建一个图,每两个牛进行比赛就连一条边,然后两遍dfs求出比他弱和比他强的牛,最后如果相加数量等于n,说明他能与全部的牛进行比较,排名确定. #include<bits/stdc++.h ... 
- [POI2010]GIL-Guilds(结论题)
			题意 给一张无向图,要求你用黑白灰给点染色,且满足对于任意一个黑点,至少有一个白点和他相邻:对于任意一个白点,至少有一个黑点与他相邻,对于任意一个灰点,至少同时有一个黑点和白点和灰点与他相邻,问能否成 ... 
- HAOI树上染色
			Description : 有一棵点数为 N 的树,树边有边权.给你一个在 0~ N 之内的正整数 K ,你要在这棵树中选择 K个点,将其染成黑色,并将其他 的N-K个点染成白色 . 将所有点染色后, ... 
- subline 快捷键与功能解释
			选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ... 
- 【C#】C#托付和事件的实例解说
			using System; namespace delegate_event { // 定义一个猫类 class Cat { // 定义一个名字字段 private string name; // 创 ... 
- nyoj 1104 just for you
			just for you 时间限制:1000 ms | 内存限制:65535 KB 难度:0 描写叙述 今天tlp和ly想去看电影了到了电影院才发现买票的人特别多 .ly不想让tlp等着急了,就先 ... 
- 七牛用户搭建c# sdk的图文讲解
			Qiniu 七牛问题解答 问题描写叙述:非常多客户属于小白类型. 可是请不要随便喷七牛的文档站.由于须要一点http的专业知识才干了解七牛的api文档.如今我给大家弄个c# sdk的搭建步骤 问题解决 ... 
- smarty课程---smarty的处理过程是怎样的
			smarty课程---smarty的处理过程是怎样的 一.总结 一句话总结:编译文件里时间戳记录模板文件修改时间,如果模板被修改过就可以检测到,然后重新编译 1. smarty将php源文件,首先编译 ... 
- [Chromium文档转载,第002章]Mojo C++ Bindings API
			Mojo C++ Bindings API This document is a subset of the Mojo documentation. Contents Overview Getting ... 
- CentOS7/RedHat7的Apache配置介绍
			这里我们介绍yum安装httpd yum install -y httpd ************* [root@100 ~]# systemctl restart httpd [root@100 ... 
