2

我使用 redux、redux-saga 和不可变 js 创建了一个基本的授权流程。

Redux 表单 (v6.0.0-rc.4) 允许表单创建不可变映射。我将这些值传递给 redux-saga,在那里我试图将这些值传递给我的登录函数。

问题 1:从概念上讲,何时是values.get('username')访问不可变映射内数据的合适时间?在我的传奇中,在功能中?我是否应该等到最后一步才能提取值?

问题 2:假设我能够在正确的位置提取值,我不确定在 sagas 中应该如何处理 - 这是我的 loginFlow 传奇:

export function* loginFlow(data) {
  while (true) {
    yield take(LOGIN_REQUEST);

    const winner = yield race({
      auth: call(authorize, { data, isRegistering: false }),
      logout: take(LOGOUT),
    });

    if (winner.auth) {
      yield put({ type: SET_AUTH, newAuthState: true });
      forwardTo('/account');
    } else if (winner.logout) {
      yield put({ type: SET_AUTH, newAuthState: false });
      yield call(logout);
      forwardTo('/');
    }

  }
}

作为data来自 redux-form 的不可变映射。但是,每当我控制台登录data我的 sagas 时,它只会返回0.

4

1 回答 1

1

显然我没有正确处理将不可变 Map 传递给操作 - 正确的代码:

export function* loginFlow() {

  while (true) {

    // this line ensures that the payload from the action
    // is correctly passed through the saga

    const { data } = yield take(LOGIN_REQUEST);

    const winner = yield race({

      // this line passes the payload to the login/auth action

      auth: call(authorize, { data, isRegistering: false }),
      logout: take(LOGOUT),
    });

    if (winner.auth) {
      yield put({ type: SET_AUTH, newAuthState: true });
      forwardTo('/account');
    } else if (winner.logout) {
      yield put({ type: SET_AUTH, newAuthState: false });
      yield call(logout);
      forwardTo('/');
    }
  }
}
于 2016-08-23T20:02:23.243 回答