Skip to content

Instantly share code, notes, and snippets.

@eddyw
Last active November 1, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddyw/a4a761cd071220d16129c1cf64f0951e to your computer and use it in GitHub Desktop.
Save eddyw/a4a761cd071220d16129c1cf64f0951e to your computer and use it in GitHub Desktop.
Suck Less at React: Understanding refs
import React from 'react'
import propTypes from 'prop-types'
import { render } from 'react-dom'
class Form extends React.Component {
static propTypes = {
onSubmit: propTypes.func.isRequired,
}
render() {
return (
<form onSubmit={this.props.onSubmit}>
<input name="example" type="text" />
</form>
)
}
}
class App extends React.Component {
constructor(...rest) {
super(...rest)
this.handleSubmit = this.handleSubmit.bind(this)
this.state = {
status: 'Not submitted!',
}
}
handleSubmit(e) {
e.preventDefault()
this.setState({
status: 'Form submitted!'
})
}
render() {
return [
<span key="0">{this.state.status}</span>,
<Form key="1" onSubmit={this.handleSubmit} />,
]
}
}
render(<App />, document.getElementById("root"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment