Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Notes › Web Development

React Basics — Components, Props and State

React is a JavaScript library for building user interfaces out of components. You describe what the UI should look like for the current data; React updates the page when the data changes.

JSX

HTML-like syntax inside JavaScript, compiled to React.createElement calls.

const name = "Asha";
const el = (
  <div className="card">              {/* className, not class */}
    <h1>Hello, {name}!</h1>             {/* {} = any JS expression */}
    <img src="/me.png" alt="Asha" />    {/* self-close empty tags */}
  </div>
);

Rules: return one parent element (or a Fragment <>...</>), use className and htmlFor, camelCase events (onClick), and style={{ color: "red" }}.

Components

A component is a function that returns JSX. Names start with a capital letter.

function Welcome() {
  return <h2>Welcome to Codify</h2>;
}
export default function App() {
  return (
    <main>
      <Welcome />
      <Welcome />
    </main>
  );
}

Props — data passed in from the parent

function CourseCard({ title, teacher, free = true }) {     // destructured props + default
  return (
    <div>
      <h3>{title}</h3>
      <p>by {teacher} {free && "· Free"}</p>
    </div>
  );
}
<CourseCard title="DSA in C++" teacher="Striver" />

State — data that changes over time

import { useState } from "react";
function Counter() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>Clicked {count} times</button>
  );
}
setItems([...items, newItem]);                 // add
setItems(items.filter((i) => i.id !== id));    // remove
setUser({ ...user, name: "Ravi" });            // update one field
setCount((c) => c + 1);                        // use the function form when the new value depends on the old one

Props vs state

Props State
Comes from the parent the component itself
Can the component change it? ❌ ✅ (with the setter)
Change causes a re-render? yes (when the parent re-renders) yes

Lifting state up: when two siblings need the same data, keep the state in their common parent and pass it down as props (plus a callback to change it).

Events and forms

function SignUp() {
  const [email, setEmail] = useState("");
  const submit = (e) => {
    e.preventDefault();               // stop the page reload
    console.log("submit", email);
  };
  return (
    <form onSubmit={submit}>
      <input value={email} onChange={(e) => setEmail(e.target.value)} />   {/* controlled input */}
      <button>Sign up</button>
    </form>
  );
}

Conditional rendering

{isLoggedIn ? <Dashboard /> : <Login />}
{error && <p className="error">{error}</p>}
if (!data) return <Spinner />;          // early return

Lists and keys

<ul>
  {courses.map((c) => (
    <li key={c.id}>{c.title}</li>
  ))}
</ul>

Keys tell React which item is which between renders. Use a stable unique id, not the array index (indices break when items are added, removed or reordered).

How React updates the page

  1. State or props change → React calls the component again (render).
  2. It builds a new virtual DOM (a lightweight JS description of the UI).
  3. It diffs it with the previous one (reconciliation).
  4. It updates only the changed parts of the real DOM (commit).

Interview one-liners

More Web Development notes