Q1. How do you validate forms in React?
Validation can be done on change, on blur, or on submit. In controlled components, you can check the state and display error messages. You can also use libraries like Formik or React Hook Form for complex validation.
Q2. What is client-side validation?
Client-side validation checks user input in the browser before submission. It improves user experience by providing instant feedback. However, you should always validate on the server as well for security.
Q3. How do you display validation errors?
You can maintain an errors state object and conditionally render error messages. For example, {errors.email && {errors.email}}. Update errors during onChange or onBlur.
Q4. What is debouncing in form validation?
Debouncing delays the validation until the user stops typing for a specified time. This prevents excessive validation on every keystroke and improves performance, especially for API calls (e.g., checking username availability).
Q5. Can you use HTML5 validation with React?
Yes, you can use HTML5 attributes like required, minLength, pattern. However, the default browser validation styling and behavior may be inconsistent. Many prefer custom validation for better control.
