router icon indicating copy to clipboard operation
router copied to clipboard

Link onClick preventDefault doesn't work

Open fnn opened this issue 2 years ago • 1 comments

Describe the bug

The usage of event.preventDefault() inside an onClick handler on the Link component will not prevent the navigation. This works with HTML anchors and react-location Link component.

Your Example Website or App

https://stackblitz.com/edit/tanstack-router-kqww9m?file=src/main.tsx

Steps to Reproduce the Bug or Issue

  1. Add onClick handler with preventDefault() to Link component
  2. Click link

Expected behavior

As a user I expected, that the page navigation will be prevented but it got triggered anyway.

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Browser: Chromium
  • Version: 104.0.5112.79

Additional context

No response

fnn avatar Nov 22 '22 12:11 fnn

On <Link /> elements try to use onClickCapture to prevent the event.

onClickCapture={(e) => {
  e.preventDefault();
}}

wizzymore avatar Nov 25 '22 14:11 wizzymore

This should be fixed on the latest version 👍

tannerlinsley avatar Dec 07 '22 23:12 tannerlinsley