How to trigger click event in ReactJS


ReactJS basically is an open-source, purely client-side JavaScript library which is used for building composable user interfaces specifically for single page applications.

In this article, we are going to trigger the click event with ReactJS “Refs

Use “refs” to get the element in the callback function and trigger a click using “click()” function.

<div id="app"></div>

class Example extends React.Component{
  TriggerClick(e) {
    e.click()
  }
  render(){
    return <div className="UFIInputContainer"
    ref={this.TriggerClick} onClick={()=> console.log('clicked')}>
      hello
      </div>
  }
}

ReactDOM.render(<Example/>, document.getElementById('app'))

Image by storyset on Freepik