Skip to content

Instantly share code, notes, and snippets.

@amankkg
Created July 6, 2018 21:52
Show Gist options
  • Save amankkg/5b22295a9eba87771346bd43601c7032 to your computer and use it in GitHub Desktop.
Save amankkg/5b22295a9eba87771346bd43601c7032 to your computer and use it in GitHub Desktop.
simple form and controlled input
class Form extends React.Component {
state = { value: '' }
onChange = e => this.setState({ value: e.currentTarget.value })
onSubmit = () => console.log('The value is ', this.state.value)
render() {
return (
<form onSubmit={this.onSubmit}>
<label>
Value: <input value={this.state.value} onChange={this.onChange} />
</label>
<br />
{this.state.value.length > 10 && <p>hey, try to keep in short</p>}
<button type="submit">Submit</button>
</form>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment