Skip to content

Instantly share code, notes, and snippets.

@namelos
Last active August 8, 2017 07:04
Show Gist options
  • Save namelos/6f7cb5e06f8b2e82aa82 to your computer and use it in GitHub Desktop.
Save namelos/6f7cb5e06f8b2e82aa82 to your computer and use it in GitHub Desktop.
reusable react curried event handler
import React, { Component } from 'react';
import { render } from 'react-dom';
import { Map, List } from 'immutable';
class App extends Component {
constructor () {
super();
this.state = { data: Map({
name: 'init',
pass: 'init'
})}
}
handleClick = type => e =>
this.setState(({data}) => ({
data: data.set(type, this.refs[type].value
)}));
render = () => <div>
<input type="text" ref="name" />
<button onClick={ this.handleClick('name') }>name</button>
<input type="password" ref="pass"/>
<button onClick={ this.handleClick('pass') }>password</button>
<p>name: { this.state.data.get('name') }</p>
<p>password: { this.state.data.get('pass') }</p>
</div>
}
render(<App />,
document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment