Computer Science Engineering β€’ React Foundation

React Foundation

A student-friendly guide to the core React concepts: JSX, components, props, state, events, forms, hooks, APIs, ReactDOM, Virtual DOM, reconciliation, and a learning roadmap.

1. What is React?

React is a JavaScript library used to create user interfaces (UI).

Instead of writing one huge HTML page, we divide the UI into components.

Traditional HTML
<h1>Welcome</h1>
<button>Login</button>
React
function App() {
  return (
    <div>
      <h1>Welcome</h1>
      <button>Login</button>
    </div>
  );
}

Here, App is a component.

2. The React Learning Order

Learn React step by step. Do not jump directly into advanced React.

JavaScript Basics→JSX→Components→Props→State→Events→Hooks→API / Backend→Routing

3. JSX

JSX = JavaScript + HTML-like syntax. It looks like HTML, but it is written inside JavaScript.

function App() {
  return (
    <h1>Hello React</h1>
  );
}

JavaScript expressions can be placed inside JSX using curly braces:

function App() {
  const name = "Avinash";
  return (
    <h1>Hello {name}</h1>
  );
}
Remember: {name} means β€œput the value of the JavaScript variable name here.”

4. Components

A component is simply a reusable piece of UI.

function Welcome() {
  return <h1>Welcome to React</h1>;
}

function App() {
  return (
    <div>
      <Welcome />
      <Welcome />
    </div>
  );
}
ELI10: Component = reusable UI block.

5. Props

Props are information passed from one component to another. Think of props like function arguments.

function Student(props) {
  return <h2>Hello {props.name}</h2>;
}

function App() {
  return (
    <div>
      <Student name="Ravi" />
      <Student name="John" />
    </div>
  );
}
JavaScript
function student(name) {
  console.log(name);
}
student("Ravi");
React
<Student name="Ravi" />

6. State

State = data that can change while the application is running.

0β†’1β†’2β†’3

React uses useState().

import { useState } from "react";

function App() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => setCount(count + 1)}>
        Increase
      </button>
    </div>
  );
}
PartMeaning
countCurrent value
setCountFunction to change the value
0Initial value
setCount(count + 1) means change the count to count + 1 and update the screen.

7. Events

React handles user actions using events.

<button onClick={handleClick}>
  Click Me
</button>

function handleClick() {
  alert("Button clicked!");
}

Common events: onClick, onChange, onSubmit, onMouseOver, onKeyDown

8. Forms

Forms are important for registration, login, search, contact, and payment.

import { useState } from "react";

function Login() {
  const [username, setUsername] = useState("");
  return (
    <div>
      <input
        value={username}
        onChange={(e) => setUsername(e.target.value)}
      />
      <h2>{username}</h2>
    </div>
  );
}
What happens? As the user types A β†’ Av β†’ Avi β†’ Avin β†’ Avina β†’ Avinash, React's state changes accordingly.

9. Conditional Rendering

Display something only when a condition is true.

function App() {
  const loggedIn = true;
  return (
    <div>
      {loggedIn
        ? <h1>Welcome User</h1>
        : <h1>Please Login</h1>}
    </div>
  );
}
loggedIn = true
Welcome User
loggedIn = false
Please Login

10. Lists

Use .map() to display items from an array.

function App() {
  const students = ["Ravi", "John", "Anita"];
  return (
    <ul>
      {students.map((student) => (
        <li>{student}</li>
      ))}
    </ul>
  );
}
Output: Ravi β€’ John β€’ Anita

Key point: In React, .map() is very important.

11. Hooks

Hooks are special React functions. Two important beginner hooks are:

HookPurpose
useState()Used for changing data.
useEffect()Used for side effects such as fetching data from an API.
useEffect(() => {
  console.log("Component loaded");
}, []);

12. React + Backend

React can provide the frontend UI while Node.js and Express.js provide backend services.

React
Frontend UI
→ fetch()Express / Node.js→JSON / Database
// React
fetch("http://localhost:3000/users")

// Express
app.get("/users", (req, res) => {
  res.json(users);
});

React receives the data and displays it.

13. The React Mental Model

DATA→COMPONENT→UI

When the data changes:

State changes→React re-renders→UI gets updated
Important: You generally don't manually modify HTML like document.getElementById(...). Instead, change the state and let React update the UI.

14. Your React Learning Roadmap

Level 1 β€” JavaScript Foundation

Learn: let/const, functions, arrow functions, objects, arrays, .map(), .filter(), destructuring, spread operator, promises, async/await, fetch().

Level 2 β€” React Foundation

Learn: JSX, Components, Props, State, Events, Conditional rendering, Lists, Forms.

Level 3 β€” React Hooks

Learn: useState, useEffect, useRef, useContext.

Level 4 β€” Real Applications

Learn: API calls, React Router, login/registration, authentication, CRUD, loading/error handling.

Level 5 β€” Advanced

Learn: Context API, custom hooks, performance optimization, state-management libraries, testing, deployment.

ReactDOM

ReactDOM is the bridge between React and the browser's DOM.

React→ReactDOM→Browser DOM→What you see on screen

Virtual DOM

The Virtual DOM is a lightweight, in-memory representation of the user interface.

Virtual DOM

React's internal β€œplan” of what the webpage should look like. Kept in memory and used to calculate UI changes.

Actual DOM

The real webpage structure in the browser. It is what the browser displays.

OLD UI
Hello World
β†’NEW UI
Hello Avinash
→Compare→Update changed part
Virtual DOMReal DOM
React's internal representationBrowser's actual page structure
Kept in memoryManaged by the browser
Used to calculate UI changesDisplays the actual webpage
Not directly visibleVisible through the webpage

Reconciliation

When application data changes, React creates a new representation and compares it with the earlier one. This comparison process is called reconciliation.

Declarative and Idiomatic React

React is declarative because a component describes what the UI should look like for the current data. Imperative code instead gives direct commands about how to alter the page.

ELI10 analogy: Declarative programming is like ordering a meal by stating the desired dish instead of telling the cook every movement needed to prepare it.

React determines the required DOM changes after state or props change. React is also idiomatic JavaScript: components are JavaScript functions and use normal variables, objects, functions, imports, arrays, and expressions. JSX is transformed into JavaScript before it reaches the browser.

Study tip: Strong JavaScript fundamentals make React significantly easier to learn. In JSX, braces allow JavaScript expressions; a full if block should be calculated before the return or replaced with an expression.

🧠 Quick Self-Check

1. What is a React component?
A reusable piece of UI.

2. What does useState() help manage?
Data that can change while the application is running.

3. What is the React mental model?
DATA β†’ COMPONENT β†’ UI; when state changes, React re-renders and updates the UI.