question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing functions to onClick

See original GitHub issue

I have created a dropdown meny with an Image acting like the trigger like so:

    <Image
          alt="Plus"
          aria-controls="long-menu"
          className={classes.plus}
          src={Plus}
          {...bindTrigger(popupState)}
          // onClick={handleClick}
        />

I want to pass a function called handleClick to onClick, but if I remove the comment then it overrides the onClick function generated by bindtrigger. I wanted to ask if there is a way to pass handleClick to the generated onClick?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
jedwards1211commented, Jan 18, 2021

Yeah currently there isn’t a convenient way to handle this.

If you need to preventDefault or stopPropagation, there might be a nearby ancestor element you could do so on. Otherwise, right now you’ll have to do

onClick={e => {
  // perform additional action...and then:
  bindTrigger(popupState).onClick(e)
}}

If you just need to perform an additional effect when the popup is opened, and you’re using a functional component, I would recommend performing an effect when popupState.isOpen changes:

React.useEffect(() => {
  if (popupState.isOpen) {
    // popup was just opened, do something
    // popupState.anchorEl is the element that was clicked
  }
}, [popupState.isOpen])

This is better since if you later need to add additional ways the popup can be opened, all of them will trigger this effect, whereas an onClick handler wouldn’t respond to the new ways of opening the popup.

There are several ways I could make the API support this, none I’m 100% happy with, each one is awkward in some way. Also, does the popup get opened before or after your onClick handler? Some people might want before, some after, and adding an option to control that would make these examples even more bulky. At least if you do the wrapping yourself, as in my example above, you have complete freedom to control this.

Possibility 1
{bindTrigger(
  popupState,
  <Image
    alt="Plus"
    onClick={handleClick}
  />
)}
Possibility 2
<Image
  alt="Plus"
  {...bindTrigger(popupState, {
    onClick: handleClick,   
  })}
/>
Possibility 3
// at root level:
const TriggerImage = createTrigger(Image)

// somewhere later:
<TriggerImage
  popupState={popupState}
  alt="Plus"
  onClick={handleClick}
/>
0reactions
jedwards1211commented, Dec 13, 2021

Actually let me think about it some more. i guess we could store the previous username onClick in a ref in the popup state and use that to memoize. But I’d need to make the <PopupState> component use hooks internally and drop support for React pre-hooks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript pass function to onclick event - Stack Overflow
Passing the function to string doesn't work. So my question is: how to pass a function handle to onclick event and get a...
Read more >
Pass a Parameter Through onClick in React - Upmostly
Pass a Button's Value as a Parameter Through the onClick Event Handler ... You might want to pass in the value or name...
Read more >
Pass event and parameter onClick in React | bobbyhadz
To pass an event and parameter onClick in React, pass an inline function to the `onClick` prop of the element. The function should...
Read more >
Pass parameter to JavaScript function onclick in HTML
It's very easy to Pass parameter to JavaScript function using the onClick() function. If sending a string value then use double” ” or...
Read more >
Passing Functions to Components - React
With React, typically you only need to bind the methods you pass to other components. For example, <button onClick={this.handleClick}> passes this.handleClick ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found