Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React history.push to localhost:3000 not redirecting

I am creating a login page using React. history.push("/") isn’t directing and this error in console: Uncaught TypeError: Cannot read properties of undefined (reading ‘push’).

Did this from tutorial and followed the codes exactly, but still can’t figure out for days.

import { useState } from "react";
import styles from "./register.css";
import Login from "../../containers/login/index";

function Register({ props, history }) {

    const [name, setName] = useState();
    const [email, setEmail] = useState();
    const [password, setPassword] = useState();

    function registerClicked() {
        if (name !== " " && email !== " " && password !== " ") {
            console.log(name, email, password);
            alert("Thanks for registering");
            history.push("/");
        } else {
            alert("Please register to continue");
        }
    }


    return(
        <div className="regContainer">

            <p className="registerTitle">Register to sign in</p>

            <input type="text" 
            placeholder="Create username" 
            onChange={(nameText) => setName(nameText.target.value)} />

            <input type="text" 
            placeholder="Email address" 
            onChange={(emailText) => setEmail(emailText.target.value)} />

            <input type="password" 
            placeholder="Password" 
            onChange={(passwordText) => setPassword(passwordText.target.value)} />

            <button onClick={() => registerClicked()}>Register</button>

            <p className="loginInstead">Already have an account? Sign in instead</p>

        </div>
    );
}

export default Register;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

import { useNavigate } from "react-router-dom"

export default const Register = () => {
  const navigate = useNavigate();
  
  if(...){
    navigate('/')
  }
  
  
  return <div></div>
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading