reactjsreact-domnavigateurl

navigate.push is not a function


I got the error in react reuter dom version related to navigate.push is not a function. this following error disturbing my whole clean code, It'll be great if somebody helps me to resolve this.

This is my code

import { Link, useNavigate } from "react-router-dom";
import { auth } from "./firebase";

function Login() {
  const navigate = useNavigate();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  const signIn = (e) => {
    e.preventDefault();

    auth
      .signInWithEmailAndPassword(email, password)
      .then((auth) => {
        navigate.push("/");
      })
      .catch((error) => alert(error.message));
  };

  const register = (e) => {
    e.preventDefault();

    auth
      .createUserWithEmailAndPassword(email, password)
      .then((auth) => {
        //it successfully created a new user with email and password

        if (auth) {
          navigate.push("/");
        }
      })
      .catch((error) => alert(error.message));
  };


Solution

  • If you are using React Router v6: use navigate("/") instead of navigate.push("/")