JavaScript and React course Β· Module 1: React Basics
Creating a React project with Vite
In this lesson6
On a cosmic journey through the React world, you need a suitable ship to set off. In the past, the standard choice was Create React App (CRA), but this tool has been officially deprecated and is no longer maintained. The modern standard for creating React projects is Vite - an ultrafast next-generation bundler.
Ra, our cosmic guide, says: "Abandon the old CRA shuttle - Vite is the new warp drive engine that will take you to the React galaxy faster than ever!"
Why Vite?
Vite (pronounced "veet," from French for "fast") is a modern tool for building frontend applications, created by Evan You (the creator of Vue.js). The React documentation lists it (alongside Parcel and Rsbuild) among the recommended tools when you build an app without a framework.
Why Vite replaced CRA:
- Lightning-fast server start - Vite leverages native browser ES modules (ESM), so the development server starts in 1-2 seconds (CRA needed 20-40 seconds)
- Instant Hot Module Replacement (HMR) - code changes appear in the browser in ~50ms, regardless of the application size
- Modern architecture - built on ESM instead of the older Webpack
- Fast production builds - since Vite 8 it uses Rolldown (previously Rollup) to bundle files
- Out-of-the-box support - TypeScript, JSX, CSS Modules, PostCSS work without additional configuration
Creating a new project with Vite
To create a new React project with Vite, open the terminal and type:
npm create vite@latest my-react-app -- --template reactThis command:
- Uses
npm createto run the Vite generator vite@latestensures using the latest versionmy-react-appis your project name--template reactselects the React template (also available:react-tsfor TypeScript)
After creating the project, navigate to the directory and install dependencies:
cd my-react-app
npm installThen start the development server:
npm run devYour application will be available at http://localhost:5173 in the browser. Note - Vite uses port 5173 by default, not 3000.
Vite + React project structure
After creating the project, you'll see the following structure:
my-react-app/
βββ node_modules/ # Installed dependencies
βββ public/ # Static files (copied as-is)
β βββ favicon.svg # Site icon
β βββ icons.svg # Icons
βββ src/ # Source code
β βββ assets/ # Assets (images, fonts)
β β βββ react.svg # React logo
β βββ App.css # App component styles
β βββ App.jsx # Main App component
β βββ index.css # Global styles
β βββ main.jsx # Application entry point
βββ index.html # Main HTML file (in root directory!)
βββ vite.config.js # Vite configuration
βββ package.json # Project info and dependencies
βββ .gitignore # Files ignored by Git
βββ .oxlintrc.json # Linter configuration (Oxlint)Key differences from CRA
Notice a few important differences:
- index.html is in the root directory, not in
public/- Vite treats it as the entry point - Files use the .jsx extension instead of .js - this clearly signals that a file contains JSX
- The entry point is main.jsx, not index.js
- vite.config.js instead of hidden Webpack configuration - you have full control
index.html
In Vite, the index.html file is in the project root directory and looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>The key line is <script type="module" src="/src/main.jsx"></script> - Vite loads the application as a native ES module, ensuring a lightning-fast start.
src/main.jsx
This is the React application entry point:
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)This code:
- Imports React and ReactDOM
- Creates a React "root" in the element with id
root - Renders the main
Appcomponent insideStrictMode
src/App.jsx
The main component of your application:
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
function App() {
const [count, setCount] = useState(0)
return (
<>
<div>
<a href="https://vite.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount(c => c + 1)}>
count is {count}
</button>
</div>
</>
)
}
export default Appvite.config.js
The Vite configuration file - simple and readable:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})That's all you need! The @vitejs/plugin-react plugin provides JSX support, Fast Refresh (HMR), and other React features.
Available scripts
In the Vite project's package.json you'll find:
npm run dev- starts the development server with HMRnpm run build- builds the application for production deploymentnpm run preview- previews the built production version locallynpm run lint- checks the code with a linter (in the current template: Oxlint)
Vite vs CRA - comparison
| Feature | CRA (deprecated) | Vite |
|---|---|---|
| Status | Deprecated, no updates | Actively maintained |
| Server start | 20-40 seconds | 1-2 seconds |
| HMR | 1-3 seconds | ~50ms |
| Bundler | Webpack | Rolldown (since Vite 8; previously esbuild + Rollup) |
| Configuration | Hidden (eject) | Explicit (vite.config.js) |
| Modules | CommonJS | Native ES Modules |
| TypeScript | Requires configuration | Works out-of-the-box |
Other tools in the React ecosystem
Besides Vite, it's worth knowing other popular tools:
- Next.js - a React framework with SSR (Server-Side Rendering), SSG, and file-based routing. Ideal for applications requiring SEO
- React Router (formerly Remix) - a React framework focused on web fundamentals and progressive enhancement
- Gatsby - a static site generator with GraphQL
For learning React and building SPA (Single Page Application) applications, Vite is the best choice - fast, simple, and listed in the React documentation.
Ra summarizes: "Vite is the next-generation engine for your React spaceship. Fast startup, instant reactions to course changes, and full control over configuration - everything you need to conquer the galaxy!"
Code for this lesson: App.jsx
import React, { useState } from 'react';
// Demonstration of Vite + React project structure
function ViteProjectDemo() {
const [activeFile, setActiveFile] = useState('App.jsx');
const fileStructure = [
{ name: 'node_modules/', type: 'folder', description: 'Installed packages' },
{ name: 'public/', type: 'folder', description: 'Static files' },
{ name: ' vite.svg', type: 'file', description: 'Vite logo' },
{ name: 'src/', type: 'folder', description: 'Source code' },
{ name: ' assets/', type: 'folder', description: 'Assets (images, fonts)' },
{ name: ' App.jsx', type: 'file', description: 'Main component', active: true },
{ name: ' App.css', type: 'file', description: 'Component styles' },
{ name: ' main.jsx', type: 'file', description: 'Entry point' },
{ name: ' index.css', type: 'file', description: 'Global styles' },
{ name: 'index.html', type: 'file', description: 'Main HTML (in the root directory!)' },
{ name: 'vite.config.js', type: 'file', description: 'Vite configuration' },
{ name: 'package.json', type: 'file', description: 'Project configuration' },
];
const fileContents = {
'App.jsx': `import { useState } from 'react'
import './App.css'
function App() {
const [count, setCount] = useState(0)
return (
<>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => setCount(c => c + 1)}>
count is {count}
</button>
</div>
</>
)
}
export default App`,
'main.jsx': `import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
ReactDOM.createRoot(
document.getElementById('root')
).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)`,
'vite.config.js': `import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
})`,
'package.json': `{
"name": "my-app",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.2.0",
"vite": "^5.0.0"
}
}`
};
return (
<div style={{
fontFamily: 'Arial, sans-serif',
maxWidth: '1000px',
margin: '0 auto',
padding: '20px'
}}>
<header style={{
background: 'linear-gradient(135deg, #61dafb 0%, #21a1f1 100%)',
padding: '30px',
borderRadius: '12px',
color: 'white',
textAlign: 'center',
marginBottom: '30px'
}}>
<h1 style={{ margin: 0, fontSize: '2rem' }}>Vite + React</h1>
<p style={{ margin: '10px 0 0 0', opacity: 0.9 }}>
Project structure and basic files
</p>
</header>
<div style={{
backgroundColor: '#1e1e1e',
borderRadius: '8px',
padding: '20px',
marginBottom: '30px',
fontFamily: 'monospace'
}}>
<div style={{ color: '#888', marginBottom: '10px' }}>Terminal:</div>
<div style={{ color: '#00ff00' }}>$ npm create vite@latest my-app -- --template react</div>
<div style={{ color: '#888', marginTop: '5px' }}>Scaffolding project in /my-app...</div>
<div style={{ color: '#00ff00', marginTop: '5px' }}>Done. Now run: cd my-app && npm install && npm run dev</div>
</div>
<div style={{ display: 'grid', gridTemplateColumns: '300px 1fr', gap: '20px' }}>
<div style={{
backgroundColor: '#252526',
borderRadius: '8px',
padding: '15px'
}}>
<h3 style={{ color: '#cccccc', margin: '0 0 15px 0', fontSize: '14px' }}>
EXPLORER
</h3>
{fileStructure.map((item, index) => (
<div
key={index}
onClick={() => item.type === 'file' && setActiveFile(item.name.trim())}
style={{
padding: '8px 10px',
color: item.type === 'folder' ? '#dcb67a' : '#9cdcfe',
cursor: item.type === 'file' ? 'pointer' : 'default',
backgroundColor: activeFile === item.name.trim() ? '#094771' : 'transparent',
borderRadius: '4px',
fontSize: '14px',
fontFamily: 'monospace'
}}
>
{item.type === 'folder' ? 'βΈ' : 'β’'} {item.name}
</div>
))}
</div>
<div style={{
backgroundColor: '#1e1e1e',
borderRadius: '8px',
padding: '20px'
}}>
<div style={{
color: '#888',
fontSize: '12px',
marginBottom: '10px',
borderBottom: '1px solid #333',
paddingBottom: '10px'
}}>
{activeFile}
</div>
<pre style={{
color: '#d4d4d4',
margin: 0,
fontSize: '14px',
lineHeight: '1.5',
overflow: 'auto'
}}>
{fileContents[activeFile] || '// Select a file from the list'}
</pre>
</div>
</div>
<div style={{
marginTop: '30px',
padding: '20px',
backgroundColor: '#fff3e0',
borderRadius: '8px'
}}>
<h3 style={{ color: '#e65100', marginTop: 0 }}>Startup commands</h3>
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(2, 1fr)', gap: '15px' }}>
<div style={{ backgroundColor: 'white', padding: '15px', borderRadius: '8px' }}>
<code style={{ color: '#e65100' }}>npm run dev</code>
<p style={{ margin: '5px 0 0 0', fontSize: '14px', color: '#666' }}>
Starts the development server
</p>
</div>
<div style={{ backgroundColor: 'white', padding: '15px', borderRadius: '8px' }}>
<code style={{ color: '#e65100' }}>npm run build</code>
<p style={{ margin: '5px 0 0 0', fontSize: '14px', color: '#666' }}>
Builds the production version
</p>
</div>
</div>
</div>
</div>
);
}
export default function App() {
return <ViteProjectDemo />;
}Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is Vite used for in the React ecosystem?
2. What is hot reloading in React?
Hands-on tasks in the game
- Click in order
Arrange the steps for creating your first React application:
- Vertical ordering
Arrange the stages of code transpilation by Babel from top to bottom:
- Code editor
Create a simple React App component that displays a heading and a welcome paragraph.
- Click in order
Build the command to create a new React project with Vite: