Tutorial 1 of 4
React Essentials
React is a library for building a UI out of components. You describe what the screen should look like for the current data, and React updates the page when the data changes. Examples use React 19, Vite and TypeScript.
Create the app
Vite is the build tool. It gives you a dev server with instant reload and a production build.
Three files matter at the start:
Concept: this is a single page app (SPA). The server sends one empty HTML file, and JavaScript builds everything in the browser.
Components and JSX
A component is a function that returns JSX. JSX looks like HTML but is JavaScript, so you can drop any expression inside { }.
- Component names start with a capital letter:
<Greeting />. - Return one root element. Use
<>...</>if you do not want a wrapper. - Write
className, notclass.
Props
Props are the inputs of a component. The parent passes them, the child reads them and never changes them. children is whatever you put between the tags.
State and events
State is data a component remembers between renders. Calling the setter makes React run the component again and update the screen.
Rule: never change state in place. Create a new value: setItems([...items, newItem]), not items.push(newItem).
Lists and conditions
Use map to render a list. Every item needs a stable key so React can track it. Use && or a ternary to show things conditionally.
Forms
A controlled input keeps its value in state. The input shows the state, and typing updates the state.
Concept: data flows down through props, events flow up through callback props like onAdd.
Effects and data fetching
useEffect runs code after render, to sync with something outside React such as an API. The dependency array says when to run it again.
| Dependency array | Effect runs |
|---|---|
[] | Once, after the first render |
[userId] | After the first render and whenever userId changes |
| none | After every render (rarely what you want) |
In real apps, a library such as TanStack Query replaces this loading and error boilerplate and adds caching.
Custom hooks
A custom hook is a function whose name starts with use and that calls other hooks. It lets you reuse stateful logic. Here the fetch logic from the last chapter becomes one line per component.
Rule of hooks: call hooks only at the top level of a component or another hook. Never inside if, loops or nested functions.
Context: shared state
Passing props through many levels gets tedious. Context lets a provider near the top share a value with any component below it.
Wrap the app once with <ThemeProvider>, then call useTheme() anywhere. Use context for app-wide things: user, theme, language. Keep everything else as local state.
Routing
React has no router built in. React Router maps URLs to components without reloading the page.
To navigate from code: const navigate = useNavigate(); navigate("/dashboard");
Auth
The pattern has three parts: a context that holds the token, a login function that calls your API, and a route wrapper that redirects when there is no token.
Send the token on every API call: headers: { Authorization: `Bearer ${token}` }.
Important: a protected route in the browser is only for user experience. Anyone can edit browser code, so the API must check the token on every request.
Styling
CSS Modules (built into Vite)
Name the file *.module.css. Class names become local to the component, so they never clash.
Tailwind CSS
Utility classes written directly in JSX. Most Figma handoffs map well to it.
From Figma to components
A Figma file and a React app have the same shape. Translate one into the other in this order:
| In Figma | In React |
|---|---|
| Variables (colors, spacing, radius) | CSS variables or the Tailwind theme |
| Component | Component file |
| Variant (size, state) | Prop: <Button size="lg" /> |
| Auto layout | Flexbox: direction, gap, padding |
| Frame or page | Route component |
| Icons and images | Export as SVG or WebP into src/assets |
By hand
Open the file in Dev Mode, click a layer, and read its size, spacing and color in the Inspect panel. Copy tokens first, then build the smallest components (button, input), then compose pages from them.
With an AI agent (Figma MCP)
The Figma MCP server lets a coding agent read a frame directly: layout, tokens, and a screenshot. Connect it once, then paste a frame link.
Figma Code Connect goes one step further: it links each Figma component to your real React component, so generated code imports Button from your codebase and does not invent new markup.
Build and deploy
dist/ is plain HTML, CSS and JS. Host it anywhere static: S3 with CloudFront, Netlify, Nginx.
One catch: because routing happens in the browser, the host must return index.html for every path. Otherwise a refresh on /users/42 gives a 404.
Environment values go in .env and must start with VITE_. Read them with import.meta.env.VITE_API_URL. They end up in the browser bundle, so never put secrets there.
The mental model
- The UI is a function of state. Change state, React redraws.
- Props go down, events go up.
- Keep state as low in the tree as possible. Lift it up only when two components need it.
- Effects are for talking to the outside world, not for computing values.
- Hooks are how you reuse logic. Components are how you reuse UI.
React leaves routing, data fetching, the server and SEO to you. That gap is what Next.js fills. Continue with Next.js Essentials, or see both side by side.