reactjs - 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("/");
}
})
解决方案
只需像这样调用 useHistory 钩子:
const history = useHistory()
推荐阅读
- google-analytics - GTM - 为数组的每个元素向 Google Analytics 触发事件
- reactjs - 从浏览器中的反应应用程序调用 docker 引擎 api 时连接被拒绝
- ruby - 带有向下宝石的 Ruby 进度条
- node.js - Babel 节点检查异步/等待无序跟踪
- javascript - 根据数组javascript中的键在数组组合上创建一个新数组
- batch-file - 无法使用批处理删除目录和子目录的空文件夹
- python - python - 删除行并将它们的值分配给其他行
- unity3d - Unity WebGL 上的材质纹理比编辑器/构建上的非常模糊
- django - 创建 API 端点的正确方法
- java - Executorservice 等待任务完成后