【发布时间】:2017-05-28 00:32:08
【问题描述】:
我试图从 redux-thunk 传递一些对 redux-saga 的调用,但我遇到了一些错误,我是 saga 的新手,所以这里来自 saga 及以下的 2 个函数的代码示例是我想要做的事情,如果有人可以提供帮助我会很有帮助的
var LOAD_TRANSLATIONS = exports.LOAD_TRANSLATIONS = 'loadTranslation';
// redux-thunk
var loadTranslations = exports.loadTranslations = function loadTranslations(translations) {
return function (dispatch) {
_index.I18n.forceComponentsUpdate();
dispatch({
type: LOAD_TRANSLATIONS,
translations: translations
});
};
};
// saga
function* loadTranslations() {
const lang = yield take(LOAD_TRANSLATIONS);
yield fork(getLang, lang);
}
function* getLang(lang){
console.log(lang);
yield put({
type: LOAD_TRANSLATIONS,
translations: lang
});
}
// redux-thunk
var SET_LOCALE = exports.SET_LOCALE = 'setLocal';
var setLocale = exports.setLocale = function setLocale(locale) {
return function (dispatch) {
_index.I18n.forceComponentsUpdate();
dispatch({
type: SET_LOCALE,
locale: locale
});
};
};
//saga
function* setLocale() {
const locale = yield take(SET_LOCALE);
yield fork(getLocale, locale);
}
function* getLocale(locale){
console.log(locale);
_index.I18n.forceComponentsUpdate();
yield put({
type: SET_LOCALE,
locale: locale
});
}
【问题讨论】:
-
能否请您粘贴您注册 saga 中间件的方式?
-
传奇看起来像这样
function* setLocale(){ yield takeEvery(SET_LOCALE, getLocale) }动作被传递到getLocale生成器函数。看看主页上的例子redux-saga.github.io/redux-saga
标签: reactjs redux-thunk redux-saga