如何从redux-saga函数中的状态/商店中获取内容? [英] How to get something from the state / store inside a redux-saga function?
本文介绍了如何从redux-saga函数中的状态/商店中获取内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在传奇功能中访问redux状态?
How do I access the redux state inside a saga function?
简答:
import { select } from 'redux-saga';
...
let data = yield select(stateSelectorFunction);
推荐答案
正如@markerikson所说, redux-saga
公开了一个非常有用的API select()
在状态中调用选择器
,以便在saga中获取部分内容。
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(getProject); // <-- get the project
yield call(fetch, '/api/project', { body: project, method: 'PUT' });
yield put({type: SAVE_PROJECT_SUCCESS});
}
}
除了建议的 doc ,有一个很好的视频教程解释了如何使用选择器
终极版。另请查看Twitter上的这个有趣的帖子。
In addition to the suggested doc by @markerikson, there is a very good video tutorial by D. Abramov which explains how to use selectors
with Redux. Check also this interesting thread on Twitter.
这篇关于如何从redux-saga函数中的状态/商店中获取内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文