Nidhi Jagga’s Post

𝐑𝐞𝐚𝐜𝐭 𝐝𝐞𝐯𝐬 𝐚𝐫𝐞 𝐧𝐨𝐭𝐨𝐫𝐢𝐨𝐮𝐬 𝐜𝐨𝐧𝐭𝐫𝐨𝐥 𝐟𝐫𝐞𝐚𝐤𝐬. 𝐖𝐞 𝐝𝐨𝐧'𝐭 𝐞𝐯𝐞𝐧 𝐭𝐫𝐮𝐬𝐭 𝐚𝐧 𝐇𝐓𝐌𝐋 `<input>` 𝐭𝐚𝐠 𝐭𝐨 𝐫𝐞𝐦𝐞𝐦𝐛𝐞𝐫 𝐢𝐭𝐬 𝐨𝐰𝐧 𝐭𝐞𝐱𝐭. 🕵️♂️ When building forms in React, you have to answer one philosophical question: Who holds the keys to the state? 🎭 𝐓𝐡𝐞 𝐏𝐮𝐩𝐩𝐞𝐭 𝐌𝐚𝐬𝐭𝐞𝐫 (𝐂𝐨𝐧𝐭𝐫𝐨𝐥𝐥𝐞𝐝 𝐂𝐨𝐦𝐩𝐨𝐧𝐞𝐧𝐭𝐬) You want total control. You use `useState`, bind the `value` prop, and demand an `onChange` event for every single keystroke. React becomes the single source of truth. You use this when you need instant validation, conditional rendering, or real-time filtering. It’s powerful, but you are officially re-rendering the component every time the user breathes on the keyboard. 🗄️ 𝐓𝐡𝐞 𝐒𝐞𝐜𝐫𝐞𝐭 𝐊𝐞𝐞𝐩𝐞𝐫 (𝐔𝐧𝐜𝐨𝐧𝐭𝐫𝐨𝐥𝐥𝐞𝐝 𝐂𝐨𝐦𝐩𝐨𝐧𝐞𝐧𝐭𝐬) You let the DOM do its actual job. You slap a `useRef` on the input and say, "Hey, hold onto this data. I'll ask you for it when the user actually clicks Submit." React isn't watching every keystroke. It’s faster, simpler, and perfect for quick-and-dirty forms or integrating with non-React libraries. The golden rule? 𝐂𝐨𝐧𝐭𝐫𝐨𝐥𝐥𝐞𝐝 = 𝐑𝐞𝐚𝐜𝐭 𝐒𝐭𝐚𝐭𝐞 𝐢𝐬 𝐭𝐡𝐞 𝐭𝐫𝐮𝐭𝐡. 𝐔𝐧𝐜𝐨𝐧𝐭𝐫𝐨𝐥𝐥𝐞𝐝 = 𝐓𝐡𝐞 𝐃𝐎𝐌 𝐢𝐬 𝐭𝐡𝐞 𝐭𝐫𝐮𝐭𝐡 (𝐑𝐞𝐚𝐜𝐭 𝐣𝐮𝐬𝐭 𝐯𝐢𝐬𝐢𝐭𝐬). 𝐀𝐫𝐞 𝐲𝐨𝐮 𝐓𝐞𝐚𝐦 `useState` 𝐟𝐨𝐫 𝐚𝐛𝐬𝐨𝐥𝐮𝐭𝐞𝐥𝐲 𝐞𝐯𝐞𝐫𝐲𝐭𝐡𝐢𝐧𝐠, 𝐨𝐫 𝐝𝐨 𝐲𝐨𝐮 𝐬𝐧𝐞𝐚𝐤 𝐢𝐧 𝐚 `useRef` 𝐰𝐡𝐞𝐧 𝐲𝐨𝐮 𝐣𝐮𝐬𝐭 𝐝𝐨𝐧'𝐭 𝐟𝐞𝐞𝐥 𝐥𝐢𝐤𝐞 𝐰𝐫𝐢𝐭𝐢𝐧𝐠 𝐚𝐧𝐨𝐭𝐡𝐞𝐫 𝐨𝐧𝐂𝐡𝐚𝐧𝐠𝐞 𝐡𝐚𝐧𝐝𝐥𝐞𝐫? 𝐂𝐨𝐧𝐟𝐞𝐬𝐬 𝐛𝐞𝐥𝐨𝐰. 👇 #ReactJS #FrontendDevelopment #WebDev #SoftwareEngineering #ChaiCode #MERNStack #CodingHumor #TechTips

  • No alternative text description for this image

This is a classic React concept that trips up many developers early on. Controlled components give you full power over form state, which is essential for validation-heavy MERN apps. Knowing when to use useRef for uncontrolled inputs can also significantly simplify certain UI flows.

To view or add a comment, sign in

Explore content categories