Lessons in this course (17)

1. JSX, components and props

Basic★ Must knowLesson 1 of 17
jsx
// JSX compiles to function calls: <h1 className="t">Hi</h1>  →  jsx("h1", { className: "t", children: "Hi" })
function OrderCard({ order, onSelect }) {            // props are read-only
  const isBig = order.total > 10000;
  return (
    <div className={`card ${isBig ? "big" : ""}`} onClick={() => onSelect(order.id)}>
      <h3>#{order.id} — {order.customer}</h3>
      <p>{order.total.toLocaleString("en-IN", { style: "currency", currency: "INR" })}</p>
    </div>
  );
}

function OrderList({ orders }) {
  return (
    <>                                               {/* Fragment: no extra DOM node */}
      {orders.map(o => (
        <OrderCard key={o.id} order={o} onSelect={id => console.log(id)} />
      ))}
    </>
  );
}

JSX rules: one root element (or fragment), className not class, htmlFor, camelCase attributes (onClick, tabIndex), {} for JavaScript expressions, self-close empty tags, style={{ color: "red" }}. JSX escapes values by default (XSS-safe); dangerouslySetInnerHTML bypasses that.

children prop for composition: <Card><p>Body</p></Card> → function Card({ children }) { return <div className="card">{children}</div>; }.