⚛️ Getting Started with React

Chapter 2 of 15

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.

💡 Tip: Make sure you have Node.js version 14.0 or higher installed. You can check your version by running 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).

🚀 Why Vite? Vite offers instant server start, blazing-fast HMR, and optimized builds. It's becoming the industry standard for React development!

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.

⚠️ Note: Don't forget the -- 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)

✨ Success! If everything went well, you should see the React logo spinning on your screen!

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

⚡ Key Differences:
  • index.html is at root (not in public/)
  • Entry file is main.jsx (not index.js)
  • Use .jsx extension 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:

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!

🎉 Hot Module Replacement (HMR): Vite includes blazing-fast HMR, which means your changes will appear instantly in the browser without a full page refresh! This is one of Vite's killer features.

Available Vite Scripts

In your Vite project directory, you can run these commands:

⚡ Vite vs Create React App:
  • 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!