How to put an onClick event on an font awesome icon in react? How to put an onClick event on an font awesome icon in react? reactjs reactjs

How to put an onClick event on an font awesome icon in react?


Here you can look into react docs which explains to use className instead of class. Also wrap your <i> tag in button tag and use onClick() function there.


Well it should work like below...

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"><i class="fa fa-cog" onClick="console.log('Clicked')"></i>

If its not try to wrap your icon into a button

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"><button><i class="fa fa-cog" onClick="console.log('Clicked')"></i></button>


If you're using react-fontawesome, you can use the FontAwesomeIcon component, and set both an id and an onClick:

<FontAwesomeIcon id={yourIdGoesHere} icon={faEdit}  onClick={this.editItem} />...editItem = event => {  let idOfClickedIcon = event.currentTarget.id  ...do something with the item associated with the id}