Reactjs 注册后如何使react路由器重定向到root?

Reactjs 注册后如何使react路由器重定向到root?,reactjs,redirect,react-router-dom,Reactjs,Redirect,React Router Dom,我已经创建了一个注册方法,该方法正在创建一个用户并将其保存在MongoDb上。后端工作正常,一切正常。用户被创建,但在前端,它停留在同一个注册页面上,而不是重定向到root async function authFunction (evt) { const response = await fetch('http://localhost:5000/api/users/signup', { method: 'POST', headers: { 'Conte

我已经创建了一个注册方法,该方法正在创建一个用户并将其保存在MongoDb上。后端工作正常,一切正常。用户被创建,但在前端,它停留在同一个注册页面上,而不是重定向到root

async function authFunction (evt) {

    const response = await fetch('http://localhost:5000/api/users/signup', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            username: nameValue,
            email: emailValue,
            password: passwordValue
        }),
    });

    const data = await response.json();
    if (!response.ok) throw new Error(data.text); 
    console.log({ user: data });

}
在App.js中,我已经创建了我需要并添加的路线,但什么都没有发生

<div className='App'>
    <Switch>
        <Route exact path='/' component={Landing} />
        <Route path='/tasks' component={TasksInitial} />
        <Route path='/auth' component={AuthPanel} />
        <Route path='/api/logout' component={Landing} />

        <Redirect to='/' />
    </Switch>
</div>


我想在注册后将其重定向到root。

从调用
authFunction
函数的组件中,您应该传递一个重定向函数,并在内部将其用作回调

authFunction(evt, this.props.history.push);
在您的功能中:

async function authFunction (evt, changeRoute) {
   ...
   if (response.ok) {
      changeRoute('/');
   }
}
请记住使用路由器将组件包装成
withRouter
hoc,以便访问
history
对象