Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IconButton onClick event Handler to Documentation #1340

Closed
eguneys opened this issue Aug 5, 2015 · 14 comments
Closed

Add IconButton onClick event Handler to Documentation #1340

eguneys opened this issue Aug 5, 2015 · 14 comments
Labels
package: icons Specific to @mui/icons

Comments

@eguneys
Copy link

eguneys commented Aug 5, 2015

IconButton onClick event handler took me a couple of hours to find out.

@corvuscornix
Copy link
Contributor

onClick should work

@eguneys eguneys changed the title This Project SUCKS and How ICONBUTTON Works! Add IconButton onClick event Handler to Documentation Aug 5, 2015
@shaurya947
Copy link
Contributor

@eguneys shouldn't you rather be using onTouchTap?

Either way, would you like to submit a PR adding the event to the doc? Should only take a couple minutes :)

@estaub
Copy link
Contributor

estaub commented Mar 19, 2016

Consider reopening; I believe this got lost somehow. The doc at http://www.material-ui.com/#/components/icon-button still does not contain any info on click handling (including onTouchTap). And it took me over an hour, too.

@mbrookes
Copy link
Member

#3172

@LeoPovoa
Copy link

LeoPovoa commented Sep 21, 2016

Hi, I can't handle the click event on IconButton. What's wrong in my code ??? Icon Button is in a toolbar element.

handleToggle() {
window.console.log("Click!");
}

       <ToolbarGroup firstChild={true}>
       <IconButton 
          onClick={this.handleToggle}
          iconStyle={stylesI.smallIcon}
          style={stylesI.small} >
          <ImageDehaze />
       </IconButton>
      </ToolbarGroup>
    </Toolbar>

@estaub
Copy link
Contributor

estaub commented Sep 26, 2016

@LeoPovoa "this" is not bound. Try it with function that's not a class member; if it works, that's your problem. You need to bind your handler to "this". http://stackoverflow.com/questions/29732015/value-of-this-in-react-event-handler

@Paelsis
Copy link

Paelsis commented Aug 18, 2017

I have bound the callback function to this in constructor and it still does not work.
When I replace IconButton tag with normal button tag and onClick={...} it works as expected !

@estaub
Copy link
Contributor

estaub commented Aug 18, 2017

@Paelsis Everything changed in this area 4 days ago, when v.19 came out.
See #7605 .
I haven't used v.19 yet, but there's a good chance it will fix your problem.

For pre-v.19:

Be sure to use doc that corresponds to the version you're using; there's a version dropdown at the top of the left navbar on the docs. onTouchTap was replaced with onClick in v.19.

I don't have time to chase this, but looking at my code I'm seeing that I use onTouchTap on all IconButtons, and I know that works; note that it is documented and onClick is not. Consider trying it. If you haven't used it before, be sure to have the react-tap-event-plugin installed and initialized; see http://www.material-ui.com/v0.18.7/#/get-started/installation

@Paelsis
Copy link

Paelsis commented Aug 21, 2017 via email

@Konstrictorman
Copy link

I have bound the callback function to this in constructor and it still does not work.
When I replace IconButton tag with normal button tag and onClick={...} it works as expected !

Did you solved this?. Please tell me how... I'm having the same exact situation

@myamyamya
Copy link

myamyamya commented Aug 29, 2020

I did it like this and it worked pretty well:
<IconButton onClick={() => {myFunction();}}>...</IconButton>

@shahjigar556
Copy link

<SearchIcon style={{color:'white'}} />
can I add onClick Funtionality to this searchIcon

@brendanhealey
Copy link

IconButton onClick event handler took me a couple of hours to find out.

And for the benefit of all the other people who have just spent an hour trying to find out this information it's:

event: React.MouseEvent<HTMLButtonElement, MouseEvent>

@zannager zannager added the package: icons Specific to @mui/icons label Dec 20, 2022
@dizonb
Copy link

dizonb commented Mar 5, 2024

Wrap any MUI component over your IconButton. In my case I'm using Box component.
I tried it and it worked!

<Box onClick={handleClick}>
            <IconButton sx={{ p: '10px' }} aria-label="menu" color="primary">
              {customIcon}
            </IconButton>
</Box>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: icons Specific to @mui/icons
Projects
None yet
Development

No branches or pull requests