Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

How to redirect to another page with the function component in reactjs Login Validation?

I just want to know then when i return redirect link the it dose not working properly please as soon as possible give me suggestion. It would be great if anybody could figure out where i did mistake.

import React, { useState } from 'react';
    import { Form, Button } from 'react-bootstrap';
    import { Redirect } from 'react-router-dom';
    
    const AdminLogin = () => {
    
        const [userName, setUserName] = useState("");
        const [password, setPassword] = useState("");
    
        const submitForm = (e) => {
            e.preventDefault();
            if (userName === "rajpreet" && password === "123") {
                alert("abd");
                return <Redirect to="/AdminPanel" />
            } else {
                alert("Credentials Error");
            }
        }
        return (
            <>
                <Form onSubmit={submitForm} >
                            <h3 className="text-dark text-center mb-3">Thrill Hiker</h3>
                            <Form.Group controlId="formBasicEmail">
                                <Form.Label>User Name</Form.Label>

                                <Form.Control name="username" value={userName} onChange={(e) => { setUserName(e.target.value) }} className="adminUser" type="text" placeholder="User Name" />

                                <Form.Text className="text-muted">
                                    We'll never share your User Name with anyone else.
                                </Form.Text>
                            </Form.Group>

                            <Form.Group controlId="formBasicPassword">
                                <Form.Label>Password</Form.Label>

                                <Form.Control name="password" value={password} onChange={(e) => { setPassword(e.target.value) }} className="adminPassword" type="password" placeholder="Password" />

                            </Form.Group>
                            <Button variant="primary" type="submit">Submit</Button>
                        </Form>
            </>
        );
    
    }
    
    
    export default AdminLogin;
question from:https://stackoverflow.com/questions/66047089/how-to-redirect-to-another-page-with-the-function-component-in-reactjs-login-val

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...