分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

[React] Create a queue of Ajax requests with redux-observable and group the results.

发布时间:2023-09-06 01:17责任编辑:胡小海关键词:AjaxReact

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

// actionexport 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 ?}}
// epicsimport {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;
// componentimport 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.

原文地址:http://www.cnblogs.com/Answer1215/p/7675215.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved