Created
November 7, 2018 22:58
-
-
Save jvalinsky/d36300b6bb53f7654a5a6429437ea320 to your computer and use it in GitHub Desktop.
React User Input Example
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
<html> | |
<body> | |
<div id="root"> | |
</div> | |
<script src="./index.js"></script> | |
</body> | |
</html> |
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 ReactDOM from "react-dom"; | |
const HelloWorld = props => { | |
return <div> Hello {props.name} </div>; | |
}; | |
class NameInput extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { name: "" }; | |
this.handleChange = this.handleChange.bind(this); | |
} | |
handleChange(event) { | |
this.setState({ name: event.target.value }); | |
} | |
render() { | |
return ( | |
<div id="app"> | |
<HelloWorld name={this.state.name} /> | |
<input | |
type="text" | |
value={this.state.name} | |
onChange={this.handleChange} | |
/> | |
</div> | |
); | |
} | |
} | |
ReactDOM.render(<NameInput />, 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
{ | |
"name": "react-example", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"scripts": { | |
"start": "parcel index.html" | |
}, | |
"dependencies": { | |
"react": "^16.6.1", | |
"react-dom": "^16.6.1", | |
}, | |
"devDependencies": { | |
"parcel-bundler": "^1.10.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment