Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The onClick handler on a React <a> tag never runs, or the page jumps away

ErrorThe handler runs when the page renders, and nothing happens when the link is clicked

Short 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.

  • Applies to React 18, React 19, current desktop browsers
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
    }
  2. 2Match what you see, then jump to its fix

    1. 1The log line prints when the component renders, and not when you click.The handler is called instead of passedFix
    2. 2The 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. 3The 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. 4The 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

What does the error look like?

  • The handler runs when the page renders, and nothing happens when the link is clicked
  • Clicking the link does nothing, with no error in the console
  • After 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 -l

Confirmed by react.dev

Did this fix it?

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

Did this fix it?

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 -l

Confirmed by react.dev

Did this fix it?

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.