Tomek Buszewski
Tomek Buszewski

Reputation: 7935

React Router and onEnter/onChange

I want to use onEnter and onChange from React Router, but I don't quite understand how. When I run simple functions from there, it works, like this:

<Route path="/tag/:slug" component={Archives} onChange={() => { console.log('awd'); }} />

But when I try to use a method from Archives component, it doesn't.

<Route path="/tag/:slug" component={Archives} onChange={this.method()} />

How can I use those methods?

Upvotes: 2

Views: 2759

Answers (1)

hellopath
hellopath

Reputation: 223

You need to bind your method with your scope.

There are 2 ways to bind.

First one, directly on your onChange

<Route path="/tag/:slug" component={Archives} onChange={this.method.bind(this)} />

or,

Second one

this.method = this.method.bind(this)

Upvotes: 1

Related Questions