r/reactjs • u/Rodaxoleaux • Sep 05 '26
Needs Help I am wracking my brain.
Okay, I'm making a little website with the new React (I'm coming back from 7 years [Tailwind CSS included, Typescript (for the first time I'll add)].) I'm making a hamburger menu and the onClick function just didn't work. Okay, so there must be some weird stuff happening. No. I made a button with an onClick in a nearly top-level fragment and it does nothing.
I just want to understand if there's something fundamental that has changed, perhaps? This isn't a "please give me answers" or information kind of deal. I just want to know if I'm going crazy here. Is there any weird bugs that React has created like it did a bunch from '18-'20; or should I just "git gud"?
const handleClickTest = (event: React.MouseEvent<HTMLButtonElement>) => {
console.log("AAAAAAAA", event.currentTarget);
};
return (
<>
<button className="relative z-50 pointer-events-auto"
onClick={(e) => handleClickTest(e)}
>
<h1>button</h1>
</button>
...
8
u/Psionatix Sep 05 '26 edited Sep 05 '26
Code in your post is a mess, let's format it properly:
This code should work as is assuming it's the definition of a component, if it isn't working then something else is going wrong and you haven't provided enough information to determine what. Did you check the console for errors after rendering and after clicking the element?
In your post you're missing a closing fragment tag
</>but I'm guessing that's a copy/paste error.Just some side notes:
onClick={handleClickTest}, in some cases having the inline arrow function can be bad as it creates a new function reference (new prop) on every re-render. In this case yourhandleClickTestis also new on every re-render (no useCallback), just something to be aware of.useCallback,useMemo, etc - this stuff hasn't changed much since functional components and hooks became a thing. But if you're using the react compiler, it can optimise some of this stuff for you.3
u/BreadStickFloom Sep 05 '26
Is the h1 covering the entire button and stopping the click from reaching the button itself?
1
u/Rodaxoleaux Sep 06 '26
For the record, I have recreated the project, saved my components (without editing them,) placed them into the new project, and suddenly, onClick just works. I'm going to be honest: I'm a very curious person, but even I don't care why. Back to work. I remember when I was doing React all the time that it was jank garbage. Some things never change.
2
u/FitPhone6332 Sep 05 '26
what the hell is this? this code is unreadable & uncomplete.
1
u/nitewalker_J Sep 05 '26
Code looks fine. Check two things:
0
u/Rodaxoleaux Sep 05 '26
Wait, I have to comment under a post? I don't understand. Okay, here's a comment, but... the above information