RF Flashcards

forms (49 cards)

1
Q

How is the form created in React?

A

Using useForm from react-hook-form and wrapping the form in FormProvider.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the schema of the form?

A

It’s a Zod object defining the shape and validation rules of the form data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you set default values for a form?

A

By passing a defaultValues object to useForm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are some methods available on the useForm object?

A

errors, control, setValue, watch, register, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a controlled input in React?

A

An input element where the value is controlled by React state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why do we prefer controlled inputs?

A

They provide better control and validation over the input’s value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why is validation important in forms?

A

It prevents invalid data from being submitted and ensures data integrity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When does onSubmit run in a form?

A

When the form is successfully submitted without validation errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When does onError run in a form?

A

When there are validation errors in the form submission.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of a toast notification in forms?

A

To provide feedback to the user, such as success or error messages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is data fetched and set in form dropdowns?

A

Using hooks to fetch data and setting options in state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the role of useEffect in forms?

A

To perform side effects such as data fetching or updating state when dependencies change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is validation integrated with react-hook-form?

A

Using a resolver like zodResolver to link Zod schema with useForm.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is FormProvider used for in React Hook Form?

A

To provide form context to nested components.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does setValue do in useFormContext?

A

It updates the value of a specific form field.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the role of Controller in forms?

A

It wraps custom inputs and manages their state and validation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How are validation errors displayed in forms?

A

Using the errors object from formState to conditionally render error messages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does the watch function do in useFormContext?

A

It monitors the value of a specific form field.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does a function like setDropdownOptions do in forms on the dropdown components?

A

It sets the options for a dropdown based on fetched data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How are nested components managed in forms?

A

By passing necessary props and using context providers like FormProvider.

21
Q

What does the register method do in React Hook Form?

A

It registers an input field to the form with its validation rules.

22
Q

What does the control object manage in React Hook Form?

A

It manages the state and validation of the form fields.

23
Q

How is setValue used in React Hook Form?

A

It updates the value of a specific field programmatically.

24
Q

What is the purpose of the watch method in React Hook Form?

A

To subscribe to field value changes and re-render the component when they change.

25
What does the handleSubmit method do in React Hook Form?
It handles the form submission and validation, calling onSubmit and onError.
26
What does the formState object include in React Hook Form?
It includes states like isDirty, isValid, errors, touchedFields, etc.
27
What is isDirty in React Hook Form?
A boolean indicating if any field in the form has been modified.
28
What is isTouched in React Hook Form?
A boolean indicating if a field has been interacted with.
29
What does isValid represent in React Hook Form?
A boolean indicating if the form passes all validation rules.
30
When does form validation trigger with onBlur mode in React Hook Form?
Validation triggers when an input field loses focus.
31
When does form validation trigger with onChange mode in React Hook Form?
Validation triggers as soon as the input value changes.
32
When does form validation trigger with onSubmit mode in React Hook Form?
Validation triggers when the form is submitted.
33
What is the reValidateMode in React Hook Form?
It determines when re-validation should occur after a field's validation fails.
34
What is the defaultValues option in React Hook Form?
It specifies the initial values of the form fields.
35
What does the errors object contain in React Hook Form?
It contains validation error messages for each field.
36
What does the clearErrors method do in React Hook Form?
It clears validation errors for specified fields.
37
What does the setError method do in React Hook Form?
It manually sets a validation error for a specific field.
38
What does the reset method do in React Hook Form?
It resets the form fields to their default values and clears errors.
39
What is dirtyTouchedValidate used for in forms?
It marks a field as touched and triggers validation. { shouldDirty: true, shouldTouch: true, shouldValidate: true };
40
Why is dirtyTouchedValidate important?
It ensures fields are validated and errors are shown after user interaction.
41
What is the validate function used for in form fields?
It provides custom validation logic for a specific field.
42
What does the trigger method do in React Hook Form?
It manually triggers validation for specific fields.
43
What is touchedFields in formState?
An object that tracks which fields have been interacted with.
44
How does getValues work in React Hook Form?
It retrieves the current values of the form fields.
45
What is errors used for in React Hook Form?
To access and display validation error messages for form fields.
46
What does mode: 'onBlur' do in useForm?
It sets the validation mode to trigger on input blur events.
47
What does mode: 'onChange' do in useForm?
It sets the validation mode to trigger on input change events.
48
What does mode: 'onSubmit' do in useForm?
It sets the validation mode to trigger on form submission.
49
How is resolver used in useForm?
To connect a validation library like Zod to handle form validation.