How to use onClick event on react Link component?

bier hier picture bier hier · Mar 15, 2017 · Viewed 170k times · Source

I am using the Link component from the reactjs router and I cannot get the onClickevent working. This is the code:

<Link to={this.props.myroute} onClick='hello()'>Here</Link>

Is this the way to do it or another way?

Answer

CodinCat picture CodinCat · Mar 15, 2017

You are passing hello() as a string, also hello() means execute hello immediately.

try

onClick={hello}