【发布时间】:2021-11-12 19:28:23
【问题描述】:
[使用 const history 后出错]
我无法使用历史钩子,即使我按照互联网上提到的相同步骤进行操作。非常感谢一些帮助
import React, { Component } from 'react'
import LoginScreen from './LoginScreen'
import './MyStyles.css'
import { useHistory } from "react-router-dom"
function Home() {
const history = useHistory();
return (
<div>
<html>
<head>
<title>CSS Website Layout</title>
</head>
<body>
<div class="topnav">
<button>Home</button>        
<button>Blog</button>        
<button>Merchandise</button>          
<button>About us</button>      
<button style={{float: 'right'}} onclick={()=> history.push("/LoginScreen")}>Sign Up!</button>
</div>
我得到的错误是
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
【问题讨论】:
-
添加您的最小代码,以便我们提供帮助
-
是的,我已经添加了
-
'./LoginScreen'以及 -
你在你的应用程序中安装了 react-router-dom 吗?执行这个命令来安装它:
npm install --save react-router-dom -
请发布您的
package.json。
标签: reactjs react-hooks react-router