Controlled vs Uncontrolled Components
In React, what is the main characteristic of a controlled input component?
- Its value is controlled by React state.
- It uses the DOM to manage its value.
- It cannot be validated in real-time.
- It updates only on form submit.
- It stores the value in a class property.
Reading Input Values
Which event would you typically use to read the current value of a text input in a React controlled component?
- onChange
- onClick
- onHover
- onBlur
- onInputted
Default Value Pitfall
In the following code, what is the issue?nnu003Cinput value={username} /u003E
- It will crash if 'username' is undefined causing an uncontrolled to controlled warning.
- It creates an uncontrolled component.
- The input will be read-only with no way to change the value.
- It does not allow for number input.
- It will trigger a memory leak in the component.
Form Submission Handling
What must you do to prevent a React form from causing a page reload when submitted?
- Call event.preventDefault() in the onSubmit handler.
- Set reload={false} in the u003Cformu003E element.
- Wrap the u003Cformu003E in a u003Cnosubmitu003E tag.
- Remove the type='submit' from all buttons.
- Use return false; at the end of your submit function.
Basic Field Validation
What is a proper way to validate that an email field is not empty before submission in React?
- Check if the email state variable is not an empty string.
- Use window.prompt to get email input.
- Set the input's required attribute to true.
- Only use HTML5 validation and not check in code.
- Render the input as type='password' instead.
Error State Display
How should you typically display error messages for invalid form input in React?
- Render error messages conditionally based on validation state.
- Use alert() every time an input is changed.
- Redirect the user away from the form.
- Display errors in the browser's title bar.
- Log errors to the console only.
Validation With useState
In a functional React component, how could you best ensure validation errors disappear immediately when a user fixes their input?
- Update the error state on every onChange event.
- Run validation only on form submit.
- Use a setTimeout to delay error removal.
- Rerender the component using forceUpdate.
- Only show errors after 3 failed attempts.
HTML5 Attributes And React
Which HTML attribute automatically provides client-side validation alongside React validation?
- required
- entry
- forceupdate
- skip
- readOnly
Handling Checkbox State
When creating a controlled checkbox in React, which property should you use to link it to state?
- checked
- value
- selected
- default
- visible
Form Libraries in React
Which library is specifically popular for advanced form management and validation in React?
- Formik
- Redux
- Lodash
- Jest
- DocumentJS