Reputation: 87
I want to make a button to link to a different app with react librery. my code looks like this
import React, { Component } from "react";
import { Link, NavLink, Redirect } from "react-router-dom";
import "./subportfolio.scss";
import Coffee1 from "../../assets/coffee-1.jpg";
import Coffee2 from "../../assets/coffee-2.jpg";
import Coffee3 from "../../assets/coffee-3.jpg";
import Coffee4 from "../../assets/coffee-4.jpg";
import CoffeeShop from "../../assets/coffee-shop-1.jpg";
import Tea1 from "../../assets/tea-1.jpg";
import Tea2 from "../../assets/tea-2.jpg";
import Tea3 from "../../assets/tea-3.jpg";
class SubPortfolio extends Component {
state = {
projectImg: {
project1: Coffee1,
project2: Coffee2,
project3: Coffee3,
project4: Coffee4,
project5: CoffeeShop,
project6: Tea1,
project7: Tea2,
project8: Tea3
},
direct: ""
};
directToGithub = () => {
this.props.history.go("https://github.com");
};
render() {
console.log(this.state.direct);
return (
<div className="subPortfolio">
<div className="subPortfolio__content">
<div className="subPortfolio__content--detail">
<h2>Image Format</h2>
<div className="subPortfolio__content--refer">
<div className="subPortfolio__content--button">
<button
id="github"
type="button"
onClick={this.directToGithub}
>
github
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default SubPortfolio;
I tried all of the history properties but all of them throw an error
TypeError: Cannot read property 'go' of undefined
SubPortfolio.directToGithub
C:/Users/Ermais Kidane/Documents/REACT/portfolio/src/components/subportfolio/subportfolio.js:30
27 | };
28 |
29 | directToGithub = () => {
> 30 | this.props.history.go("https://github.com");
| ^ 31 | };
32 | render() {
33 | // const projectsImage = Object.keys(this.state.projectImg).map(proImg => {
I tried using Link React-router-dom and to adds to with the previous url like (http://localhost:3000/https://github.com) and does not take make anywhere.
how could I make the link to another web site? thanks for your help.
Upvotes: 0
Views: 167
Reputation: 11
If you want to programmatically direct to an external website, you can also use window.location = "https://github.com"
or, assuming a global window
object, location = "https://github.com"
.
Upvotes: 1
Reputation: 520
If you are trying to get directed to an external website, just use the standard <a></a>
tag
<a href="https://www.github.com"><button id="github" type="button">github</button></a>
if you are trying to use the <Link>
to get directed to an internal link, it would look more like this
<Link to="/github"><button id="github" type="button">github</button></Link>
And the link would then have to be setup in the app.js using the router to then be directed to the named component
Upvotes: 2