forked from kentcdodds/beginners-guide-to-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
21-controlled-forms.html
37 lines (33 loc) · 1.07 KB
/
21-controlled-forms.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@16.12.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.12.0/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.8.3/babel.js"></script>
<script type="text/babel">
function UsernameForm() {
const [username, setUsername] = React.useState('')
function handleSubmit(event) {
event.preventDefault()
alert(`You entered: ${username}`)
}
function handleChange(event) {
setUsername(event.target.value.toLowerCase())
}
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="usernameInput">Username:</label>
<input
id="usernameInput"
type="text"
onChange={handleChange}
value={username}
/>
</div>
<button type="submit">Submit</button>
</form>
)
}
ReactDOM.render(<UsernameForm />, document.getElementById('root'))
</script>
</body>