It is really frustrating to write many functions to handle events on multiple elements. Here we can use Javascript closure concept to avoid multiple functions. Observe below code

To Run Example Code

execute below commands
npm install
npm start
now open index.html in browser


Here commonFunction is handling click events on multiple elements by using Javascript closure concept
import React from 'react'

class ExampleApplication extends React.Component {

  constructor(props) {
    this.commonFunction = this.commonFunction.bind(this);
    this.state = {

  commonFunction(itemName) {
    const _this = this;
    return function() {
        message:'clicked on '+itemName+'....take some action'

  render() {
    return (
         Click on below list items<br/><br/>
           <li onClick={this.commonFunction('item 1')}>List item 1</li>
           <li onClick={this.commonFunction('item 2')}>List item 2</li>
           <li onClick={this.commonFunction('item 3')}>List item 3</li>
           <li onClick={this.commonFunction('item 4')}>List item 4</li>
           <li onClick={this.commonFunction('item 5')}>List item 5</li>


ExampleApplication.propTypes = {

ExampleApplication.defaultProps = {

export default ExampleApplication;



Follow this blog by Email

Popular Posts