I am currently trying to redirect the user when an article of my website get clicked, however, I am getting the following error:
TypeError: Cannot read property 'props' of undefined onArticleClicked
13 |
14 | onArticleClicked() {
15 | // go to http://my-react-app.com/article/{articleID}
16 | this.props.history.push("/article/" + this.article.ID)
17 | }
18 |
19 | priceToDouble() {
There is my component:
import React from 'react'
import { Image } from 'react-bootstrap'
import { centimesToDouble } from '../../utils/price_converter';
export default class Article extends React.Component {
constructor(props) {
super(props);
this.state = {
article: props.article
}
}
onArticleClicked() {
// go to http://my-react-app.com/article/{articleID}
this.props.history.push("/article/" + this.article.ID)
}
priceToDouble() {
return centimesToDouble(this.state.article.price / 100) + "€"
}
render () {
return (
<div onClick={this.onArticleClicked}>
<Image src={this.state.article.variants[0].pictureUrl} responsive />
<h3>{this.state.article.name}</h3>
<p>{this.priceToDouble()}</p>
</div>
)
}
}
I don't get it because, based on other stackoverflow answers, I should be able to redirect like that, why can't I?
I am trying to learn reactjs through this project, feel free to give any advices, thank you