react-form-controls Flashcards

1
Q

How do controlled components differ from uncontrolled components?

A

An input like <input></input> is uncontrolled. Even if you pass an initial value like <input></input>, your JSX only specifies the initial value. It does not control what the value should be right now.

To render a controlled input, pass the value prop to it (or checked for checkboxes and radios). React will force the input to always have the value you passed. Usually, you would do this by declaring a state variable:

In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by the DOM itself. To write an uncontrolled component, instead of writing an event handler for every state update, you can use a ref to get form values from the DOM.

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

What are some advantages of using uncontrolled components?

A

No component re-renders
Browser DOM handles the changes to the element
Simple to use
Keeps track of the internal state

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

What are some advantages of using controlled components?

A

The UI and the data are in sync
Form data can be passed between different components
The event handler and the value prop can be from the parent or a redux store
The React component acts as a source of truth for this component

When you use a controlled input, you set the state on every keystroke.

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

Which style do you prefer?

A

controlled

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

What two props must you pass to an input for it to be “controlled”?

A

value, onChange

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

What are some popular npm packages for creating forms in React?

A

React Hook Form, Formik, and React Final Form.

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