43

I have a button. When the users presses it, I want the outcome ot be the same as if the user had pressed a specific link.

Put another way, I want to represent a link as a button.

I can see from this that it is possible using Jquery: How to call a href by onclick event of a button?

What is the "correct" way to do this in ReactJS?

2
  • You can possibly use style anchor (<a>) tags and have a link in form of href. No need of ReactJS to handle it. Commented Dec 10, 2016 at 21:30
  • Just style link as a button using CSS. Or use Bootstrap Commented Dec 10, 2016 at 21:34

6 Answers 6

49

Like this:

<button
    type="button"
    onClick={(e) => {
      e.preventDefault();
      window.location.href='http://google.com';
      }}
> Click here</button>
Sign up to request clarification or add additional context in comments.

2 Comments

Is adding e.preventDefault() necessary? It works fine without it as well. Could you please mention the purpose?
Can we still use this method and target a new tab?
21

Use React-Router Link,instead of Anchor tag.

import React, { Component } from 'react';
import {Link} from 'react-router-dom'
 // reactClass --
    return (
      <div>
      <Link to='https://react.semantic-ui.com/'>
      <button type="button" className="btn btn-info">Button</button>
      </Link>
      </div>
    );
  // end--

6 Comments

"Uncaught Error: You should not use <Link> outside a <Router>"
Whenever you try to show a Link on a page thats outside the BrowserRouter you will get that error. This error message is essentially saying that any component that is not a child of our <Router> cannot contain any React Router related components. You need to migrate your component hierarchy.
This is not openning a new tab neither overwrting the actual link showed at the address bar.
In this case, a classic <a href="react.semantic-ui.com" target="_blank"> will work.So to solve what you are trying to achieve, the easiest way is to add a <a> over your <Button>.
Nesting <a> and <button> is invalid HTML. See validation error
|
4

You don't even need jQuery nor React to do that. It's as simple as this:

var ex = document.getElementById('ex');
ex.onclick = function(){
  console.log('clicked');
}

var bt = document.getElementById('bt');
bt.onclick = function(){
  ex.click();
}
<button id="bt">Click</button>
<a id="ex">Triggerable link</a>

Of course, in React you can store the ref of the components, bind to the event and then trigger the event like this:

onClick(){
    this.ex.click();
}
render(){
    return (
        <div>
          <button id="bt" onClick={this.onClick.bind(this)} ref={(ref)=>{this.bt = ref}}>Click</button>
          <a id="ex" onClick={()=>console.log("clicked")} ref={(ref)=>{this.ex = ref}}>Triggerable link</a>
        </div>
    )
}

Edit: If you just want a button that behaves like a link, you can use this, or any of the solutions listed in this question:

onClick(){
    window.location.href="http://url.com";
}
render(){
    return (
        <button id="bt" onClick={this.onClick}>Click</button>
    )
}

5 Comments

can your ReactJS solution not have a separate link? I just want a button that results in the same thing as a link click, without there being a link.
Done. May I ask why exactly do you need it to be a button?
I get "Adjacent JSX elements must be wrapped in an enclosing tag" when using <button> and <a> tags in the example code under "Of course, in React you can..."
@DarylSpitzer my mistake, thanks for pointing it out. I wrapped the components in a <div>.
I would change one thing here. I would add a {display: "none"} style to the <a> tag in your refexample
4

2021 Update :

<div
    style={{
      width: 15,
      height: 15,
      background: "blue",
      cursor: "pointer",
    }}
    onClick={() => {
      window.open("http://google.com", "_blank");
    }}
  />

Comments

1

This worked for me :)

<a target="_blank" href="http://google.com"> <button id="bt" >Click</button> </a>

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You can use the following code :

  import { useNavigate } from "react-router-dom";
    const handleClick = (url)=>{
       const navigate = useNavigate(); 
       navigate(url);
     }

The useNavigate hook returns a function that lets you navigate to a specific link

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.