简体   繁体   English

在 React 中使用 ReactRouter 进行条件路由

[英]Conditional routing with ReactRouter in React

 class Login extends Component { async handle_login(e) { e.preventDefault(); this.props.history.push('/home') } render() { return ( <input type='submit' value='Log in' onSubmit={(e)=>this.handle_login(e)}></input> <input type='submit'value='Sign up' onSubmit={(e)=>this.handle_signup(e)}></input> ); } } export default withRouter(Login)

class App extends Component {
  // app.js

 render() {
 
return (
    <Router> 
      <div className='App' style={{marginTop:'0px'}}>
        <div className='container'>
          <Header></Header>
            
        <Route exact style={{marginTop:'0px'}} path='/' render={(props)=>(
  
   <div style={{display:'flex',justifyContent:'center'}}>
     {/* add  a  background in this div */}
   <Link to='/login' style={{color:'#000000', fontSize:'large',paddingRight:'10px' }}> Login </Link>
   
   <Link to='/' style={{color:'#000000', fontSize:'large' }}> Index </Link>
   </div>


)}></Route>


  <Route exact path='/home' component={Home}></Route>
  <Route  exact path='/login' component={Login}></Route>
  </div>   
  </div>  
       </Router>
  );
 }}
 export default App;

I am trying to redirect the 'login' component to the '/home' using withRouter using the aforementioned code, but running the code does nothing, neither does it throw any error.I have attached the codes of both the home and the login components.我正在尝试使用上述代码使用 withRouter 将“登录”组件重定向到“/ home”,但运行代码什么也不做,也不会引发任何错误。我已经附上了主页和登录组件的代码.

The main issue is probably because you forgot your constructor to get the props and bind your method .主要问题可能是因为您忘记了构造函数来获取道具并绑定您的方法

Update your class to this将您的 class 更新为此

class Login extends Component {
  constructor(props) {
    super(props);

    // This binding is necessary to make `this` work in the callback
    this.handle_login = this.handle_login.bind(this);
  }
  
  // No "async" need here
  handle_login(e) {
    e.preventDefault();
    this.props.history.push('/home')
  }
  render() {
    return (     
      <input type='submit' value='Log in' onSubmit={(e)=>this.handle_login(e)}></input>
      <input type='submit'value='Sign up' onSubmit={(e)=>this.handle_signup(e)}></input>
    );
  }
}

export default withRouter(Login)

I would also suggest passing your method to the onSubmit handle, instead of creating a new function there:我还建议将您的方法传递给onSubmit句柄,而不是在那里创建一个新的 function :

<input type='submit' value='Log in' onSubmit={this.handle_login}></input>

Update更新

I also notice that you have 2 inputs of type submit , which is not very common.我还注意到您有 2 个submit类型的输入,这不是很常见。 Your action is also in the onSubmit and not onClick , but you don't have a <form> which is usually what triggers the submit function.您的操作也在onSubmit而不是onClick中,但您没有通常触发submit function 的<form>

My suggestion is to review your HTML structure as well and make sure it make sense.我的建议是检查您的 HTML 结构,并确保它有意义。 For now, try this to at least get your method working:现在,试试这个至少让你的方法工作:

render() {
  // buttons have type="submit" by default, so no need to include that
  return (
    <button value='Log in' onClick={(e)=>this.handle_login(e)}></input>
  );
}

There is an interesting discussion here , as additional reference. 这里有一个有趣的讨论,作为附加参考。

@BrunoMonteiro is correct but there is an alternate option for this you can declare your function as arrow function so that you don't have to bind @BrunoMonteiro 是正确的,但有一个替代选项,您可以将 function 声明为箭头 function 以便您不必绑定

class Login extends Component {
  constructor(props) {
    super(props);
  }
  
  
  handle_login=async(e)=> {
    e.preventDefault();
    this.props.history.push('/home')
  }
  render() {
    return (     
      <input type='submit' value='Log in' onClick={(e)=>this.handle_login(e)}></input>
      <input type='submit'value='Sign up' onClick={(e)=>this.handle_signup(e)}></input>
    );
  }
}

export default withRouter(Login)

also make sure you have access to history property in your props for checking this you can do console.log(this.props) and check whether it has required property or not还要确保您可以访问道具中的历史记录属性以进行检查,您可以执行 console.log(this.props) 并检查它是否具有必需的属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM