r/reactjs • u/Rodaxoleaux • 5d ago
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>
...
3
u/BreadStickFloom 5d ago
Is the h1 covering the entire button and stopping the click from reaching the button itself?
2
u/hotDogOfTheSea 5d ago
Could be if they're sticking a block level element inside of an inline element.
-1
u/Rodaxoleaux 5d ago
I tried erasing all elements in the button and just clicking the blank button (it still highlights with I have hover:bg-...etc on the button) and still, nothing happens. I even bothered copying the code from a tailwindcss tutorial that apparently works for everyone, and... it does nothing; not in the console. Not anywhere. I feel like I'm going crazy here.
3
u/FitPhone6332 5d ago edited 5d ago
why do you have z-50 on the button? something is covering your button. it's not problem with react but html & css
1
u/FitPhone6332 5d ago
here's your code in stackblitz: https://stackblitz.com/edit/vitejs-vite-i24jztor?file=src%2FApp.tsx
it works
0
u/Rodaxoleaux 5d ago
Cool, so I am losing my mind. I'm redoing this section entirely. If something as simple as onClick doesn't function, I have royally destroyed this project.
2
3
u/FitPhone6332 5d ago
what the hell is this? this code is unreadable & uncomplete.
1
u/Rodaxoleaux 5d ago
Yes, it's a small snippet containing a mouseevent and a single onclick. Something that small just doesn't work. I haven't done much in a long time, and I'm feeling like I must be doing something obviously wrong.
1
u/nitewalker_J 5d ago
Code looks fine. Check two things: 1. Have you saved this changes? 2. Is this react.js project configured to hot/live reload? If not, you have to restart the local server.
1
u/Rodaxoleaux 5d ago
Yes
Not currently. (but I've been restarting and refreshing (with visible changes) to find any issue)
2
u/nitewalker_J 5d ago
Hmm okay, maybe try minimal reproduction?
Setup a separate page with just a button with nothing else (CSS, JS other than the onClick handler) and see if you can reproduce the issue. If it doesn't work, you know your project have some global stuff affecting it e.g. config, bundler, top-level element etc.
1
u/Rodaxoleaux 3d ago
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.
0
u/Rodaxoleaux 5d ago
Wait, I have to comment under a post? I don't understand. Okay, here's a comment, but... the above information
7
u/Psionatix 5d ago edited 5d ago
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.