πŸš€ Building Your First React App with Vite: A Step-by-Step Guide

Hey DEV community! πŸ‘‹ This quick guide shows you how to build your first React app with Vite β€” step by step, with clear examples and a summary so you can learn fast and start coding right away! πŸš€

βœ… Step 1: Install Node.js and npm
-> First things first: React apps need Node.js to run the development server and npm (or pnpm/yarn) for dependencies.

πŸ”Ή Download and install Node.js from nodejs.org.
πŸ”Ž Verify installation:

node -v
npm -v

βœ… Step 2: Create a New React App with Vite
-> Instead of Create React App, we’ll use Vite, which is faster and has a better developer experience.

Run these commands:

npm create vite@latest my-first-react-app -- --template react
cd my-first-react-app
npm install
npm run dev

Your browser should open at http://localhost:5173/ with a default React + Vite page β€” congratulations, your app is running! πŸŽ‰

βœ… Step 3: Understand the Project Structure πŸ—‚οΈ
-> Your Vite project will look like this:

my-first-react-app/
β”œβ”€β”€ node_modules/          πŸ“¦ Installed npm packages
β”œβ”€β”€ public/                🌐 Static assets
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ App.jsx            βš›οΈ Main App component
β”‚   β”œβ”€β”€ App.css            🎨 Styles for App component
β”‚   β”œβ”€β”€ main.jsx           πŸš€ Entry point that renders <App /> to the DOM
β”‚   └── assets/            πŸ–ΌοΈ Images and static files
β”œβ”€β”€ index.html             πŸ“ The main HTML template
β”œβ”€β”€ package.json           πŸ“¦ Project metadata & dependencies
β”œβ”€β”€ vite.config.js         βš™οΈ Vite config file
└── README.md              πŸ“˜ App documentation

πŸ”Ž Key folders/files explained:

-> index.html – The single HTML file React mounts into.
-> src/ – Where you write your components.
-> App.jsx – Your main component.
-> main.jsx – Renders App into the DOM.
-> App.css – Styles for your app.
-> vite.config.js – Configures Vite
.

βœ… Step 4: Edit Your First Component ✏️
-> Open src/App.jsx. By default, it will look like:

import { useState } from 'react';
import './App.css';

function App() {
  return (
    <div className="App">
      <h1>Hello Vite + React! 🎨</h1>
    </div>
  );
}
export default App;

βœ… Step 5: Add State with useState Hook πŸ”„
-> Make it interactive by adding a simple counter:

import { useState } from 'react';
import './App.css';

function App() {
  const [count, setCount] = useState(0);

  return (
    <div className="App">
      <h1>Simple Counter πŸš€</h1>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}
export default App;

βœ… Now your app has:

-> A stateful variable (count) to track the counter.
-> A button that updates the state.
-> Instant UI updates as the state changes!

βœ… Step 6: Styling Your App 🎨
-> Open src/App.css and add styles:

.App {
  text-align: center;
  margin-top: 50px;
}

button {
  font-size: 1.2rem;
  padding: 10px 20px;
  cursor: pointer;
}

Styles are automatically applied to your component since App.css is imported in App.jsx.

βœ… Step 7: Build for Production πŸ“¦
-> To create a production-ready build, run:

npm run build

That’s the URL you can open in your browser to view your production build locally: http://localhost:5173/ πŸŽ‰ Just copy that URL and paste it into your browser!

πŸ“ Summary & Conclusion

-> Set up a modern React app using Vite.
-> Edit your first React component.
-> Add state with the useState hook.
-> Style your app with CSS.
-> Build your app for production deployment.

πŸŽ‰ That’s it!
Congratulations β€” your first React + Vite app is live! Keep coding, stay curious, and happy developing! πŸ’»βœ¨

Similar Posts