Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a link clickable in React.Js inside render?

How to make a link clickable in React.Js inside render? If I click the link it goes to home page. I want to follow the link only. my code looks like this

              <tr>
                <td>IPFS Hash # stored on Eth Contract</td>

                <td><a href= "#">{"https://gateway.ipfs.io/ipfs/"+this.state.ipfsHash}</a></td>
              </tr>
like image 966
shantanu rahut Avatar asked Dec 07 '18 16:12

shantanu rahut


People also ask

How do I make a clickable link in React?

To add the link in the menu, use the <NavLink /> component by react-router-dom . The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.

How do I link a URL in React JS?

js as follows. import React from 'react'; import ReactDOM from 'react-dom'; import { BrowserRouter, Switch, Route } from 'react-router-dom'; import Home from './Home'; import Contact from './Contact'; import Header from './App'; We will use the render function of ReactDOM imported in line 2.

How do you give an external link in React JS?

To add an external link with React Router, we can set the to prop to an object with the pathname property set to the external URL we go to when the link is clicked. to set the to prop to { pathname: "https://example.com" } to go to https://example.com when we click on the link.


1 Answers

The React way to add a click and redirect is to use the Link to provided by the react-router-

Inside the component

import {Link} from 'react-router-dom;
class Parent extends React.Component{
    render(){
         <div><Link to="/home">Click here to go back to home page</Link></div>
    } 
}

In route file

import React from 'react';
import {BrowserRouter as Router,Switch} from 'react-router-dom;
export class RoutingClass extends React.Component{
    render(){
        <Router>
            <Switch>
                <Route exact path="/home" component={Home} />
            </Switch>
        </Router>
    }
}
like image 116
Erick Avatar answered Sep 28 '22 17:09

Erick