onSubmit按钮未重定向到“个人档案”组件页面

问题描述

所以我希望我的“提交”按钮可以将我定向到我的React应用程序中的“个人资料”页面。但我无法将其与在线给出的建议配合使用。我想知道我的代码中有什么错误。 我希望能够单击“注册”按钮,并能够进入我的应用程序的/ profile路由。我不确定为什么它不起作用,并且在任何地方都没有显示任何错误,此刻它只是进入我的Express POST路由。 sign-up.component.js

import React,{ Component } from "react";
import { Redirect } from 'react-router';
import { withRouter } from 'react-router';

export default class SignUp extends Component {
    constructor(props){
        super(props)
        this.state = {
            firstName: '',lastName: '',email: '',}
    }
    onChange = (e) => {
        this.setState({ [e.target.name]: e.target.value });
      }

  onSubmit = (e) => {
        e.preventDefault();
        const {firstName,lastName,email} = this.state;

        fetch('http://localhost:9000/users/new',{
        method: "POST",headers: {
            'Content-Type' : 'application/json'
        },body: JSON.stringify(this.state)
    })
    .then((result) => result.json())
    .then((info) => {console.log(info)})
    this.props.history.push('/profile');    }

render() {
        return (
            <form method='POST' action='http://localhost:9000/users/new'>
                <h3>Sign Up</h3>

                <div className="form-group">
                    <label>First name</label>
                    <input type="text" className="form-control" placeholder="First name" name ="firstName"/>
                </div>

                <div className="form-group">
                    <label>Last name</label>
                    <input type="text" className="form-control" placeholder="Last name" name="lastName" />
                </div>

                <div className="form-group">
                    <label>Email address</label>
                    <input type="email" className="form-control" placeholder="Enter email" name="email" />
                </div>

                <div className="form-group">
                    <label>Password</label>
                    <input type="password" className="form-control" placeholder="Enter password" />
                </div>

                <button type="submit" className="btn btn-primary btn-block" onClick={this.onSubmit}>Sign Up</button>
                
                <p className="forgot-password text-right">
                    Already registered <a href="#">sign in?</a>
                </p>
            </form>
        );
    }

App.js

import React from 'react';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import './App.css';
import { browserRouter as Router,Switch,Route,Link } from "react-router-dom";

import Login from "./components/login.component";
import SignUp from "./components/sign-up.component";
import Profile from "./components/profile";

function App() {
  return (<Router>
    <div className="App">
      <nav className="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
        <div className="container">
          <Link className="navbar-brand" to={"/sign-in"}>Code Process Reviews</Link>
          <div className="collapse navbar-collapse" id="navbarTogglerDemo02">
            <ul className="navbar-nav ml-auto">
              <li className="nav-item">
                <Link className="nav-link" to={"/sign-in"}>Login</Link>
              </li>
              <li className="nav-item">
                <Link className="nav-link" to={"/sign-up"}>Sign up</Link>
              </li>
            </ul>
          </div>
        </div>
      </nav>

      <div className="auth-wrapper">
        <div className="auth-inner">
          <Switch>
            <Route exact path='/' component={Login} />
            <Route path="/sign-in" component={Login} />
            <Route path="/sign-up" component={SignUp} />
            <Route path="/profile" component={Profile} />
          </Switch>
        </div>
      </div>
    </div></Router>
  );
}

export default App;



解决方法

您不能在箭头函数中使用this将箭头语法更改为普通 从onSubmit(e)=>{}function onSubmit(e){}

,

不是使用history.push设置状态标志进行重定向

例如

    this.state = {
       ...
       url: '',}
    
    ....
    
    // After network response 
    this.setState({url: '/profile'});
    
    
    // And in Component
    { this.state.url ? <Redirect to={this.state.url} /> : null }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...