Master React From Scratch

Clear, practical, and structured React lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to React
Introduction to React What Is React? Features of React Advantages of React Limitations of React Uses of React React vs JavaScript React Architecture
2. React Getting Started
Getting Started Introduction Prerequisites Installing Node.js and npm Creating a React Project with Vite React Project Structure Running React Application Your First React Application
3. JSX
JSX Introduction What Is JSX? JavaScript Expressions JSX Attributes Rendering JSX Elements JSX Children React Fragments JSX Rules and Best Practices JSX vs HTML
4. React Components
Components Introduction Functional Components Class Components Component Composition Reusable Components Component Tree Component Lifecycle Import and Export Components
5. React Props
Props Introduction What Are Props? Passing Props to Components Props Destructuring Default Props Values Children Props Typing Props with TypeScript Props vs State
6. React State
State Introduction What Is State? useState Hook Updating State Functional State Updates State with Objects State with Arrays State Immutability Lifting State Up
7. React Events
Events Introduction Handling Events Passing Arguments to Event Handlers Synthetic Events Form Events Keyboard and Mouse Events Event Propagation
8. Conditional Rendering
Conditional Rendering Introduction If-Else Rendering Ternary Operator Logical AND Operator Element Variables Preventing Rendering Multiple Conditions Conditional CSS Classes
9. Lists and Keys
Lists and Keys Introduction Rendering Lists with map() Understanding Keys Choosing Unique Keys Lists of Objects Filtering Lists Sorting Lists Empty Lists and Conditional Rendering
10. React Forms
Forms Introduction Handling Forms Controlled Components Uncontrolled Components Handling Input Fields Checkboxes and Radio Buttons Select and Textarea Form Validation Form Submission
11. React Hooks
Hooks Introduction useState Hook useEffect Hook Effect Dependencies and Cleanup useRef Hook useReducer Hook useMemo Hook useCallback Hook Custom Hooks Rules of Hooks
12. React Context API
Context API Introduction createContext useContext Context Provider Reading Context Updating Context Using Multiple Contexts Context API vs Props Context API Best Practices
13. React Routing
Routing Introduction Introduction to React Router Installing React Router BrowserRouter Setup Routes and Route Components Link and NavLink Programmatic Navigation URL Parameters and useParams Nested Routes Not Found Routes Protected Routes
14. API Integration
API Integration Introduction Fetch API Async/Await with API Calls Displaying API Data Loading and Error States Sending POST Requests API Error Handling Using Axios Custom Fetch Hook API Environment Variables
15. State Management
State Management Introduction Global State Prop Drilling Introduction to Redux Redux Core Concepts Redux Toolkit Store and Slices useSelector and useDispatch Async Operations with Redux Toolkit State Management Best Practices
16. React Performance
Performance Introduction Understanding Re-Renders Avoiding Unnecessary Re-Renders React.memo useMemo Optimization useCallback Optimization React Developer Tools and Profiler Lazy Loading Components Code Splitting Optimizing Large Lists Performance Best Practices

1. Introduction to Uncontrolled Components

An uncontrolled component is a form element that manages its own value through the DOM instead of having its value controlled by React state.

React can access the current value of an uncontrolled input when needed. A common approach is to use the useRef hook to create a reference to the input element.

Remember: Controlled inputs commonly use value and onChange. Uncontrolled inputs commonly use defaultValue or refs to access their values.

2. How Uncontrolled Components Work

In an uncontrolled input, the browser maintains the current input value. React can read that value through a ref when an action such as a button click or form submission occurs.

User Enters Information The user types into an input field.
↓
DOM Stores the Value The input maintains its current value.
↓
React Reads the Value A ref provides access to the input element.
↓
Process the Information The value can be displayed or submitted.
Important: A ref provides access to the input element. Reading its value does not automatically update React state.

3. Using defaultValue

The defaultValue prop sets the initial value of an uncontrolled text input. After the input is displayed, the user can change its value without React controlling every change through state.

Example

function StudentInput() {
    return (
        <div>
            <label htmlFor="studentName">
                Student Name:
            </label>

            <input
                id="studentName"
                type="text"
                defaultValue="Rahul"
            />
        </div>
    );
}

export default StudentInput;

Expected Output

The input initially displays "Rahul". The defaultValue prop sets the initial value rather than controlling every subsequent change.

4. Accessing Input Values Using useRef

The useRef hook can hold a reference to a DOM input element. We can read the current input value through the ref when a button is clicked.

Example

import { useRef } from "react";

function NameForm() {
    const nameRef = useRef(null);

    function handleClick() {
        alert(nameRef.current.value);
    }

    return (
        <div>
            <label htmlFor="name">Name:</label>

            <input
                id="name"
                type="text"
                ref={nameRef}
                defaultValue="Rahul"
            />

            <button onClick={handleClick}>
                Show Name
            </button>
        </div>
    );
}

export default NameForm;

Expected Result

Clicking the button in the running React application displays the current input value in an alert.

The ref's current property refers to the input element. Its value property contains the text entered by the user.

5. Handling Form Submission with Refs

We can use refs to read uncontrolled input values when a form is submitted. The submit handler can then process the values without storing every keystroke in React state.

Example

import { useRef } from "react";

function LoginForm() {
    const usernameRef = useRef(null);
    const passwordRef = useRef(null);

    function handleSubmit(event) {
        event.preventDefault();

        const username = usernameRef.current.value;
        const password = passwordRef.current.value;

        console.log("Username:", username);
        console.log("Password entered:", password.length > 0);
    }

    return (
        <form onSubmit={handleSubmit}>
            <label htmlFor="username">
                Username:
            </label>

            <input
                id="username"
                ref={usernameRef}
                defaultValue=""
            />

            <label htmlFor="password">
                Password:
            </label>

            <input
                id="password"
                type="password"
                ref={passwordRef}
                defaultValue=""
            />

            <button type="submit">
                Submit
            </button>
        </form>
    );
}

export default LoginForm;

Expected Console Output

Username: Rahul
Password entered: true

The handler reads both values when the form is submitted. This example logs only whether a password was entered, rather than printing the password itself.

Security note: Do not log real passwords or sensitive form information to the browser console.

6. Using Refs with Textarea

A textarea is used to collect multiline text. We can attach a ref to it and read the entered text when required.

Example

import { useRef } from "react";

function FeedbackForm() {
    const feedbackRef = useRef(null);

    function handleSubmit(event) {
        event.preventDefault();

        console.log(feedbackRef.current.value);
    }

    return (
        <form onSubmit={handleSubmit}>
            <label htmlFor="feedback">
                Feedback:
            </label>

            <textarea
                id="feedback"
                ref={feedbackRef}
                defaultValue=""
            />

            <button type="submit">
                Submit Feedback
            </button>
        </form>
    );
}

export default FeedbackForm;

Expected Result

When submitted, the entered feedback is written to the console.

The ref provides access to the textarea element, and its current value contains the entered feedback.

7. Controlled vs Uncontrolled Components

Both approaches can collect user input, but they differ in how the current value is managed and accessed.

Feature Controlled Uncontrolled
Value management React state DOM element
Common props value and onChange defaultValue and ref
Reading values From state From the input element
React updates State updates on input changes Value can change without state updates
Typical use Live validation and dynamic UI Reading values when needed
Remember: Choose the approach based on your form's requirements. Controlled inputs are useful when the interface needs to respond to every value change.

8. Common Mistakes

Mistake 1: Forgetting to Attach the Ref

const nameRef = useRef(null);

function handleClick() {
    console.log(nameRef.current.value);
}

return (
    <input defaultValue="Rahul" />
);

The input does not have the ref prop, so nameRef.current does not refer to that input element.

Correct Code

<input
    ref={nameRef}
    defaultValue="Rahul"
/>

Mistake 2: Accessing the Value Before Mounting

const nameRef = useRef(null);

console.log(nameRef.current.value);

Initially, nameRef.current is null before React attaches the ref to the input element. Accessing value at that time causes an error.

Correct Code

function handleClick() {
    if (nameRef.current) {
        console.log(nameRef.current.value);
    }
}

Mistake 3: Confusing defaultValue and value

<input
    defaultValue={name}
    onChange={handleChange}
/>

defaultValue sets the initial value of an uncontrolled input. It does not make the input a controlled component.

Correct Controlled Input

<input
    value={name}
    onChange={handleChange}
/>

9. Practice Tasks

Complete these tasks to practise uncontrolled components.

Task 1: Create an uncontrolled text input with an initial value using defaultValue.
Task 2: Use useRef to read the value of a student name input when a button is clicked.
Task 3: Create a login form with username and password refs. Read the values on submission without logging the password.
Task 4: Create a feedback form using textarea and read its value through a ref.
Task 5: Write a short comparison of controlled and uncontrolled components with one example of each.

Summary

Uncontrolled components allow form elements to maintain their values in the DOM instead of controlling every value through React state. The defaultValue prop can provide an initial input value, and the useRef hook can access the current value when needed. Refs can be used with text inputs, password fields, and textareas. Controlled components are often useful for live validation and state-driven interfaces, while uncontrolled components can be useful when values only need to be read at specific moments, such as during form submission.