|
| 1 | +# 100 React Questions (Controlled & Uncontrolled Components) |
| 2 | + |
| 3 | +## Q1. What are Controlled Components in React? `V. IMP.` |
| 4 | + |
| 5 | +A controlled component is a component whose form elements (like input fields or checkboxes) are `controlled by the state` of the application. |
| 6 | + |
| 7 | +```javascript |
| 8 | +const Controlled = () => { |
| 9 | + // State to store the input value |
| 10 | + const [inputValue, setInputValue] = useState(""); |
| 11 | + |
| 12 | + // Event handler for input changes |
| 13 | + const handleInputChange = (e) => { |
| 14 | + // Update the state with the new input value |
| 15 | + setInputValue(e.target.value); |
| 16 | + }; |
| 17 | + |
| 18 | + return ( |
| 19 | + <div> |
| 20 | + {/* Input controlled by the state */} |
| 21 | + <input type="text" value={inputValue} onChange={handleInputChange} placeholder="Type..." /> |
| 22 | + |
| 23 | + {/* Display the current state value */} |
| 24 | + <p>You typed: {inputValue}</p> |
| 25 | + </div> |
| 26 | + ); |
| 27 | +}; |
| 28 | + |
| 29 | +export default Controlled |
| 30 | +``` |
| 31 | + |
| 32 | +## Q2. What are the Differences between Controlled & Uncontrolled Components? `V. IMP.` |
| 33 | + |
| 34 | +| Feature | Controlled Components | Uncontrolled Components | |
| 35 | +|---|---|---| |
| 36 | +| Value management | Controlled by `React state` | Not controlled by React state | |
| 37 | +| State updates | Event handlers `update React state` | No explicit state update; values accessed directly from DOM | |
| 38 | +| Refs | Don't depend on useRef() | Commonly uses `useRef()` to access element values | |
| 39 | +| Re-rendering | `Re-renders` on state changes | Less re-rendering due to decoupled values | |
| 40 | +| Recommendation | A `Recommended` and standard practice for form handling 🏆 | Useful in certain scenarios but less common best practice | |
| 41 | + |
| 42 | + |
| 43 | +### Controlled Component |
| 44 | +```javascript |
| 45 | +const Controlled = () => { |
| 46 | + // State to store the input value |
| 47 | + const [inputValue, setInputValue] = useState(""); |
| 48 | + |
| 49 | + // Event handler for input changes |
| 50 | + const handleInputChange = (e) => { |
| 51 | + // Update the state with the new input value |
| 52 | + setInputValue(e.target.value); |
| 53 | + }; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + {/* Input controlled by the state */} |
| 58 | + <input type="text" value={inputValue} onChange={handleInputChange} placeholder="Type..." /> |
| 59 | + |
| 60 | + {/* Display the current state value */} |
| 61 | + <p>You typed: {inputValue}</p> |
| 62 | + </div> |
| 63 | + ); |
| 64 | +}; |
| 65 | + |
| 66 | +export default Controlled |
| 67 | +``` |
| 68 | + |
| 69 | +### Uncontrolled Component |
| 70 | +```javascript |
| 71 | +const Uncontrolled = () => { |
| 72 | + // Create a ref to access the input value |
| 73 | + const inputRef = useRef(null); |
| 74 | + |
| 75 | + const handleClick = () => { |
| 76 | + // Access the input value directly using ref |
| 77 | + const value = inputRef.current.value; |
| 78 | + alert(`You typed: ${value}`); |
| 79 | + }; |
| 80 | + |
| 81 | + return ( |
| 82 | + <div> |
| 83 | + {/* Uncontrolled input with ref */} |
| 84 | + <input type="text" ref={inputRef} placeholder="Type something..." /> |
| 85 | + |
| 86 | + <button onClick={handleClick}>Click</button> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export default Uncontrolled |
| 92 | +``` |
| 93 | + |
| 94 | +## Q3. What are characteristics of controlled components? |
| 95 | + |
| 96 | +* **State Control**: The value of the form element is stored in the component's state. |
| 97 | + |
| 98 | +* **Event Handling**: Changes to the form element trigger an event (e.g., onChange for input fields). |
| 99 | + |
| 100 | +* **State Update**: The event handler updates the component's state with the new value of the form element. |
| 101 | + |
| 102 | +* **Re-rendering**: The component re-renders with the updated state, and the form element reflects the new value. |
| 103 | + |
| 104 | +```javascript |
| 105 | +const Controlled = () => { |
| 106 | + // State to store the input value |
| 107 | + const [inputValue, setInputValue] = useState(""); |
| 108 | + |
| 109 | + // Event handler for input changes |
| 110 | + const handleInputChange = (e) => { |
| 111 | + // State update |
| 112 | + setInputValue(e.target.value); |
| 113 | + }; |
| 114 | + |
| 115 | + return ( |
| 116 | + <div> |
| 117 | + {/* Event Handling */} |
| 118 | + <input type="text" value={inputValue} onChange={handleInputChange} placeholder="Type..." /> |
| 119 | + |
| 120 | + {/* Display the current state value */} |
| 121 | + <p>You typed: {inputValue}</p> |
| 122 | + </div> |
| 123 | + ); |
| 124 | +}; |
| 125 | + |
| 126 | +export default Controlled |
| 127 | +``` |
| 128 | + |
| 129 | +## Q4. What are the advantages of using controlled components in React forms? |
| 130 | + |
| 131 | +* Single Source of Truth: Form element values are managed by React state, ensuring data consistency. |
| 132 | + |
| 133 | +* Predictable Updates: Facilitates form validation, dynamic rendering, and seamless integration with React's lifecycle methods. |
| 134 | + |
| 135 | +* Better Control and Maintainability: Offers superior control and manageability compared to uncontrolled components, making them the preferred approach for React form handling. |
| 136 | + |
| 137 | +## Q5. How to handle forms in React? |
| 138 | + |
| 139 | +The preferred and recommended approach for handling forms in React is `by using controlled components`. |
| 140 | + |
| 141 | +## Q6. How can you handle multiple input fields in a controlled form? |
| 142 | + |
| 143 | +Maintain `seperate state variables` for each input and update them individually using the `onChange event`. |
| 144 | + |
| 145 | +## Q7. How do you handle form validation in a controlled component? |
| 146 | + |
| 147 | +By using conditional rendering based on the state and validate input values before updating the state. |
| 148 | + |
| 149 | +## Q8. In what scenarios might using uncontrolled components be advantageous? |
| 150 | + |
| 151 | +Uncontrolled components can be beneficial when integrating with non-React libraries, or when dealing with forms where controlled components are not possible. |
| 152 | + |
| 153 | +<!--- |
| 154 | +Adarsh |
| 155 | +3rd August 2024 |
| 156 | +05:22 AM |
| 157 | +(19:50) |
| 158 | +---> |
0 commit comments