Câu hỏi phỏng vấn Reactjs
Câu hỏi

What are Uncontrolled components?

Câu trả lời

Uncontrolled components in React are those where the form data is managed by the DOM itself, rather than by the React component state. This approach is akin to traditional HTML form inputs where the form data is handled directly by the DOM elements.

In uncontrolled components, instead of using React state to manage the input values, you use a ref to directly interact with the DOM node to get or set values. This means that the source of truth for the input values resides in the DOM, not in the React component state. This can be particularly useful when integrating React with non-React libraries or when you need a quick and dirty solution without much overhead.

Here are some key characteristics and practices related to uncontrolled components:

  1. State Management: The state of the form data in uncontrolled components is maintained by the DOM. React does not have control over the form element's state[1][2][3][4][5][6][7][8].

  2. Using Refs: To access the values of form elements in uncontrolled components, ref is used. A ref provides a way to access the actual DOM node, and you can interact with it directly. For example, to get the value of an input field, you would use inputRef.current.value where inputRef is the ref attached to the input element[1][2][3][4][5][6][7][8].

  3. Default Values: Uncontrolled components can use the defaultValue attribute to set an initial value for the input field, but subsequent updates to the value due to user interaction are not controlled by React. This is different from controlled com...

middle

middle

Gợi ý câu hỏi phỏng vấn

junior

When rendering a list what is a key and what is it's purpose?

middle

What is prop drilling and how can you avoid it?

senior

Does React re-render all components and sub components every time setState
is called?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào