Created
October 30, 2020 14:26
-
-
Save uicoded/d6205eba8a5c5d86c4228fb64234c3c7 to your computer and use it in GitHub Desktop.
React Controlled Components (Class Syntax)
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
<h1>React Controlled Components (Class Syntax)</h1> | |
<div id="root"></div> | |
<p>Submit the form</p> | |
<p><a href="https://reactjs.org/docs/forms.html#controlled-components">Controlled Components - React Docs</a></p> |
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
class NameForm extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {value: ''}; | |
this.handleChange = this.handleChange.bind(this); | |
this.handleSubmit = this.handleSubmit.bind(this); | |
} | |
handleChange(event) { | |
this.setState({value: event.target.value}); | |
} | |
handleSubmit(event) { | |
alert('A name was submitted: ' + this.state.value); | |
event.preventDefault(); | |
} | |
render() { | |
return ( | |
<form onSubmit={this.handleSubmit}> | |
<label> | |
Name: | |
<input type="text" value={this.state.value} onChange={this.handleChange} /> | |
</label> | |
<input type="submit" value="Submit" /> | |
</form> | |
); | |
} | |
} | |
ReactDOM.render( | |
<NameForm />, | |
document.getElementById('root') | |
); |
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment