React: From Components to Production · Components and JSX

Setting Up a React Project

Create a React + TypeScript app with Vite and understand the project structure.

The recommended ways to start a React app today are a framework (Next.js, React Router framework mode) or a build tool like Vite for a client-side app. npm create vite@latest my-app -- --template react-ts scaffolds React with TypeScript, a dev server with hot module replacement and an optimized production build. main.tsx mounts the root component with createRoot, and <StrictMode> runs extra development checks (double-invoking renders and effects to surface bugs).

// src/main.tsx
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { App } from "./App";
import "./index.css";

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

// src/App.tsx
export function App() {
  return <h1>Hello, React</h1>;
}
npm create vite@latest my-app -- --template react-ts
cd my-app && npm install && npm run dev
npm run build && npm run preview

Common pitfalls

  • Using Create React App for new projects — it's deprecated.
  • Being surprised that effects run twice in development under StrictMode (intentional, to catch missing cleanups).
  • Putting secrets in VITE_* env variables — they're bundled into client code.

Key points

  • Vite or a framework to start
  • StrictMode surfaces bugs in development
  • Client env variables are public

Further reading: react.dev: Creating a React app

Practice questions

Why do effects run twice in development with StrictMode?

Answer

React intentionally mounts, unmounts and remounts components in development to reveal effects without proper cleanup. It doesn't happen in production.

Is it safe to put an API secret in VITE_API_KEY?

Answer

No. Variables prefixed with VITE_ are embedded in the client bundle and visible to anyone. Keep secrets on a server.

Open in the app to mark lessons complete, take notes and track your progress.