What is React?
React shines in applications with many possible user interactions — like Instagram, YouTube, Netflix, Spotify, and LinkedIn, all of which use React.Web
React for web applications
Mobile
React Native for iOS and Android
Desktop
Electron for desktop apps
Setting Up with Vite
| Command | Description |
|---|---|
node -v | Check if Node is installed. |
npm install | Install project dependencies. |
npm install -g vite | Install Vite globally. |
npm create vite@latest . | Create Vite project in current folder. |
npm run dev | Run the Vite project. |
The dot
. in the command creates the project inside the current folder without creating a subfolder.Project Structure
Understanding the file structure of a React project is essential. Here are the key files you’ll work with:package.json
package.json
Standard configuration file for any Node.js project. It contains:
- Scripts: Available commands like
dev,build, etc. - Dependencies: Packages required for the project to run
- devDependencies: Development-only packages
npm install, npm reads this file and installs everything listed.package.json
index.html
index.html
The only HTML file in the application. It serves as a shell with no real content.It contains a
<div id="root"></div> where React will inject the entire application via JavaScript. It also loads main.jsx, which is React’s entry point.index.html
main.jsx
main.jsx
The entry point of the React application. Responsible for rendering the root component (
App) inside the div#root from the HTML.This is also where you configure React Router (RouterProvider) when used. You rarely need to edit this file in day-to-day development.src/main.jsx
App.jsx
App.jsx
The root component of the application — the first component React renders.Functions as the central point where you organize other components and manage main application states. It’s common to keep global states here and pass data to children via props.
src/App.jsx
Folder Structure
Folder Structure
There’s no mandatory structure, but the most common convention separates code into folders inside
src/:components/— Reusable UI pieces (buttons, cards, lists, etc.)pages/— Components that represent a full route/page
.jsx extension and PascalCase naming (e.g., AddTask.jsx, TaskPage.jsx).Single Page Application (SPA)
- Concept
- Example
React generates a SPA — the application’s HTML is practically empty. Content is dynamically inserted via JavaScript.Instead of having one HTML file per page, React manages all pages via JavaScript with just one base HTML.
Components
In React, the application is divided into components — think of them as LEGO pieces that combine to form complete pages. Each component is a JavaScript function that returns JSX. By convention, use PascalCase to name them (e.g.,AddTask, TaskList).
src/components/MyComponent.jsx
Component files use the
.jsx extension, created inside the src/components/ folder.JSX
JSX is a special syntax: HTML mixed with JavaScript.- JavaScript in JSX
- Key Differences
To use JavaScript inside JSX, wrap it with
{} (curly braces).State with useState
State is a special React variable: when you change the state, the interface automatically updates (re-render).
It’s created with the useState Hook, imported from React. Returns an array with [value, update function].
Props
Props are the way to pass data from a parent component to a child component.Everything you pass as an attribute becomes available inside
props.attributeName.Side Effects with useEffect
The useEffect executes a function whenever a value in the dependency list (second parameter) changes.
- With Dependencies
- Empty Dependencies
localStorage Persistence
ThelocalStorage persists data in the browser even after closing the tab or browser.
Stores only strings, so use
JSON.stringify() to save and JSON.parse() to retrieve.Routing with React Router Dom
React Router manages routes (pages) in an SPA using JavaScript, without multiple HTMLs. Install:npm install react-router-dom@6.26.1
Create a src/pages/ folder for page components. Pages are common components — the name is just semantic.
| Hook / Component | Use |
|---|---|
createBrowserRouter | Creates the router with the list of routes (path + element). |
RouterProvider | Component that wraps the application and injects the router. |
useNavigate() | Hook to navigate between routes. navigate('/route') or navigate(-1) to go back. |
useSearchParams() | Hook to read query params from URL. E.g., ?title=foo&desc=bar. |
Consuming APIs with fetch
The most common flow for APIs in React:
Next Steps
Data Fetching
React Query (most popular) or SWR for managing API calls.
Forms
React Hook Form + Zod for complex form validation.
Global State
Context API + useReducer → then Zustand and Redux.
Testing
Jest / Vitest + React Testing Library + Cypress (e2e).
React on Server
Server Components + Next.js (market standard) or Remix.
GraphQL
Apollo Client for consuming GraphQL APIs in React.