Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»How to Work with React Forms So They Don’t Break Your Brain

    How to Work with React Forms So They Don’t Break Your Brain

    July 7, 2025

    If you’ve ever built a form in React and felt like the input fields had a mind of their own, you’re not alone. One minute your form is working fine, the next you’re staring at a blank input that won’t update. Or React throws a warning like “A component is changing an uncontrolled input of type text to be controlled.” and you’re not even sure what that means.

    I didn’t really get it either until I realized that React doesn’t just read form inputs – it can own them. And whether you let React control your inputs or let the DOM handle them makes a real difference in how your form behaves.

    In this article, I’ll break down:

    • What controlled and uncontrolled components are

    • Why the difference matters

    • When to use each one

    • And how to avoid common beginner mistakes

    You’ll get real code, clear examples, and a no-nonsense guide to making React forms behave exactly how you want.

    Here’s what we’ll cover:

    • What Is a Controlled Component?

    • What Is an Uncontrolled Component?

    • Controlled vs Uncontrolled: What’s the Difference?

    • When to Use Controlled vs Uncontrolled Components

      • Use Controlled Components When:

      • Use Uncontrolled Components When:

    • Conclusion

    What Is a Controlled Component?

    A controlled component in React is an input (like a text box or dropdown) where React keeps track of the value.

    Instead of the browser handling the input on its own, you use React state to tell the input what to show, and update that state when the user types. Basically, for every keystroke the state updates and the component re-renders.

    Here’s a basic example:

    import { useState } from "react";
    
    function NameForm() {
      const [name, setName] = useState("");
    
      return (
        <input
          type="text"
          value={name} //Whatever the state is, that is what the value of the input field will be
          onChange={(e) => setName(e.target.value)} //When you type, this function runs and updates the state
        />
      );
    }
    

    React re-renders with the new value, keeping the UI and the data in sync. You’re always in control of what’s in that field.

    Why use this approach?

    • You always know the current value.

    • It’s easy to validate, reset, or change the input from code.

    • It’s the standard approach in most React apps.

    What Is an Uncontrolled Component?

    An uncontrolled component is the opposite of what we just looked at. Instead of using React state to manage the input, you let the browser handle it on its own, like a regular HTML form.

    To get the value, you use something called a ref (short for “reference”) to reach into the DOM and grab it when you need it.

    In React, refs are created using a built-in hook called useRef. This hook lets you create a reference to a DOM element (like an <input> ), so you can directly access its current value whenever you need it (for example, when a form is submitted).

    Unlike useState, which tracks changes and causes re-renders, useRef simply gives you a way to “point to” or “reach into” an element in the DOM without triggering re-renders. It’s useful when you don’t want React to manage the input’s state, but you still need to read its value later.

    Here’s what that looks like:

    import { useRef } from "react";
    
    function NameForm() {
      const inputRef = useRef();
    
      const handleSubmit = () => {
        alert(inputRef.current.value); //displays the value of the input element
      };
    
      return (
        <>
          <input type="text" ref={inputRef} />; //gives you direct access to the input element
          <button onClick={handleSubmit}>Submit</button> //
        </>
      );
    }
    

    React isn’t involved in tracking every keystroke. It only checks the value when you ask for it and the input keeps track of it’s value on it’s own.

    Why use this?

    • It’s simpler for quick forms where you only need the value at the end (like on submit).

    • It avoids re-renders while typing, which can be useful in performance-sensitive apps.

    But: it’s harder to do things like validation, real-time updates, or syncing with other parts of your app.

    Controlled vs Uncontrolled: What’s the Difference?

    Now that you’ve seen both, let’s make the differences crystal clear.

    Controlled Components Uncontrolled Components
    React is in charge. The browser is in charge.
    You use useState to store the value. You use useRef to access the value.
    You update the value with onChange. The input keeps its own value.
    React re-renders the input every time the value changes. You access it using a ref only when you need it.

    Think of a controlled component like a parent carefully tracking what their kid is writing in a notebook, checking every word as it’s written.

    An uncontrolled component is more like letting the kid write freely and just reading what they wrote at the end.

    When to Use Controlled vs Uncontrolled Components

    Both controlled and uncontrolled components have their place. The key is knowing when each one makes sense for your project and what you want to achieve.

    Use Controlled Components When:

    • You need to validate input while the user types.
      Example: Show an error if the user leaves a field empty.

    • You want to enable/disable buttons based on input.
      Example: Disable the “Submit” button until all fields are filled.

    • You’re building dynamic forms.
      Example: Show or hide fields based on what the user selects.

    • You need to sync input values with other state.
      Example: Update a live preview as the user types.

    Use Uncontrolled Components When:

    • You just need the value when the form is submitted.
      Example: A basic contact form that sends data once.

    • You don’t need to update the UI based on input.

    • You want better performance in large forms.
      Example: Dozens of inputs that don’t need to trigger re-renders on every change.

    In short:

    • If you need to watch, validate, or react to what the user types(interact with your app’s state or UI), go with controlled.

    • If you just need to grab the value later, uncontrolled can work fine.

    Conclusion

    Controlled vs. Uncontrolled Components might seem like a small technical distinction at first but understanding the difference gives you much more control over how your forms behave in React.

    Controlled components put you in the driver’s seat. React manages the form state for you, so you always know what’s in your inputs. This makes it easy to validate user input in real-time, sync form data with other parts of your app, and build more interactive, dynamic experiences.

    Uncontrolled components, on the other hand, keep things minimal. The browser handles the input’s state, and you only reach in to get the value when you need it, usually using a ref.

    There’s no one-size-fits-all answer for which is better. It depends entirely on your needs. If your form needs to react to user input as it changes or connect tightly with app logic, go controlled. If you just need to collect some values and move on, uncontrolled might be simpler.

    Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleI finally found a portable power station I can store in my truck, and it’s $100 off
    Next Article How to Build Production-Ready Full Stack Apps with the MERN Stack

    Related Posts

    Development

    GPT-5 is Coming: Revolutionizing Software Testing

    July 22, 2025
    Development

    Win the Accessibility Game: Combining AI with Human Judgment

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    The AI Fix #59: Grok thinks it’s Mecha Hitler, and AIs can think strategically

    Development

    Best Free and Open Source Software: May 2025 Updates

    Linux

    CVE-2025-48269 – WPAdverts Cross-Site Scripting (XSS)

    Common Vulnerabilities and Exposures (CVEs)

    1fr 1fr vs auto auto vs 50% 50%

    Web Development

    Highlights

    CVE-2025-30202 – vLLM ZeroMQ Denial of Service and Data Exposure Vulnerability

    April 30, 2025

    CVE ID : CVE-2025-30202

    Published : April 30, 2025, 1:15 a.m. | 1 hour, 52 minutes ago

    Description : vLLM is a high-throughput and memory-efficient inference and serving engine for LLMs. Versions starting from 0.5.2 and prior to 0.8.5 are vulnerable to denial of service and data exposure via ZeroMQ on multi-node vLLM deployment. In a multi-node vLLM deployment, vLLM uses ZeroMQ for some multi-node communication purposes. The primary vLLM host opens an XPUB ZeroMQ socket and binds it to ALL interfaces. While the socket is always opened for a multi-node deployment, it is only used when doing tensor parallelism across multiple hosts. Any client with network access to this host can connect to this XPUB socket unless its port is blocked by a firewall. Once connected, these arbitrary clients will receive all of the same data broadcasted to all of the secondary vLLM hosts. This data is internal vLLM state information that is not useful to an attacker. By potentially connecting to this socket many times and not reading data published to them, an attacker can also cause a denial of service by slowing down or potentially blocking the publisher. This issue has been patched in version 0.8.5.

    Severity: 7.5 | HIGH

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    AI could erase half of entry-level white collar jobs in 5 years, CEO warns

    May 29, 2025

    CVE-2025-48027 – pGina HttpAuth DNS Rebinding Vulnerability

    May 15, 2025

    Next.js Flaw (CVE-2025-49826, CVSS 7.5): Cache Poisoning Leads to Denial-of-Service

    July 3, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.