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);

最新文章

  1. Android——WebView
  2. CSS技能汇总,研究及实践
  3. signalR制作微信墙 开源
  4. HDU 2993 MAX Average Problem(斜率优化)
  5. django apache error.log过大
  6. android 实现2张图片层叠效果
  7. Asp.Net 之 母版页中对控件ID的处理
  8. 实现n皇后问题(回溯法)
  9. Swift编程语言的相关资料
  10. 关于sed中的Pattern Space和Hold Space的随笔
  11. bootstrap datetimepicker 时间段选择限制
  12. xcode UIImage图片拉伸
  13. A Byte of Python 笔记(12)python 标准库:sys、os,更多内容
  14. PHP 字符串处理 总结
  15. 使用C#在Windows应用商店程序中获取CPU信息
  16. Android 自定义 View 圆形进度条总结
  17. 使用腾讯云“自定义监控”监控GPU使用率
  18. POSIX标准 库文件
  19. JS中this的四种用法
  20. centos7+cobbler+kickstart

热门文章

  1. HDU-1034 Candy Sharing Game 模拟问题(水题)
  2. php计算两个时间相差的天数、小时数、分钟数、秒数
  3. 现在有一个函数A和函数B,请你实现B继承A
  4. AJAX--XMLHttpRequest五步使使用方法
  5. hdu4691 Front compression(后缀数组)
  6. 在cmd命令行中弹出Windows对话框(使用mshta.exe命令)
  7. 记一次httpclient Connection reset问题定位
  8. Whats the difference between git reset --mixed, --soft, and --hard?
  9. Programming Languages - Coursera 整理
  10. Sqoop 的优势