首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React按钮onClick

React按钮onClick
EN

Stack Overflow用户
提问于 2021-09-18 04:20:43
回答 2查看 155关注 0票数 1

使用常量历史记录后出错

我不能使用历史钩子,尽管我遵循了internet.Would中提到的相同步骤,真的很感谢一些帮助

代码语言:javascript
复制
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> &ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
                            <button>Blog</button> &ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
                            <button>Merchandise</button> &ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
                            <button>About us</button>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
                            <button style={{float: 'right'}} onclick={()=> history.push("/LoginScreen")}>Sign Up!</button>
                        </div>

我得到的错误是

代码语言:javascript
复制
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.
EN

回答 2

Stack Overflow用户

发布于 2021-09-18 04:56:44

首先,您使用的是onClick而不是onclick。在使用html时,onClick是可以的,但在使用react时,请使用onclick。

不使用内联函数,而是创建一个名为handleClick的单独函数,然后在单击后调用此函数。

有关更多参考信息,请查看此链接- React Router Dom

票数 1
EN

Stack Overflow用户

发布于 2021-09-18 04:28:29

问题是你忘记在历史之前写const这个词了。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69231548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档