Skip to content

Instantly share code, notes, and snippets.

@robrichard
Created March 20, 2017 19:47
Show Gist options
  • Save robrichard/fbd91bd246d2fbe5ae479651613881dc to your computer and use it in GitHub Desktop.
Save robrichard/fbd91bd246d2fbe5ae479651613881dc to your computer and use it in GitHub Desktop.
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {
input: {
email: "",
name: ""
}
};
}
handleInputChange(newPartialInput) {
this.setState(state => ({
...state,
input: {
...state.input,
...newPartialInput
}
}))
}
render() {
const {input} = this.state;
return (
<form onSubmit={() => /* save your form here */}>
<div>
<input
name="email"
placeholder="email"
value={input.email}
onChange={e => this.handleInputChange({email: e.target.value})}
/>
</div>
<div>
<input
name="name"
placeholder="name"
value={input.name}
onChange={e => this.handleInputChange({name: e.target.value})}
/>
</div>
<div>
<button type="submit">Submit</button>
</div>
</form>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment