flux-router-component icon indicating copy to clipboard operation
flux-router-component copied to clipboard

Is there a way to add other click handlers onto the NavLink?

Open tedlin182 opened this issue 10 years ago • 19 comments

I want to preserve the native behavior of NavLink but also want it to trigger a state change in a component via a custom handler I created.

When I add the onClick to the NavLink component it does direct to the new page but does a page refresh also.

Not sure if this has been addressed but let me know if there's a way to do that. Apologies if this is a noob question.

tedlin182 avatar Feb 06 '15 19:02 tedlin182

Did you preventDefault the click event? https://github.com/yahoo/flux-router-component/blob/master/lib/NavLink.js#L70

lingyan avatar Feb 07 '15 03:02 lingyan

Closing this issue. Feel free to reopen with the issue still exists.

lingyan avatar Feb 24 '15 05:02 lingyan

This issue still happens; if you call preventDefault() then it stops NavLink from completing execution

agrippanux avatar Mar 10 '15 22:03 agrippanux

Hi @lingyan

Yeah preventDefault prevents NavLink from executing. Is there a way to execute both it's intended behavior and additional handlers I attach to it?

tedlin182 avatar Mar 11 '15 15:03 tedlin182

@tedlin182 Sorry, for some reason my email app filtered some of my github emails into spam folder. So did now see your comment till now :(

As part of https://github.com/yahoo/flux-router-component/pull/81, we are planning to wrap the logic in NavLink's dispatchNavAction (https://github.com/yahoo/flux-router-component/blob/master/lib/NavLink.js#L58-L104) into a separate lib. So that you can provide your own onClick handler for NavLink, and use the lib for navigate behavior.

Let us know if it will be ok for you to wait for that PR, which we want to merge this week. Thanks!

lingyan avatar Mar 23 '15 23:03 lingyan

Sounds good. Thanks for the update and no worries on the lateness.

tedlin182 avatar Mar 24 '15 23:03 tedlin182

Reopening so I can track this as part of #81.

mridgway avatar Mar 26 '15 03:03 mridgway

+1 for this :)

longlho avatar Apr 01 '15 19:04 longlho

I bind my custom click handler this way

var EventListener = require('react/lib/EventListener');

componentDidMount: function() {
    var fooComponent = this.refs.foo;
    if (fooComponent) {
        EventListener.listen(fooComponent.getDOMNode(), 'click', this.barHandler);
    }
},
render: function () {
    return (
        <NavLink ref="foo" href={this.props.uri}>{this.props.foo}</NavLink>
    );
}

kaddopur avatar Apr 08 '15 09:04 kaddopur

#181 was closed but the changes required for this issue to be resolved weren't made.

I think what is needed is a prop to pass in a function that can be called after the NavLinks handler has been executed.

I don't want to override the click behaviour, but do something else (collapse an expanded navigation bar).

samkelleher avatar Jan 11 '16 20:01 samkelleher

Another workaround would be to wrap the content of the NavLink with a component with an onClick handler, i.e. <NavLink><div onClick={myClickHandler}>Some content</div></NavLink>

But I would also appreciate a simpler way to add onClick functionality to a NavLink.

abrudin avatar Jun 01 '16 11:06 abrudin

@abrudin is this working properly for you? We we're using this technique but the onClick event doesn't fire when you click the NavLink element using the keyboard to tab focus to it and press enter.

mettin avatar Aug 15 '16 13:08 mettin

@mettin Good point. No, the click handler is not invoked when using the keyboard (as the keyboard focuses on the a element instead of the div) - i guess you could add some kind of onkeydown handler on the navlink, but it is starting to look ugly indeed.

abrudin avatar Aug 16 '16 11:08 abrudin

Yes, it's quite annoying. We want to have some tracking on the NavLink but it's quite impossible now. (we can only track the pageload where we're going to now). I'll update this thread if/when we find a solution

mettin avatar Aug 16 '16 11:08 mettin

I did the change handling on the render method, but got this expected warning ...

Warning: setState(...): Cannot update during an existing state transition (such as within render or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to componentWillMount.

Going to see if there's another alternative.

trajano avatar Apr 03 '17 01:04 trajano

what about wrapping in another element and handling the onClick on it? <li onClick={function() {this.props.handleScreenNb('E705');}.bind(this)}> <NavLink exact activeClassName='active' to={config.baseUrl+'/'}>Etat civil / Domiciliation du salarié</NavLink> </li>

BillOTei avatar May 15 '17 08:05 BillOTei

Probably a bit late on this one but I found this result worked best for me:

<a href="javascript:void(0)" onClick={clickHandler} className = "nav-link">Your Label</a>

andrew-finlayson avatar Jun 21 '17 07:06 andrew-finlayson

<NavLink to='/' onClick={this.handleReset}>Reset</NavLink>
 <NavLink to='/features' onClick={this.handleNext}>
          {this.state.activeStep!==3?'Next':'Done'}
</NavLink>

In my case Both are running without giving any problem. Version-"react": "^16.8.5"

B4BIPIN avatar May 19 '19 08:05 B4BIPIN

@B4BIPIN I tried your solution, a simple onClick on the NavLink element, and I also tried inserting a div, like so:

<NavLink className="dropdown-item" to="/myarea" onClick={(e) => {
    alert('It's alive!')
}}>
    My Area
</NavLink>

It won't work with me. Anyone got any tips?

Malavos avatar Jun 26 '19 13:06 Malavos