Skip to content

Instantly share code, notes, and snippets.

@beardedtim
Created January 21, 2019 16:33
Show Gist options
  • Select an option

  • Save beardedtim/deb7e8486f50e5c26ebe72fa5840c22c to your computer and use it in GitHub Desktop.

Select an option

Save beardedtim/deb7e8486f50e5c26ebe72fa5840c22c to your computer and use it in GitHub Desktop.
A basic form in React
import React, { Component } from 'react'
class Form extends Component {
constructor(props) {
super(props)
this.onSubmit = this.onSubmit.bind(this)
this.onChange = this.onChange.bind(this)
this.state = {}
}
onSubmit(e) {
e.preventDefault()
console.log(this.state)
}
onChange(e) {
const { name, value } = e.target
this.setState(() => ({ [name]: value }))
}
render() {
return (
<form onSubmit={this.onSubmit}>
<input type="text" name="title" onChange={this.onChange} />
<input type="submit" value="Submit Form" />
</form>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment