<body>
<div id="root"></div>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/[email protected]/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) {
// making sure that the value is turned into lower case
// the input value is now handled with React
setUsername(event.target.value.toLowerCase());
}
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="usernameInput">Username:</label>
{/* controlling the value with value={username} */}
<input
id="usernameInput"
type="text"
onChange={handleChange}
value={username}
/>
</div>
<button type="submit">Submit</button>
</form>
);
}
ReactDOM.render(<UsernameForm />, document.getElementById('root'));
</script>
</body>