React - 如何在发出请求后重定向到另一个组件?

React - How redirect to another component after making an request?

我是新手,正在寻找与垂钓者 $state.go 相当的东西。我正在使用路由器 4,但对我不起作用。我在网上看到的其他东西似乎不适用于路由器 4。任何帮助都会很棒!谢谢

class App extends React.Component{
  ...
  axis.post(/signup,{info})
    .then(function(){
     <Redirect if successful />
  })
}

 <HashRouter>
   <Route exact path='/' component={Home}/>
   <Route path='/login' component={Login}/>
   ...
 </HashRouter >

一个很好的方法是通过历史库(如果你还没有它,你可以这样做npm install history --save React-Routerv4 也使用这个库。

这是 ReactRouterv4 在其文档中也链接到的文档: https://github.com/ReactTraining/history

import createHistory from 'history/createBrowserHistory';

class App extends React.Component{
  ...
  axis.post(/signup,{info})
    .then(function(){
   const history = createHistory();
//listen for changes in current location

    const listen = history.listen((location, action) => {
      console.log(action, location.pathname, location.state);
    });
  //push the state as props to the route that will be redirected to
      history.push('/RouteToRedirectTo', { some: stateifneeded });

      //redirect to the route you want
      history.go('/RouteToRedirectTo');
  })
}

 <HashRouter>
   <Route exact path='/' component={Home}/>
   <Route path='/login' component={Login}/>
   ...
   </HashRouter >