<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() {
// adding state to handle username, keeping it up to date
// updating the current state of the user's input
const [username, setUsername] = React.useState('');
// determine if it's lower case / displaying error message
const isLowerCase = username === username.toLowerCase();
const error = isLowerCase ? null : 'Username must be lower case';
function handleSubmit(event) {
event.preventDefault();
alert(`You entered: ${username}`);
}
function handleChange(event) {
setUsername(event.target.value);
}
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="usernameInput">Username:</label>
{/* Calling the function handleChange */}
<input id="usernameInput" type="text" onChange={handleChange} />
</div>
{/* displaying error message */}
<div style={{ color: 'red' }}>{error}</div>
{/* disable button boolean */}
<button disabled={Boolean(error)} type="submit">
Submit
</button>
</form>
);
}
ReactDOM.render(<UsernameForm />, document.getElementById('root'));
</script>
</body>