[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 ...
随机推荐
- css与jquery、图标字体
*)还能这样选择 header #search input[type="text"] *)按钮常用颜色:#008cBA(字母大小写没有区别) *)清除浮动后,text-align没 ...
- 【Henu ACM Round#19 E】 Om Nom and Candies
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 紫书上的原题: 链接 [代码] #include <bits/stdc++.h> #define ll long lon ...
- Spring学习总结(13)——Spring+Log4j+ActiveMQ实现远程记录日志
应用场景 随着项目的逐渐扩大,日志的增加也变得更快.Log4j是常用的日志记录工具,在有些时候,我们可能需要将Log4j的日志发送到专门用于记录日志的远程服务器,特别是对于稍微大一点的应用.这么做的优 ...
- Android笔记---Intent实现Activity跳转
学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...
- wxWidgets笔记_1_linux环境下wxwidgets的安装与配置
linux下wxwidgets环境的安装与配置 一.建立目标文件夹(自己定义) mkdir /opt/SCOTT 二.安装wxWidgets 1.wxWidgets版本号能够选用wxGTK-2.8.1 ...
- rgba
正反两面展示效果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- 基于Verilog语言的可维护性设计技术
[注]本文内容主体部分直接翻译参考文献[1]较多内容,因此本文不用于任何商业目的,也不会发表在任何学术刊物上,仅供实验室内部交流和IC设计爱好者交流之用. “曲意而使人喜,不若直节而使人忌:无善而致人 ...
- android文本排布
首先看一幅图,是简书App的一篇文章的截图,如下: 图1,图2 上面两个图片都是文本的显示,但是由于有多种格式,所以较为复杂,例如其中有普通文本,还有加粗的文本,还有图文混排的显示等等. 一.解析HT ...
- HTTP 与 HTTPS
https就是http和TCP之间有一层SSL层,这一层的实际作用是防止钓鱼和加密. 防止钓鱼通过网站的证书,网站必须有CA证书,证书类似于一个解密的签名. 另外是加密,加密需要一个密钥交换算法,双方 ...
- 最长回文子串 hihocode 1032 hdu 3068
最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...