question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Link with another Component like Buttons

See original GitHub issue

Lets say I have a component called MyButton. Is there a quick way to attach link behaviour to it without writing an onClick handler for every button?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
gaearoncommented, May 10, 2015

I don’t like this approach but if you absolutely need this, you can set up a top-level document link handler like here:

document.onclick = event => {
  const { toElement: target } = event;

  if (!target) return;

  if (target.tagName !== 'A') return;

  const href = target.getAttribute('href');

  if (!href) return;

  const resolvedHref = url.resolve(window.location.href, href);
  const { host, path } = url.parse(resolvedHref);

  if (host === window.location.host) {
    event.preventDefault();
    router.transitionTo(path);
  }
};

However, in my opinion, it’s kinda strange to have some link components out of your control that nevertheless link to the internal routes in your app.

3reactions
ryanflorencecommented, Jun 18, 2015

we provide Link as a router-aware href, and mixins that have everything that link uses to work, so you can build whatever elements you want that interact with the router.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrapping a react-router Link in an html button - Stack Overflow
LinkButton component - a solution for React Router v4 ... First, a note about many other answers to this question. ⚠️ Nesting <button>...
Read more >
How to use a Button as a Link in React | bobbyhadz
To use a button as a link in React, wrap the button in an <a> tag or a Link component if using react...
Read more >
How to Use a Button as a Link in React - Coding Beauty
To use a button as a link in React, wrap the button in an anchor ( <a> ) element. Clicking a link button...
Read more >
How to use a Button component as a link in React
To correctly use a Button component as a link in React, override the underlying button HTML tag of the component and turn it...
Read more >
Using Link with another Component like Buttons #1176 - GitHub
Lets say I have a component called MyButton. Is there a quick way to attach link behaviour to it without writing an onClick...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found