Last active
August 8, 2017 07:04
-
-
Save namelos/6f7cb5e06f8b2e82aa82 to your computer and use it in GitHub Desktop.
reusable react curried event handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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