javascript - 反应历史挂钩不起作用
问题描述
React Hook "useHistory" 在函数 "header" 中调用,它既不是 React 函数组件也不是自定义 React Hook 函数
下面是我的代码
import { Link, useHistory } from 'react-router-dom'
function header() {
let history = useHistory()
return (
<header>
<div className="header_buttons">
<Link onClick={history.push('/auth/login')} to='/auth/login'>
<a href="" className='primary-link'>Login</a>
</Link>
</div>
</header>
)}
export default header
下面是我得到的错误
Failed to compile.
./src/Header/header.js
Line 6:19: React Hook "useHistory" is called in function "header" which is neither a React function component or a custom React Hook function react-hooks/rules-of-hooks
解决方案
React 组件必须大写。将其重命名为Header
.
function Header() {
let history = useHistory();
return (...);
}
推荐阅读
- docker - 从子目录运行两个 docker 容器找不到配置文件
- html - 文本溢出:在 Chrome 中调整大小时出现省略号渲染问题
- python - 打印平衡括号的所有组合 - 当参数大于 4 时丢失案例
- azure - 如何根据 Azure 逻辑应用中的 oData 请求有条件地查询表
- gradle - 如何在不复制/粘贴大量代码的情况下为多个 gradle 项目提供相同的 3rd 方依赖项?
- visual-studio-code - 为什么满足格式后我的 vs-code 片段不起作用?
- python - 有没有办法检查一个值是否可以被给定数据集中的任何值整除?
- regex - 在 Shell 或 bash 中使用正则表达式的子字符串
- java - Java 引用条件表达式
- vulkan - 如何在 Vulkan 中将 ImGui 帧与场景渲染“混合”