Passing functions to onClick
See original GitHub issueI 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:
- Created 3 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah currently there isn’t a convenient way to handle this.
If you need to
preventDefault
orstopPropagation
, there might be a nearby ancestor element you could do so on. Otherwise, right now you’ll have to doIf 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: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
Possibility 2
Possibility 3
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.