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 Select and Textarea

A select element allows users to choose an option from a dropdown list. A textarea allows users to enter multiple lines of text, such as feedback, comments, or a course enquiry.

In React, both elements can be controlled using state. The value prop stores the current selection or text, while onChange updates the state when the user changes the input.

Remember: Use value and onChange to manage controlled select and textarea elements in React.

2. Handling a Select Element

A select element displays a dropdown list. React state can store the selected option and update it whenever the user chooses another option.

Example

import { useState } from "react";

function CourseSelect() {
    const [course, setCourse] = useState("React");

    function handleChange(event) {
        setCourse(event.target.value);
    }

    return (
        <div>
            <label htmlFor="course">Choose a Course:</label>

            <select
                id="course"
                value={course}
                onChange={handleChange}
            >
                <option value="React">React</option>
                <option value="Angular">Angular</option>
                <option value="Python">Python</option>
                <option value="Data Analytics">
                    Data Analytics
                </option>
            </select>

            <p>Selected Course: {course}</p>
        </div>
    );
}

export default CourseSelect;

Expected Output

Selected Course: React

The course state initially contains React. When the user selects another option, handleChange reads the selected value and updates the state.

3. Handling a Textarea Element

A textarea is useful when users need to enter longer text. For example, students may provide feedback or describe their learning requirements.

Example

import { useState } from "react";

function FeedbackInput() {
    const [feedback, setFeedback] = useState("");

    function handleChange(event) {
        setFeedback(event.target.value);
    }

    return (
        <div>
            <label htmlFor="feedback">Your Feedback:</label>

            <textarea
                id="feedback"
                value={feedback}
                onChange={handleChange}
                rows={4}
            />

            <p>Feedback: {feedback}</p>
        </div>
    );
}

export default FeedbackInput;

Expected Output

Feedback: Great learning experience at CIIT Training Institute!

The feedback state stores the text entered by the user. Every change updates the state, and React displays the current feedback below the textarea.

4. Setting a Default Selection

A controlled select element can have an initial selection by setting the initial state value. The select value should match one of the option values.

Example

import { useState } from "react";

function PreferredCourse() {
    const [course, setCourse] = useState("Data Analytics");

    return (
        <div>
            <label htmlFor="preferredCourse">
                Preferred Course:
            </label>

            <select
                id="preferredCourse"
                value={course}
                onChange={(event) =>
                    setCourse(event.target.value)
                }
            >
                <option value="React">React</option>
                <option value="Angular">Angular</option>
                <option value="Data Analytics">
                    Data Analytics
                </option>
            </select>

            <p>Preferred Course: {course}</p>
        </div>
    );
}

export default PreferredCourse;

Expected Output

Preferred Course: Data Analytics

Because the initial state is Data Analytics, that option appears selected when the component first renders.

5. Handling Multiple Lines of Text

The rows attribute controls the visible height of a textarea. You can use it to provide enough space for users to write comments, questions, or course enquiries.

Example

<textarea
    rows={5}
    value={message}
    onChange={(event) =>
        setMessage(event.target.value)
    }
    placeholder="Enter your enquiry"
/>

Expected Output

The textarea provides multiple visible lines for entering text. The value and onChange props connect it to React state.

6. CIIT Training Institute Course Enquiry Form

Consider Samadhan Samiksha submitting a course enquiry to CIIT Training Institute. This example combines a select element and a textarea in one controlled form.

Example

import { useState } from "react";

function CourseEnquiry() {
    const [studentName, setStudentName] =
        useState("Samadhan");

    const [course, setCourse] = useState("React");

    const [message, setMessage] = useState("");

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

        console.log({
            studentName,
            course,
            message
        });
    }

    return (
        <form onSubmit={handleSubmit}>
            <h2>CIIT Training Institute</h2>

            <label htmlFor="studentName">
                Student Name:
            </label>

            <input
                id="studentName"
                value={studentName}
                onChange={(event) =>
                    setStudentName(event.target.value)
                }
            />

            <label htmlFor="course">
                Select Course:
            </label>

            <select
                id="course"
                value={course}
                onChange={(event) =>
                    setCourse(event.target.value)
                }
            >
                <option value="React">React</option>
                <option value="Angular">Angular</option>
                <option value="Data Analytics">
                    Data Analytics
                </option>
            </select>

            <label htmlFor="message">
                Your Enquiry:
            </label>

            <textarea
                id="message"
                value={message}
                onChange={(event) =>
                    setMessage(event.target.value)
                }
                rows={4}
            />

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

export default CourseEnquiry;

Expected Console Output

{
    studentName: "Samadhan",
    course: "React",
    message: "Please share the course details."
}

When the form is submitted, the handler prevents the browser's default form submission and logs the current values. This example demonstrates form handling; it does not save the enquiry to a database or send it to the institute.

CIIT Training Institute: Select and textarea controls can help build course enquiry forms that collect a student's preferred course and additional questions.

7. Handling Changes with onChange

The onChange event handler receives an event object. For a select element, event.target.value contains the selected option. For a textarea, it contains the current text.

Select Handler

function handleCourseChange(event) {
    setCourse(event.target.value);
}

Textarea Handler

function handleMessageChange(event) {
    setMessage(event.target.value);
}
Remember: The value prop determines the displayed value of a controlled element. Update its state in onChange so the user can change the selection or text.

8. Common Mistakes

Mistake 1: Using value Without onChange

<select value={course}>
    <option value="React">React</option>
    <option value="Angular">Angular</option>
</select>

The select is controlled by course, but no handler updates the state when the selection changes.

Correct Code

<select
    value={course}
    onChange={(event) =>
        setCourse(event.target.value)
    }
>
    <option value="React">React</option>
    <option value="Angular">Angular</option>
</select>

Mistake 2: Not Matching the Option Value

const [course, setCourse] = useState("Python");

return (
    <select value={course} onChange={handleChange}>
        <option value="React">React</option>
        <option value="Angular">Angular</option>
    </select>
);

The state contains Python, but there is no option with that value. The initial state should match an available option, or the option list should include Python.

Mistake 3: Using Children Instead of State for a Controlled Textarea

<textarea value={message}>
    Initial message
</textarea>

For a controlled textarea, provide the content through value rather than placing initial text between its tags.

Correct Code

<textarea
    value={message}
    onChange={(event) =>
        setMessage(event.target.value)
    }
/>

9. Practice Tasks

Complete these tasks to practise select and textarea elements.

Task 1: Create a dropdown containing React, Angular, and Python. Display the selected course.
Task 2: Create a controlled textarea that displays the current feedback below it.
Task 3: Set Data Analytics as the default selected course.
Task 4: Create a CIIT Training Institute enquiry form for Samadhan Samiksha with a course dropdown and a message textarea.
Task 5: Submit the enquiry form and log the student name, selected course, and message to the console.

Summary

Select elements allow users to choose options from a dropdown, while textarea elements collect multiline text. In React, both can be managed as controlled components using value and onChange. State stores the selected option or entered text, and event.target.value provides the current value when a change occurs. These controls can be combined to build course enquiry forms that collect a student's preferred course and additional questions for CIIT Training Institute.