The onClick handler on a React <a> tag never runs, or the page jumps away
The handler runs when the page renders, and nothing happens when the link is clickedShort answer
Four things break an anchor's onClick. The handler is written as a call, so it runs while rendering and not on click. A custom component never forwards the onClick prop. A child element stops the event with stopPropagation. Or the link's default navigation fires and takes the page away. Add a console.log first.
Fix it fast
1Find out which cause you have
Put a log line as the first statement inside the handler, then click the link with the console open. If the line appears before you click, the handler is being called during render. If it never appears, the click does not reach the handler. If it appears and the page then jumps, the link's default action is the problem.
function handleClick(e) { console.log('anchor clicked', e.type); }2Match what you see, then jump to its fix
- 1
The log line prints when the component renders, and not when you click.The handler is called instead of passedFix - 2
The element is your own component, such as <NavLink onClick={...}>, and the log line never appears.A custom component receives onClick and does not use itFix - 3
The handler works when you click the text of the link, and never when you click an icon or button placed inside it.A child inside the link stops the clickFix - 4
The log line prints once, and then the page reloads or the address changes, so the result of the handler is lost.The link follows its href after your handlerFix
- 1
What does the error look like?
The handler runs when the page renders, and nothing happens when the link is clickedClicking the link does nothing, with no error in the consoleAfter the click the page reloads or the address changes, and the result of the handler is lost
Cause 1 of 4
The handler is called instead of passed
1Check
Look at the JSX. A function name followed by parentheses runs immediately while React renders, and what is handed to onClick is the return value, usually undefined. The React docs show this exact pair of onClick={handleClick} and onClick={handleClick()}.
grep -rnE "onClick=\{[A-Za-z_.]+\(" src/2Fix
Pass the function itself. When the handler needs an argument, wrap the call in an arrow function.
// wrong: runs during render
<a href="/docs" onClick={handleClick()}>Docs</a>
// right: passed
<a href="/docs" onClick={handleClick}>Docs</a>
// right, with an argument
<a href="/docs" onClick={() => handleClick('docs')}>Docs</a>3Verify
Reload the page. No log line at render time, and one log line per click.
grep -rnE "onClick=\{[A-Za-z_.]+\(" src/ | wc -lConfirmed by react.dev
Cause 2 of 4
A custom component receives onClick and does not use it
1Check
Built-in tags like <a> and <button> support the browser event names. A component you wrote can name its handler props anything, and its props do nothing until the component passes them to a real tag.
grep -rn "function NavLink\|const NavLink" src/2Fix
Take the prop in the component and put it on the DOM element.
function NavLink({ href, onClick, children }) {
return (
<a href={href} onClick={onClick}>
{children}
</a>
);
}3Verify
Click the link: the log line from the first step appears.
grep -rn -A6 "function NavLink" src/Confirmed by react.dev
Cause 3 of 4
A child inside the link stops the click
1Check
Events bubble up from the element that was clicked to its parents. A child handler that calls e.stopPropagation() keeps the handlers above it, including the anchor's, from running.
grep -rn "stopPropagation" src/2Fix
Remove stopPropagation from the child, or call the parent's work from the child's own handler.
// child: do not stop the event if the parent must see it
<button onClick={(e) => { doChildWork(); }}>Open</button>3Verify
Clicking the child now also runs the anchor's handler.
grep -rn "stopPropagation" src/ | wc -lConfirmed by react.dev
Cause 4 of 4
The link follows its href after your handler
1Check
A click on an anchor with an href navigates by default. preventDefault tells the browser that the event is handled and that its default action, such as link navigation, should not be taken.
grep -rn "<a " src/ | grep -n "href"2Fix
Call preventDefault in the handler when the click should only run code.
function handleClick(e) {
e.preventDefault();
// your code
}3Verify
Click the link: the code runs and the page stays where it is.
grep -rn -A3 "function handleClick" src/Confirmed by developer.mozilla.org
How do I stop it coming back?
- Write handlers as handleClick or an arrow function, and never put parentheses after a handler name in JSX unless you mean to call it.
- Use a <button> for actions that run code, and keep <a> for places to go, so there is no navigation to cancel.
- Forward onClick to the DOM element in every wrapper component you write.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
ESLint says an anchor needs a valid, navigable href in React
jsx-a11y/anchor-is-valid
The rule wants every <a> to be a real link.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix