MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Controlled vs Uncontrolled Components with useRef0%
Study Materials
React.js for IntermediateProgress 0%
1.1Component Reusability and Polymorphic Components1.2Render Props Pattern in Modern React1.3Higher-Order Components vs Modern Hooks1.4Compound Components Pattern Explained
3.1Controlled vs Uncontrolled Components with useRef3.2Multi-Input Forms with Dynamic State Handlers3.3Client-Side Form Validation and Error Handling3.4React Hook Form Basics and Schema Validation
HomeTutorialsReact.js for IntermediateControlled vs Uncontrolled Components with useRef
Multi-Input Forms with Dynamic State Handlers

Controlled vs Uncontrolled Components with useRef

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. Two Philosophical Approaches to Form Inputs

In web development, form elements like <input>, <textarea>, and <select> maintain their own internal state inside the browser's Document Object Model as users type.

React offers two primary architectural patterns to handle form inputs:

  1. 1
    Controlled Components: React state (useState) is the single source of truth. Every input value is pushed into state via onChange, and read from state via value.
  2. 2
    Uncontrolled Components: The browser DOM itself holds the source of truth. React reads values only when needed (such as upon form submission) using a useRef reference.
1Controlled Component (React is Single Source of Truth):
2[Keystroke] ──> [onChange fires] ──> [setVal(e.target.value)] ──> [Re-render] ──> [value={val}]
3 
4Uncontrolled Component (DOM is Source of Truth):
5[Keystroke] ──> [DOM updates internally (Zero re-renders!)]
6[Submit Click] ──> [inputRef.current.value read once]

2. Implementing Controlled Components

1import React, { useState } from 'react';
2 
3export default function ControlledSearch() {
4 const [query, setQuery] = useState('');
5 
6 return (
7 <div>
8 <input
9 type="text"
10 value={query}
11 onChange={(e) => setQuery(e.target.value)}
12 placeholder="Type to search..."
13 />
14 <p>Instant Character Count: {query.length}</p>
15 </div>
16 );
17}

Why use Controlled Components?

  • Instant Validation: Validate input on every keystroke (e.g. enforcing password rules or phone formatting).
  • Dynamic UI Responses: Enable or disable submit buttons based on current field values.
  • Enforcing Input Formats: Convert text to uppercase automatically as the user types.

3. Implementing Uncontrolled Components with useRef

When you don't need real-time keystroke tracking and want to maximize performance in massive forms, useRef provides direct access to the underlying DOM node:

1import React, { useRef } from 'react';
2 
3export default function UncontrolledRegistration() {
4 const nameRef = useRef(null);
5 const emailRef = useRef(null);
6 const fileRef = useRef(null);
7 
8 const handleSubmit = (e) => {
9 e.preventDefault();
10 
11 // Read values directly from DOM nodes upon submission
12 const studentName = nameRef.current.value;
13 const studentEmail = emailRef.current.value;
14 const attachedFile = fileRef.current.files[0];
15 
16 console.log('Submitting:', { studentName, studentEmail, attachedFile });
17 };
18 
19 return (
20 <form onSubmit={handleSubmit} className="uncontrolled-form">
21 <div>
22 <label>Name:</label>
23 {/* defaultValue sets initial value without controlling updates */}
24 <input type="text" ref={nameRef} defaultValue="Deepak" />
25 </div>
26 
27 <div>
28 <label>Email:</label>
29 <input type="email" ref={emailRef} placeholder="email@example.com" />
30 </div>
31 
32 <div>
33 <label>Certificate Upload:</label>
34 {/* File inputs in HTML are ALWAYS uncontrolled! */}
35 <input type="file" ref={fileRef} />
36 </div>
37 
38 <button type="submit">Submit Record</button>
39 </form>
40 );
41}

4. Special Case: HTML File Inputs <input type="file" />

In HTML, file inputs are strictly read-only for JavaScript security reasons (scripts cannot programmatically set a user's file path).

Because their value cannot be set via value={...}, file inputs in React must always be uncontrolled components accessed via useRef!


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the defining characteristic of a Controlled Component in React?
Question 2
How does an Uncontrolled Component store and track its form data?
Question 3
Why MUST <input type="file" /> elements always be treated as uncontrolled in React?
Question 4
What prop is used to set the initial starting text of an uncontrolled input without controlling subsequent typing?
Question 5
When should an engineer prefer controlled components over uncontrolled components?
Next Lesson

Multi-Input Forms with Dynamic State Handlers

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Lifecycle Equivalents: Mount, Update, and UnmountMulti-Input Forms with Dynamic State Handlers

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext