[英]React onClick property not firing
I am trying to implement a simple component (an animated hamburger menu button).我正在尝试实现一个简单的组件(动画汉堡菜单按钮)。 Its class needs to be updated when it is active or not.
它的类无论是否处于活动状态都需要更新。 For that point, I'm using a state hook.
对于这一点,我正在使用状态挂钩。 I want to add a onClick property to this button to invert its state.
我想向这个按钮添加一个 onClick 属性来反转它的状态。 But this onClick function is never firing.
但是这个 onClick 函数永远不会触发。 When I set the button active by myself, the button appearence changes as I wanted.
当我自己设置按钮处于活动状态时,按钮外观会根据需要而改变。 I am using fullPage.js in my project.
我在我的项目中使用 fullPage.js。
I saw a lot of topic about that, but no one answered to my problem...我看到了很多关于这个的话题,但没有人回答我的问题......
Thanks for future answers !感谢未来的答案!
Here is my code :这是我的代码:
import React, { useState } from 'react';
import './hamburger.css';
const MenuButton = () => {
const [isMenuActive, setIsMenuActive] = useState(false);
return (
<button className={`hamburger hamburger--slider ${isMenuActive && "is-active"}`} type="button" onClick={() => setIsMenuActive(!isMenuActive)}>
<span className="hamburger-box">
<span className="hamburger-inner"></span>
</span>
</button>
);
};
export default MenuButton;
``
You are starting the state with false value.您以假值开始状态。 So it's not firing the onClick event because it's inactive from the get go
所以它不会触发 onClick 事件,因为它从一开始就处于非活动状态
change the initial state useState(true)
改变初始状态
useState(true)
Ok I found the problem.好的,我发现了问题。
I didn't noticed that I was using React v17 (beta).我没有注意到我使用的是 React v17(测试版)。 I downgraded to React 16 and my button works well !
我降级到 React 16,我的按钮运行良好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.