redux-saga

appstore-react v2.0—redux-actions和redux-saga的应用

放肆的年华 提交于 2019-11-30 06:37:00
开发文档 https://redux-saga.js.org/ https://redux-saga-in-chinese.js.org/ https://redux-actions.js.org/ 源码 代码已经上传到github中,欢迎star或者fork appstore-react-v2.0 redux-saga 一、介绍 之前异步处理用的是redux-thunk + redux-actions + redux-promise,但是随着ES6中Generator的出现,人们发现用Generator处理异步可以更简单。而redux-saga就是用Generator来处理异步。 redux-saga文档并没有说自己是处理异步的工具,而是说用来处理边际效应(side effects),这里的边际效应你可以理解为程序对外部的操作,比如请求后端,比如操作文件。 redux-saga同样是一个redux中间件,它的定位就是通过集中控制action,起到一个类似于MVC中控制器的效果。 同时它的语法使得复杂异步操作不会像promise那样出现很多then的情况,更容易进行各类测试。 二、 安装 npm install --save redux-saga 三、saga常用辅助函数 put、call、takeEvery、takeLatest 1、put和call

How to test API request failures with Redux Saga?

☆樱花仙子☆ 提交于 2019-11-30 02:58:31
I am trying to test every scenarios my saga could follow, but i am not able to make happens the behaviors i want. This is pretty simple, i have a HTTP request (login), and i want to test the success and the failure cases by mocking my API method. But, it looks like the call effect doesn't fire my api function, i don't really get yet how it works, but i guess that the middleware is in charge of invoking the function, and since i don't go though the store on my test, i can't get the result. So my question is, how can you test your saga when you need to dispatch different actions (typically

How to achieve callbacks in Redux-Saga?

我与影子孤独终老i 提交于 2019-11-30 00:38:17
问题 The scenario is, I want to redirect a user or show alert based on the success, error callbacks after dispatching an action. Below is the code using redux-thunk for the task this.props.actions.login(credentials) .then((success)=>redirectToHomePage) .catch((error)=>alertError); because the dispatch action in redux-thunk returns a Promise, It is easy to act with the response. But now I'm getting my hands dirty on redux-saga, and trying to figure out how I can achieve the same result as above

redux-saga实践与思考

蹲街弑〆低调 提交于 2019-11-29 20:03:51
redux-saga 对于redux-saga,网上的文章已经数不胜数,在这里重提redux-saga,主要是想记录一下最近使用redux-saga的一些考虑和看法,本文不会去讲解各个 Effect ,如有需要可查看 文档 。 在redux-saga官网中的解释是一个saga就像是应用程序中一个单独的线程,独自负责处理副作用。第一次读完这句话,心里并没有太多的思考。因此后续在项目中也爬了许多坑。在这里我们需要明确两点,saga如何像一个单独的线程,在项目中具体是什么样的?什么叫做副作用? 首先来看两段示例代码: 代码一: ``` js // getInitialData获取初始化数据 handleFetchShopData 获取商品数据 function* initialAct() { try { const res = yield call(getInitialData) const { code, userType } = res if(code === '0'){ if(userType) { yield fork(handleFetchShopData, userType) } } } catch(err) { console.log('initialAct', err) } } function* handleFetchShopData(param) { // do

How to get something from the state / store inside a redux-saga function?

南楼画角 提交于 2019-11-29 19:04:22
How do I access the redux state inside a saga function? Short answer: import { select } from 'redux-saga'; ... let data = yield select(stateSelectorFunction); NickGnd As @markerikson already says, redux-saga exposes a very useful API select() to invoke a selector on the state for getting some part of it available inside the saga. For your example a simple implementation could be: /* * Selector. The query depends by the state shape */ export const getProject = (state) => state.project // Saga export function* saveProjectTask() { while(true) { yield take(SAVE_PROJECT); let project = yield select

Why use Redux-Observable over Redux-Saga?

浪子不回头ぞ 提交于 2019-11-29 18:41:24
I have used Redux-Saga . Code written with it is easy to reason so far, except JS generator function is messing up my head from time to time. From my understanding, Redux-Observable can achieve the similar job that handles side effects but without using generator function. However, docs from Redux-Observable does not provide many opinions of why it is superior against Redux-Saga. I would want to know whether not using generator function is the only benefit of using Redux-Observable. And what could be the downsides, gotcha or compromises from using Redux-Observable instead of Redux-Saga? Thanks

How to “yield put” in redux-saga within a callback?

徘徊边缘 提交于 2019-11-29 16:54:17
问题 Because "yield"-statement isn't allowed within a callback, how can i use the "put" feature of redux-saga within a callback? I'd like to have the following callback: function onDownloadFileProgress(progress) { yield put({type: ACTIONS.S_PROGRESS, progress}) } This isn't working and ends up in "unexpected token", because yield is not allowed in a plain function. Otherwise i can't pass a callback as a " function * ", that would allow yield. ES6 seems broken here. I've read that redux-saga

How to debug rxjs5?

最后都变了- 提交于 2019-11-29 11:46:05
问题 On RxJS - Goals I read that their goal is better debuggability: Goals Provide more debuggable call stacks than preceding versions of RxJS I have just started to use redux-observable which is quite easier for me to understand comparing it to redux-saga as I am already used to the reactive style with lodash and ramda (ok, fp style maybe ;). I was surprised that it's not possible to debug it yet. Is it true? If so, then I gotta switch to redux-saga s maybe or stick with redux-thunk . Edit based

What's the difference between fork and spawn in redux-saga?

守給你的承諾、 提交于 2019-11-29 10:48:28
问题 The docs say fork is an attached fork and spawn is a detached fork - how do they differ? 回答1: One way to look at it is to see your saga's as a Graph. 'fork' creates a child from the calling process. While 'spawn' creates a new child at the root of the graph. So when you 'fork' another process, the parent process will wait until the 'forked' process is finished. Also every exception will bubble up from the child to the parent and can be caught in the parent. A 'spawned' process though will not

redux-saga when to use fork?

蹲街弑〆低调 提交于 2019-11-28 18:37:34
问题 what would be the difference between the two approaches below? export function* watchLoginUser() { yield takeEvery(USER_LOGIN, loginUser) } export function* watchLogoutUser() { yield takeEvery(USER_LOGOUT, logoutUser) } export function* watchGetParties() { yield takeEvery(PARTIES_GET, getParties) } export default function* root() { yield [ fork(watchLoginUser), fork(watchLogoutUser), fork(watchGetParties) ] } export default function* root() { yield [ takeEvery(USER_LOGIN, loginUser),