How can I prevent href from firing using React onClick?

Daniel Krichevsky

trying to make a link element (<a> element) with href. Why am I using href and not changing history? because I want the option to open in a new tab in browser.

The Problem is, I have a button inside the element, and when you click on it, it triggers the href immediately, before even getting to stopProppagation or preventDefault inside the onClick handler of the button. It’s like 2 different event types…

something like this:

...
const onButtonClick = (event) => {
    event.stopPropagation();
    event.nativeEvent.stopImmediatePropagation();
    event.preventDefault();
    if (clickDisabled) return;
    if (!onClick) return;
    onClick(event);
}

return (
    <a href={someHref}>
        <button onClick={onButtonClick}>open menu</button>
    </a>
)

Any ideas?

Daniel Krichevsky

The workaround I ended up using: instead of the button onClick - I used onMouseDown which happens before the href trigger. Not the best solution, but was good enough in my case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I prevent href redirect onclick

Prevent onclick from firing

How can I prevent PrimeNG Tree onNodeSelect() from firing when using templating?

How can I prevent laravel from firing events inside of tests?

How i can prevent parent li's from not firing

React how to prevent a parent's onclick event from firing when a child is clicked?

How do I prevent a parent's onclick event from firing when a child anchor is clicked?

How to prevent nested mapped elements from firing onClick event twice

How can I prevent an onclick JavaScript from running on page load?

React prevent onClick firing when onFocus fires

How can I prevent the change event from firing when setting state in Bootstrap Switch?

How can i prevent useEffect() from firing up the first time but listening for a state change?

How can I disable HREF if onclick is executed?

Prevent onClick from firing if another element is on top

How do I get the onclick text from <a href onclick="sometext" in Java using selenium

How can you prevent a click from firing at the end of a long press?

How can I prevent React from unmounting/remounting a component?

How can I prevent the modal window from closing in React?

How can I get href links from HTML using Python?

How can i extract href from this html using selenium?

React - How can I render a component within a class by using OnClick?

How can I test an onClick line of a React component using Jest?

How can I handle a onClick using Action/Flux [JS/REACT]

How can I render different component using onClick() function in React?

How can I return a React Component from a function and render it onClick?

How can I prevent bundling from using files not included in project?

How can I prevent the other user from using the D drive

How can I prevent devs from using a global?

How can I prevent a button from clicking a second time when it already has an Onclick event in C#