
使用常量历史记录后出错
我不能使用历史钩子,尽管我遵循了internet.Would中提到的相同步骤,真的很感谢一些帮助
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.发布于 2021-09-18 04:56:44
首先,您使用的是onClick而不是onclick。在使用html时,onClick是可以的,但在使用react时,请使用onclick。
不使用内联函数,而是创建一个名为handleClick的单独函数,然后在单击后调用此函数。
有关更多参考信息,请查看此链接- React Router Dom
发布于 2021-09-18 04:28:29
问题是你忘记在历史之前写const这个词了。
https://stackoverflow.com/questions/69231548
复制相似问题