Evhz
Evhz

Reputation: 9284

How to retrigger a js event cancelled with preventdefault()

Having the following React code:

// foo element
<NavLink onClick={(e) => this.sectionSelected(e)} />

//method invoqued
sectionSelected(e) {
    e.preventDefault();

    // foo actions   
    // this.setState( ... )

    // trigger the click again
    // e.retrigger() 
}

How can I retrigger the click that I cancelled using preventDefault ?

Looking for an answer not involving jquery.

Upvotes: 1

Views: 364

Answers (1)

Marc Davies
Marc Davies

Reputation: 266

I don't think there is a way to suspend default event behaviour in the way you want.

You could wrap the NavLink to handle the click AND follow the default behaviour...

const NavLinkWithOnClick = ({
 clickHandler,
 to
}) => (
  <div onClick={clickHandler}>
   <NavLink to={to} />
  </div>
)

...

<NavLinkWithOnClick clickHandler={sectionSelected] to="/wherever" />

Upvotes: 1

Related Questions