Redux Saga两次异步调用



我正在用redux传奇构建react原生应用程序。我想我做错了什么。这可能是因为我对传奇故事的运作方式不太了解。但我想明白的是,为什么你需要打两次电话。

我第一次打电话的按钮。

<Button
loading={pending}
onPress={this.handleLogin}
containerStyle={styles.margin}
/>

我的按钮功能。

handleLogin = () => {
const { screenProps: {t} } = this.props;
const {email, password} = this.state;
this.props.dispatch(signInWithEmail({email, password}));

我的行动

import * as Actions from './constants';
export function signInWithEmail({email, password}) {
return {
email,
password,
type: Actions.SIGN_IN_WITH_EMAIL,
};
}

我的传奇听众

export default function* authSaga() {
yield takeEvery(Actions.SIGN_IN_WITH_EMAIL, signInWithEmailSaga);
yield takeEvery(Actions.SIGN_IN_WITH_MOBILE, signInWithMobileSaga);
yield takeEvery(Actions.SIGN_UP_WITH_EMAIL, signUpWithEmailSaga);
yield takeEvery(Actions.SIGN_IN_WITH_GOOGLE, signInWithGoogleSaga); 
...
}

我的传奇功能

function* signInWithEmailSaga({email, password}) {
try {
const language = yield select(languageSelector);
let loginRequest = {
email,
password,
};
const token = globalConfig.getToken();
const responseLogin = yield call(loginWithEmail, loginRequest);
if (!responseLogin.user) {
console.log('if not user', responseLogin);
yield put({
type: Actions.SIGN_IN_WITH_EMAIL_ERROR,
payload: {
message: responseLogin.message,
},
});
} else {
yield call(setUser, responseLogin);
}
} catch (e) {
// yield call(handleError, e)
yield put({
type: Actions.SIGN_IN_WITH_EMAIL_ERROR,
payload: {
message: e.message,
},
});
}
}

我向服务器发送电子邮件和密码以获得响应。我通过yield call(loginWithEmail, loginRequest);

如果signInWithEmailSaga函数只包含控制台日志行,则只触发两次。这对我来说是个大错误。我花了20个小时,但我没有。

我使用了takeLatest、takeEvery并更改了动作名称。同样的结果。

这是我的redux存储和saga中间件设置。

import {composeWithDevTools} from 'redux-devtools-extension';
import {createStore, applyMiddleware, compose} from 'redux';
import createSagaMiddleware from 'redux-saga';
import {persistStore, persistReducer} from 'redux-persist';
import AsyncStorage from '@react-native-community/async-storage';
import immutableTransform from 'redux-persist-transform-immutable';
import rootReducer from './reducers';
import rootSaga from './sagas';
const persistConfig = {
key: 'root',
transforms: [immutableTransform()],
storage: AsyncStorage,
whitelist: [
// 'test',
'common',
'category',
'classified',
//'auth',
],
};
const composeEnhancers =
process.env.NODE_ENV === 'development'
? composeWithDevTools({realtime: true})
: compose;
const sagaMiddleware = createSagaMiddleware();
const persistedReducer = persistReducer(persistConfig, rootReducer);
export default () => {
const enhancer = composeEnhancers(applyMiddleware(sagaMiddleware));
const store = createStore(persistedReducer, enhancer);
let persistor = persistStore(store);
// then run the saga
sagaMiddleware.run(rootSaga);
return {store, persistor};
};

如果你能提供问题所在的信息,我将非常高兴。谢谢。

当您更改路线并返回同一页面时,您的传奇听众可能会再次注册

解决方案是,当对应于该特定路线的容器未安装时,取消对传奇的注册

最新更新