Custom Hooks in React
15 questions · 12 min · Medium
Test your knowledge of forms and controlled components in React, including form elements, state management, event handling, controlled inputs, form submission, validation, and select and checkbox controls.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. A form element whose value is controlled by React state
In a controlled component, React state is the source of truth for the value of a form element.
A. useState
useState is commonly used to store and update the current value of a controlled form input.
A. value
The value prop connects the input's displayed value to React state.
A. onChange
The onChange event handler can read the new input value and update the corresponding React state.
A. event.target.value
For a text input, event.target.value provides the current value of the input element.
A. onSubmit
The onSubmit prop can be placed on a form element to handle its submission.
A. event.preventDefault()
Calling event.preventDefault() prevents the browser's default action, which is commonly used to handle form submission entirely within React.
A. <input value={name} onChange={handleChange} />
A controlled input typically receives its value from state through value and updates that state through an onChange handler.
A. The input can become effectively read-only
If the value is controlled by state and that state is never updated, the displayed value cannot change through normal user input.
A. Using the checked prop and onChange
Checkboxes are commonly controlled with a boolean state connected to the checked prop and updated through onChange.
A. event.target.checked
For checkbox inputs, event.target.checked provides a boolean indicating whether the checkbox is currently checked.
A. By using the value prop and onChange
A controlled select element uses value to specify the selected option and onChange to update the state.
A. React has direct control over the form data
Controlled components keep form values in React state, making the current form data available to the component for validation and other logic.
A. A form element whose current value is primarily managed by the DOM
In an uncontrolled component, the DOM maintains the form element's current value, and React can access it using mechanisms such as refs.
A. defaultValue
The defaultValue prop specifies the initial value of an uncontrolled input without making React state its ongoing source of truth.