1.0.1 • Published 7 years ago

memoized-change-handler v1.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

memoized-change-handler

npm npm

Helper method designed to simplify creation of React's onChange event handlers.

How to use

Include the npm package in your project with npm install --save memoized-change-handler.

To use in a React component, import the generator function and pass the component as an argument. This gives the generator access to setState. Call the returned function with the desired component state key to generate the memoized event handler.

An example:

import changeHandler from 'memoized-change-handler'

class BasicForm extends React.Component {

  constuctor(props) {
    super(props)
    this.handleChange = changeHandler(this)
  }

  render() {
    return (
      <form onSubmit={this.props.submit}>
        <label>Name
          <input type='text'
            onChange={this.handleChange('name')}
            value={this.state.name}/>
        </label>
        <label>Phone
          <input type='text'
            onChange={this.handleChange('phone')}
            value={this.state.phone}/>
        </label>
        <button>Submit</button>
      </form>
    )
  }
}

Issues & Feature Requests

Please raise any issues or feature requests here on GitHub. Hope you enjoy!