Code Reference

Hello World

React · Reference cheat sheet

Hello World

React · Reference cheat sheet


📋 Overview

React Hello World is a component that returns a simple element (often an <h1>). Once it appears in the browser via your app’s root render, your toolchain is working.

🔧 Core concepts

PieceRole
RootcreateRoot(...).render(<App />) mounts React
AppTop-level component
return (...)JSX describing UI
ExportMakes the component importable

In Vite, main.jsx mounts <App /> into #root.

💡 Examples

Minimal App:

export default function App() {
  return <h1>Hello, World!</h1>;
}

With a child component:

function Hello() {
  return <h1>Hello, World!</h1>;
}

export default function App() {
  return (
    <div>
      <Hello />
      <p>Welcome to React.</p>
    </div>
  );
}

Mount (Vite-style main.jsx):

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";

createRoot(document.getElementById("root")).render(
  <StrictMode>
    <App />
  </StrictMode>
);

Expression in JSX:

const name = "Ada";
export default function App() {
  return <h1>Hello, {name}!</h1>;
}

⚠️ Pitfalls

  • getElementById("root") must match an element in index.html.
  • Returning two sibling elements needs a wrapper or fragment (&lt;>...&lt;/>).
  • class in JSX is written className.
  • Browser console errors are your friend — read the first red message carefully.

On this page