Complete the code to create a state variable for the input value using React hooks.
const [inputValue, [1]] = useState('');
In React, useState returns a pair: the current state and a function to update it. The convention is to name the update function starting with 'set'.
Complete the code to update the state when the input changes.
function handleChange(event) {
[1](event.target.value);
}The update function from useState is called with the new value to update the state.
Fix the error in the form submission handler to prevent page reload.
function handleSubmit(event) {
[1];
alert('Form submitted with: ' + inputValue);
}Calling event.preventDefault() stops the browser from reloading the page on form submit.
Fill both blanks to create a controlled input element with value and change handler.
<input type="text" value=[1] onChange=[2] />
A controlled input uses the state variable as its value and a handler function for onChange to update the state.
Fill all three blanks to complete a form component with state, change handler, and submit handler.
function Form() {
const [inputValue, [1]] = useState('');
function handleChange(event) {
[2](event.target.value);
}
function handleSubmit(event) {
[3];
alert('Submitted: ' + inputValue);
}
return (
<form onSubmit={handleSubmit}>
<input type="text" value={inputValue} onChange={handleChange} aria-label="Input field" />
<button type="submit">Submit</button>
</form>
);
}The state update function is used to update the input value. The submit handler calls event.preventDefault() to stop page reload.