Javascript 在redux saga中创建帐户后如何重定向回登录?

Javascript 在redux saga中创建帐户后如何重定向回登录?,javascript,reactjs,redirect,redux,redux-saga,Javascript,Reactjs,Redirect,Redux,Redux Saga,我是redux传奇的新手,我不太清楚为什么在创建帐户后不能重定向回登录页面?我已经包括了启用重定向所需的代码,但在注册时我总是得到一个空白页 action.js reducer.js saga.js 登记表 {({错误,触摸})=>( {errors.email&&touch.email&&( {errors.email} )} {errors.password&&touch.password&&( {错误.密码} )} )} 我正在使用formik渲染表单,但我认为我使用的表单并不重要。我

我是redux传奇的新手,我不太清楚为什么在创建帐户后不能重定向回登录页面?我已经包括了启用重定向所需的代码,但在注册时我总是得到一个空白页

action.js

reducer.js

saga.js

登记表


{({错误,触摸})=>(
{errors.email&&touch.email&&(
{errors.email}
)}
{errors.password&&touch.password&&(
{错误.密码}
)}
)}

我正在使用formik渲染表单,但我认为我使用的表单并不重要。我主要无法理解处理重定向的传奇部分。任何帮助都将不胜感激

经过多次尝试,我终于解决了这个问题

结果是
const item={uid:registerUser.user.uid,…currentUser}实际上指向了错误的数据库。删除它并设置为正确的db,一切都像一个符咒

因此,saga.js中的最终代码是:

function* registerWithEmailPassword({ payload }) {
  const { email, password, username } = payload.user;
  const { history } = payload;
  try {
    const registerUser = yield call(
      registerWithEmailPasswordAsync,
      email,
      password,
      username
    );
    if (!registerUser.message) {
      setCurrentUser(registerUser.data); <-- correct db
      yield put(registerUserSuccess(registerUser.data));
      history.push(adminRoot);
    } else {
      yield put(registerUserError(registerUser.message));
    }
  } catch (error) {
    yield put(registerUserError(error));
  }
}
function*registerWithEmailPassword({payload}){
const{email,password,username}=payload.user;
const{history}=有效载荷;
试一试{
const registerUser=产生调用(
registerWithEmailPasswordAsync,
电子邮件,
密码,
用户名
);
如果(!registerUser.message){

setCurrentUser(registerUser.data);似乎是正确的想法-adminRoot从何而来?您的路由器是否正确识别和处理它所指的任何路由?如果您得到一个空白页,我猜这不是传奇的问题,因为现在唯一更改历史记录的时间是在传奇中登录成功之后。adminRoot被用作传奇的一部分默认值
export const adminRoot='/app';
尝试类似于
历史记录的操作。按(“/hello world”);
并检查浏览器中的URL是否变为
/hello world
。URL保持为
http://localhost:3000/user/register
export default (state = INIT_STATE, action) => {
  switch (action.type) {
    case REGISTER_USER:
      return { ...state, loading: true, error: '' };
    case REGISTER_USER_SUCCESS:
      return {
        ...state,
        loading: false,
        currentUser: action.payload,
        error: '',
      };
    case REGISTER_USER_ERROR:
      return {
        ...state,
        loading: false,
        currentUser: null,
        error: action.payload.message,
      };
  }
};
const registerWithEmailPasswordAsync = async (email, password, username) => {
  const data = await axios.post(
    `${baseUrl}auth/signup`,
    { username, password, email },
    { headers }
  );
  return data;
};

function* registerWithEmailPassword({ payload }) {
  const { email, password, username } = payload.user;
  const { history } = payload;
  try {
    const registerUser = yield call(
      registerWithEmailPasswordAsync,
      email,
      password,
      username
    );
    if (!registerUser.message) {
      const item = { uid: registerUser.user.uid, ...currentUser };
      setCurrentUser(item);
      yield put(registerUserSuccess(item));
      history.push(adminRoot);
    } else {
      yield put(registerUserError(registerUser.message));
    }
  } catch (error) {
    yield put(registerUserError(error));
  }
}

export function* watchRegisterUser() {
  yield takeEvery(REGISTER_USER, registerWithEmailPassword);
}
            <Formik initialValues={initialValues} onSubmit={onUserRegister}>
              {({ errors, touched }) => (
                <Form className="av-tooltip tooltip-label-bottom">
                  <FormGroup className="form-group has-float-label">
                    <Label>
                      <IntlMessages id="user.fullname" />
                    </Label>
                    <Field
                      className="form-control"
                      name="username"
                      placeholder="John Doe"
                    />
                  </FormGroup>
                  <FormGroup className="form-group has-float-label">
                    <Label>
                      <IntlMessages id="user.email" />
                    </Label>
                    <Field
                      name="email"
                      className="form-control"
                      validate={validateEmail}
                      placeholder="johndoe@example.com"
                    />
                    {errors.email && touched.email && (
                      <div className="invalid-feedback d-block">
                        {errors.email}
                      </div>
                    )}
                  </FormGroup>
                  <FormGroup className="form-group has-float-label">
                    <Label>
                      <IntlMessages id="user.password" />
                    </Label>
                    <Field
                      className="form-control"
                      placeholder="Enter Password"
                      name="password"
                      type="password"
                      validate={validatePassword}
                    />
                    {errors.password && touched.password && (
                      <div className="invalid-feedback d-block">
                        {errors.password}
                      </div>
                    )}
                  </FormGroup>
                  <FormGroup className="form-group has-float-label">
                    <Label>
                      <IntlMessages id="user.repeat-password" />
                    </Label>
                    <Field
                      className="form-control"
                      name="repeatPassword"
                      type="password"
                      placeholder="Repeat Password"
                    />
                  </FormGroup>
                  <div className="d-flex justify-content-between align-items-center">
                    <Button
                      type="submit"
                      color="primary"
                      className={`btn-shadow btn-multiple-state btn-login ${
                        loading ? 'show-spinner' : ''
                      }`}
                    >
                      <span className="spinner d-inline-block">
                        <span className="bounce1" />
                        <span className="bounce2" />
                        <span className="bounce3" />
                      </span>
                      <span className="label">
                        <IntlMessages id="user.register-button" />
                      </span>
                    </Button>
                  </div>
                </Form>
              )}
            </Formik>
function* registerWithEmailPassword({ payload }) {
  const { email, password, username } = payload.user;
  const { history } = payload;
  try {
    const registerUser = yield call(
      registerWithEmailPasswordAsync,
      email,
      password,
      username
    );
    if (!registerUser.message) {
      setCurrentUser(registerUser.data); <-- correct db
      yield put(registerUserSuccess(registerUser.data));
      history.push(adminRoot);
    } else {
      yield put(registerUserError(registerUser.message));
    }
  } catch (error) {
    yield put(registerUserError(error));
  }
}