【问题标题】:navigate.push is not a functionnavigate.push 不是一个函数
【发布时间】:2022-01-09 00:27:34
【问题描述】:

我在 react reuter dom 版本中遇到与 navigate.push 相关的错误不是函数。 以下错误扰乱了我的整个干净代码,如果有人帮助我解决这个问题,那就太好了。

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));
  };

【问题讨论】:

    标签: reactjs react-dom navigateurl


    【解决方案1】:

    如果您使用的是 React Router v6: 使用navigate("/") 而不是navigate.push("/")

    【讨论】:

    • 谢谢大哥,非常感谢它现在正在工作
    • @VishalMishra 请标记为正确答案
    • 如果我使用:BrowserRouter as useNavigate,如何使它工作,因为当我尝试它说 Navigate 不是一个函数时
    猜你喜欢
    • 2017-11-26
    • 1970-01-01
    • 2016-07-08
    • 2013-03-03
    • 2018-03-12
    • 2017-08-17
    • 2017-09-22
    • 2018-12-16
    • 2017-12-23
    相关资源
    最近更新 更多