【问题标题】:React-router-dom's Redirect not doing anythingReact-router-dom 重定向不做任何事情
【发布时间】:2020-09-07 09:49:48
【问题描述】:

我正在开发一个身份验证系统(我目前正在测试但不发送数据) 没有做任何事情。我在这里做错了吗?

import React, { Component } from 'react';
import { withRouter, Link, Redirect, useHistory } from 'react-router-dom';


class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            email: '',
            password: '',
        };
    } 
    login(event) {
        event.preventDefault();
        return <Redirect to="/home"></Redirect>
    }
    render() {
        return (
            <div className="login">
                <img src="images/tradecast-logo.png"/>
                <h2>Employee Management System</h2>
                <form onSubmit={this.login}>
                    <h1>Inloggen</h1>
                    <p>E-mail</p>
                    <input type="text" name="email" placeholder="E-mail" />
                    <p>Wachtwoord</p>
                    <input type="password" name="password" placeholder="Wachtwoord"/>
                    <Link to="/forgot-password">Wachtwoord vergeten</Link>
                    <div className="btns">
                        <button type="submit">Aanmelden</button>
                        <Link className="btn-register" to="/register">Registreren</Link>
                    </div>
                </form>
                <p className="copyright">&copy; {new Date().getFullYear()} TradeCast Employee Management by Jens Bouma</p>
            </div>
        )
    }
}
        
export default withRouter(Login);
        

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

正如here 所说,必须呈现重定向,而不仅仅是返回。作为替代方案,由于您使用的是类组件,因此您可以改用this.props.history.push("/home”)

【讨论】:

  • 不错的黛安女士?
【解决方案2】:

重定向将在渲染函数 this.context.history.push("/home") 中工作

     this.context.history.push("/home")

【讨论】:

    猜你喜欢
    • 2022-06-24
    • 2021-02-22
    • 2019-06-28
    • 2018-02-04
    • 2019-02-05
    • 2023-04-03
    • 2021-07-27
    • 2023-03-22
    • 2022-08-08
    相关资源
    最近更新 更多