Skip to content

Pass custom param to event handler. Avoid binding.

License

Notifications You must be signed in to change notification settings

sneas/react-event-param

Repository files navigation

react-event-param

version MIT License

Provide custom param to DOM event handler. And avoid memory-consuming bindings or arrow functions.

Installation

npm install --save react-event-param

Usage Example

import React, { Component } from "react";
import { setEventParam, getEventParam } from "react-event-param";

class List extends Component {
  state = {
    selectedIndex: null
  };

  onItemClick = e => {
    const index = getEventParam(e.target);
    this.setState({
      selectedIndex: index
    });
  };

  render() {
    return (
      <ul>
        {this.props.items.map((itemText, index) => (
          <li
            key={index}
            {...setEventParam(index)}
            onClick={this.onItemClick}
          >
            {{ itemText }}
          </li>
        ))}
      </ul>
    );
  }
}

export default List;