首页 > 解决方案 > history.push 不是函数

问题描述

我正在尝试构建一个登录页面,当在登录/注册页面中单击创建帐户按钮时,检查用户的身份验证,它应该返回主页。但它显示错误

import React, { useState } from "react";
import "./Login.css";
import { Link, useHistory } from "react-router-dom";
import { auth } from "./firebase";

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

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

  };

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

    auth
      .createUserWithEmailAndPassword(email, password)
      .then((auth) => {
   
        console.log(auth);
        if (auth) {
          history.push("/");
        }
      })

标签: reactjsreact-routerreact-hooksreact-dom

解决方案


只需像这样调用 useHistory 钩子:

const history = useHistory()


推荐阅读