Last active
July 4, 2019 14:32
-
-
Save craftdelivery/a78166d9bdc6987789148af87139ba69 to your computer and use it in GitHub Desktop.
React Form: Single Handler for Multiple Inputs
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 TextField from 'material-ui/TextField' | |
/* | |
Using Material Design: | |
- set id to state key | |
- use a single handler to update state for n inputs | |
- no need to use formik or redux forms every time | |
*/ | |
export default class extends Component { | |
state = { | |
email: undefined, | |
name: undefined, | |
pwd: undefined, | |
} | |
change = e => { | |
const { id, value } = e.target | |
this.setState({ [id]: value }) | |
} | |
render() { | |
const { email, name, pwd } = this.state | |
return ( | |
<div> | |
<TextField | |
hintText='name' | |
floatingLabelText={name} | |
floatingLabelFixed={true} | |
onChange={this.change} | |
id='name' | |
> | |
</TextField> | |
<TextField | |
hintText='password' | |
floatingLabelText={pwd} | |
floatingLabelFixed={true} | |
onChange={this.change} | |
id='pwd' | |
> | |
</TextField> | |
<TextField | |
hintText='email' | |
floatingLabelText={email} | |
floatingLabelFixed={true} | |
onChange={this.change} | |
id='email' | |
> | |
</TextField> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment