【问题标题】:Redirect to another component after submit in react在反应中提交后重定向到另一个组件
【发布时间】:2019-06-24 02:41:07
【问题描述】:

我是 React 新手。单击提交按钮后,我想从登录表单重定向到另一个名为 MainModule 的页面(另一个组件)。

这是我的登录表单=>

import React, {Component} from "react";
import { withRouter} from 'react-router-dom';
import {Button,FormGroup,FormControl,FormLabel,Form} from "react-bootstrap";


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

        this.state={
            email:"",
            password:""
        };
    }

    handelChange = event =>{
        this.setState({
            [event.target.id]:event.target.value  
        },()=>{

        });       

    }

    handelSubmit = event => {
        event.preventDefault();

        this.props.history.push('/MainModule');



    }

    render()
    {
        return (
            <div className="Login">
                <Form onSubmit={this.handelSubmit}>

                    <FormGroup controlId="email">
                        <FormLabel>Email</FormLabel>
                        <FormControl autoFocus type="email" value={this.state.email} onChange={this.handelChange}/>
                    </FormGroup>
                    <FormGroup controlId="password">
                        <FormLabel>Password</FormLabel>
                        <FormControl type="password" value={this.state.password} onChange={this.handelChange}/>
                    </FormGroup>

                    <Button type="submit">Login</Button>
                </Form>



            </div>
        )
    }
}

export default withRouter(Login);

但问题是在我单击提交按钮后,url 更改为 MainModule 但未显示 MainMoudle 表单,仅显示当前登录表单。我认为这需要定义 MainModule 的路由,我不知道怎么做。请帮忙。

更新

我的 MainModules =>

import React from 'react';
import {  Route, Link,Switch,BrowserRouter } from "react-router-dom";
import allowance from './components/master/allowance';


class MainModule extends React.Component {
    // constructor(props){
    //   super(props)
    // }




    render(){
      return(
          <div className="mainform">
               <nav className="navbar navbar-expand-lg navbar-light bg-light">            
                <Link className="navbar-brand" to="/MainModule">TMS</Link>
                <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                </button>
                <div className="collapse navbar-collapse" id="navbarNavAltMarkup">
                    <div className="navbar-nav">
                        <Link className="nav-item nav-link" to={'/allowance'}>Allowance</Link>

                    </div>
                </div>
                </nav>

                <div id="maincontent">                 
                    <Route path='/allowance' component={allowance} />             
                </div>     
          </div>        
      )
    }
  }

  export default MainModule;

【问题讨论】:

    标签: reactjs react-router-dom


    【解决方案1】:

    你是对的,你必须为你的 MainModule 组件定义一个路由。

    在您的 App.js 文件中,我假设您已经设置了路线:

    import React from "react"
    import { BrowserRouter, Route } from "react-router-dom"
    import Login from "/yourcomponentfolder/Login"
    import MainModule from "/yourcomponentfolder/MainModule"
    
    const App = () => {
       return(
          <BrowserRouter>
               <div>
                   //Using exact tells react-router that you will only render this component if the URL matches exactly with the path definition.
                   <Route path="/" component={Login} exact/>
                   <Route path="/MainModule" component={MainModule}/>          
               </div>
          </BrowserRouter>
       )
    
    }
    

    它非常简洁,您使用 Route 组件,给它一个 path 和一个 component 来渲染。

    【讨论】:

    • 感谢您的回答,但为什么要在 MainModule url 中同时显示 login 和 MainModule 组件?我只想显示 MainModule。
    • @Jze 是的,这告诉我您的登录组件实际上正在使用路径为“/”的路由,如果您不希望发生这种情况,请更新该路由以使用 exact 道具.我会在上面更新的答案中告诉你。
    • 在我改变 MainModules 中的导航栏路由后不再工作。我应该问另一个问题还是在聊天室讨论?
    • @Jze 发布您的 MainModules 组件代码。我可以尝试找出问题所在。上面的代码是设置路由所需的最低限度。
    • @Jze 您需要更改此行 &lt;Route path='/allowance' component={allowance} /&gt; 您不应该像这样渲染 Route 组件。只需执行您的组件,将其替换为:&lt;Allowance/&gt;
    猜你喜欢
    • 2019-01-19
    • 2020-03-21
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2013-06-14
    • 2017-11-25
    相关资源
    最近更新 更多