Installation
To use React in production, you need npm which is included with Node.js. If you don't have Node.js installed, download it from nodejs.org.
node --version in your terminal.
⚡ Create a React Application with Vite
The fastest and most modern way to create a React application is by using Vite - a next-generation build tool that provides instant server start and lightning-fast Hot Module Replacement (HMR).
To create a React application with Vite, run the following command in your terminal:
npm create vite@latest my-react-app -- --template react
This command will create a new folder called my-react-app with all the necessary files and folders to run a React application.
-- before --template react. This is required!
Running Your React Application
After the installation is complete, navigate to your project folder, install dependencies, and start the development server:
cd my-react-app
npm install
npm run dev
This will start the development server and open your React application in the browser at http://localhost:5173 (Vite's default port)
Vite Project Structure
After installation, your Vite project structure will look like this:
my-react-app/
├── node_modules/ (All dependencies)
├── public/ (Static assets)
├── src/
│ ├── App.jsx (Main App component)
│ ├── App.css (App styles)
│ ├── main.jsx (Entry point)
│ ├── index.css (Global styles)
│ └── assets/ (Images, fonts, etc.)
├── index.html (At root, not in public!)
├── package.json (Project configuration)
├── vite.config.js (Vite configuration)
└── README.md
Important Files
- index.html: The main HTML file at root (KEY DIFFERENCE from CRA!)
- src/main.jsx: The entry point of your React application (.jsx extension)
- src/App.jsx: The main component of your application
- vite.config.js: Vite configuration (port, plugins, etc.)
- package.json: Contains project metadata and dependencies
index.htmlis at root (not in public/)- Entry file is
main.jsx(not index.js) - Use
.jsxextension for files with JSX - Command is
npm run dev(not npm start) - Default port is 5173 (not 3000)
Understanding main.jsx
The main.jsx file is the entry point of your Vite React application:
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>,
)
Let's break this down:
- We import React and ReactDOM libraries
- We import our main App component (with .jsx extension)
- We import global CSS
- We create a root and render our App in one statement
Understanding App.jsx
The App.jsx file contains your main App component:
import React from 'react'
import './App.css'
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Welcome to React with Vite!</h1>
<p>Edit src/App.jsx and save to see changes instantly!</p>
</header>
</div>
)
}
export default App
Modifying Your First React App
Let's modify the App component to display a simple message:
function App() {
return (
<div>
<h1>Hello, React!</h1>
<p>This is my first React application.</p>
</div>
);
}
export default App;
Save the file, and the browser will automatically reload with your changes!
Available Vite Scripts
In your Vite project directory, you can run these commands:
npm run dev- Runs the app in development mode (⚡ Fast!)npm run build- Builds the app for productionnpm run preview- Preview the production build locallynpm run lint- Run ESLint to check code quality (if configured)
- Faster: Instant server start vs 20-30 seconds
- Simpler: Cleaner project structure
- Modern: ES modules, no bundling in dev
- Flexible: Easy to configure via vite.config.js
What's Next?
Now that you have your React development environment set up and running, you're ready to learn about JSX - the syntax that allows you to write HTML in JavaScript!