React Interview Questions - Controlled vs Uncontrolled

React Interview Questions - Controlled vs Uncontrolled

React Interview Questions

Controlled vs Uncontrolled Components in React

1. What is a Controlled Component in React?
A controlled component is a form element whose value is controlled by React state. Example: const [name,setName] = useState(""); <input value={name} onChange={(e)=>setName(e.target.value)} />
2. What is an Uncontrolled Component in React?
An uncontrolled component stores form data in the DOM instead of React state. Example: const inputRef = useRef(); <input ref={inputRef} />
3. What is the difference between Controlled and Uncontrolled components?
Controlled components use React state while uncontrolled components use DOM references. Controlled components are easier for validation and form handling.
4. When should you use Controlled Components?
Use controlled components when you need validation, dynamic UI updates, or form control logic.
5. When should you use Uncontrolled Components?
Use uncontrolled components for simple forms where React state management is not necessary.

Comments

Popular posts from this blog

What is the closure function in Javascrpt(javascrpt)

gg