Framework Guide
Build Your First React Component
React powers most modern web apps — Facebook, Netflix, Airbnb, and thousands of startups. In this guide, you'll set up a React project with Vite and build an interactive counter in under 15 minutes.
📋 What You'll Need
- Node.js installed — see our Install Node.js guide
- A code editor (VS Code recommended)
- A terminal
- 15 minutes of your time
✅ React is free and open source. No signup required.
What you'll build: A working counter app with two buttons — one to increment, one to decrement. It uses useState to track the count and update the UI when it changes.
Create a New React Project with Vite
Vite (pronounced "veet") is the modern build tool for React. It's faster than the old Create React App and is now the recommended way to start.
In a terminal, run:
npm create vite@latest my-first-react-app -- --template react
You'll be asked a few questions. Accept the defaults. Then:
cd my-first-react-app
npm install
This installs React and the dev server. It takes about 30 seconds.
💡 Use react-ts template instead of react if you want TypeScript. It's the same setup with types enabled.
Start the Development Server
Still in your project folder, run:
npm run dev
You'll see a URL like http://localhost:5173. Open it in your browser.
You should see the default Vite + React welcome page. Leave this running — changes you make will hot-reload instantly.
Look at the Project Structure
Open the project in VS Code. Here's what matters:
my-first-react-app/
├── src/
│ ├── App.jsx ← your main component
│ ├── main.jsx ← entry point
│ └── index.css ← global styles
├── index.html ← the single HTML page
├── package.json ← dependencies
└── vite.config.js ← Vite config
The entire app lives inside src/App.jsx. That's where we'll work.
Replace App.jsx
Open src/App.jsx, delete everything, and paste this:
function App() {
return (
<div>
<h1>Hello from React!</h1>
<p>This is my first component.</p>
</div>
);
}
export default App;
Save the file. Your browser should update instantly to show:
- Hello from React!
- This is my first component.
What's happening? You wrote a function called App that returns JSX (that HTML-like syntax). React calls that function and renders the result to the page. Every React component is just a function like this.
Add State with useState
Now let's make it interactive. Replace App.jsx with this:
import { useState } from 'react';
function App() {
const [count, setCount] = useState(0);
return (
<div style={{ padding: '2rem', textAlign: 'center' }}>
<h1>Counter: {count}</h1>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<button onClick={() => setCount(count - 1)}>
Decrement
</button>
</div>
);
}
export default App;
Save. Now click the buttons — the count updates instantly. 🎉
💡 What does useState do? It creates a piece of state (count) and a function to update it (setCount). Whenever setCount is called, React re-renders the component with the new value.
Extract a Reusable Component
Real apps break UI into small components. Let's make a Button component and reuse it:
import { useState } from 'react';
function Button({ label, onClick }) {
return (
<button
onClick={onClick}
style={{ margin: '0 4px', padding: '8px 16px' }}
>
{label}
</button>
);
}
function App() {
const [count, setCount] = useState(0);
return (
<div style={{ padding: '2rem', textAlign: 'center' }}>
<h1>Counter: {count}</h1>
<Button label="Increment" onClick={() => setCount(count + 1)} />
<Button label="Decrement" onClick={() => setCount(count - 1)} />
</div>
);
}
export default App;
Same result, but now Button is reusable. That's the pattern behind every React app — small components combined into larger ones.
🧠 Key React Concepts You Just Used
- Components — functions that return JSX. Everything in React is a component.
- JSX — HTML-like syntax inside JavaScript. Note:
classNameinstead ofclass, andonClickinstead ofonclick. - Props — inputs passed to a component. In our example,
labelandonClickare props. - State — data that changes over time. Managed by
useState. - Re-rendering — when state changes, React re-runs the component function and updates the DOM automatically.
🔧 Troubleshooting
"npm: command not found"
Node.js isn't installed or isn't on your PATH. See our Install Node.js guide.
Page shows a blank screen
Open the browser console (F12). The error message usually tells you exactly which line is wrong. Common culprit: missing export default App.
"Port 5173 is already in use"
Another Vite project is running. Close the other terminal, or Vite will offer to use a different port automatically.
Styling doesn't apply
In React, use className="..." instead of class="...". Also remember to import your CSS file: import './index.css'.
🎯 Practice Challenges
- Reset button: add a third button that sets the count back to 0
- Step counter: add +5 and −5 buttons
- Color the count: show red when negative, green when positive
- Input field: let the user type a number to set the count directly
- Multiple counters: render two independent counters on the same page
🎯 What's Next?
❓ Frequently Asked Questions
What is a React component?
A function that returns JSX. Everything on the screen is a component — buttons, headers, pages.
What is JSX?
A syntax extension that lets you write HTML-like code inside JavaScript. React compiles it into function calls behind the scenes.
Why does React use className instead of class?
Because "class" is a reserved keyword in JavaScript. React uses "className" to avoid a conflict with JS class syntax.
Should I use Create React App or Vite?
Use Vite. Create React App is no longer maintained and slow. Vite is faster, simpler, and the current standard for new React projects.