Redux Saga 异步调用两次

问题描述

我正在使用 redux saga 构建 react-native 应用程序。我想我做错了什么。这可能是因为我不太了解 saga 的工作原理。但我试图理解的部分是为什么你需要调用两次。

我的第一次通话按钮。

'whitenoise.middleware.WhiteNoiseMiddleware',

我的按钮功能

    <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,}; 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,}); } }

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

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

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

yield call(loginWithEmail,loginRequest);

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

解决方法

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

解决方案是在与该特定路由对应的容器未安装时取消注册 saga