Jordan England-Nelson
Jordan England-Nelson

Reputation: 615

Back button fires as soon as page loads

Here is the code for the back button I'm trying to include at the top of my page:

<a onClick={this.goBack()}>Go back</a>

And here is the helper function goBack():

  goBack(){
    return history.go(-1);
  }

I've also tried this, which produces the same result:

<a href={this.goBack()}>Go back</a>

I'm new to the history method, so any clarification would be much appreciated! Thanks

Upvotes: 0

Views: 49

Answers (2)

Ishey4
Ishey4

Reputation: 327

<a href="#" onclick="window.history.back()">Go back</a>

Upvotes: 0

TimoStaudinger
TimoStaudinger

Reputation: 42520

this.goBack() immediately executes function this.goBack.

Instead, only assign the function as an event handler by removing the brackets ():

<a onClick={this.goBack}>Go back</a>

Upvotes: 1

Related Questions