使用React Router,< Redirect/>不重定向到任何地方,但是为什么呢? [英] Using React Router the <Redirect /> Does not redirect anywhere, but why?

查看:193
本文介绍了使用React Router,< Redirect/>不重定向到任何地方,但是为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试不成功地将用户重定向回"/"或"/home"在.then()内部完成注册后,promise中的所有内容都会执行重定向以外的操作.我只使用了if(true)来对其进行测试,并且确实可以做到这一点,因为我可以使console.log在此处显示文本,但是Redirect是徒劳的..

I have been trying to Unsuccesfully redirect a user back ho "/" or "/home" after registration is done inside .then() , eveyrthing inside promise executes EXCEPT the redirection. I used just if(true) to test it, and It does go to that point, because I can get the console.log to display text there, but just Redirect is fruitless..

const createUserWithEmailAndPasswordHandler =(event)=> { event.preventDefault();

const createUserWithEmailAndPasswordHandler =(event) => { event.preventDefault();

    auth.createUserWithEmailAndPassword(signUp.email, signUp.pw1).then((authData) =>{
        console.log("authData" + authData);
        addUserDataToDb({
            email: signUp.email,
            username: signUp.username,
        });
        if (true) {
            return <Redirect to='/home' />;
        }
    })
        .catch((error) => {
            setSignUp({
                ...signUp,
                error: error,
            });
            console.error(error);
        });

    setSignUp(INITIAL_STATE);

}

推荐答案

Redirect需要进行渲染才能正常工作!您可以尝试使用history.push代替.

Redirect need to be rendered in order to work! You can try using history.push instead.

这篇关于使用React Router,&lt; Redirect/&gt;不重定向到任何地方,但是为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆