Accepted answer

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




 const onlinkclick = (e) => {
    ---do your stuff---

<a href='/your-route' onclick={onlinkclick}> navigate </a>
<link to='/your-route' onclick={onlinkclick}> navigate </link>


i don't believe this is a good pattern to use in general. link will run your onclick event and then navigate to the route, so there will be a slight delay navigating to the new route. a better strategy is to navigate to the new route with the 'to' prop as you have done, and in the new component's componentdidmount() function you can fire your hello function or any other function. it will give you the same result, but with a much smoother transition between routes.

for context, i noticed this while updating my redux store with an onclick event on link like you have here, and it caused a ~.3 second blank-white-screen delay before mounting the new route's component. there was no api call involved, so i was surprised the delay was so big. however, if you're just console logging 'hello' the delay might not be noticeable.


you should use this:

<link to={this.props.myroute} onclick={hello}>here</link>

or (if method hello lays at this class):

<link to={this.props.myroute} onclick={this.hello}>here</link>

update: for es6 and latest if you want to bind some param with click method, you can use this:

    const somevalue = 'some';  
    <link to={this.props.myroute} onclick={() => hello(somevalue)}>here</link>

Related Query

More Query from same tag