Last active
November 1, 2017 16:44
-
-
Save eddyw/a4a761cd071220d16129c1cf64f0951e to your computer and use it in GitHub Desktop.
Suck Less at React: Understanding refs
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 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