Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Setting Up Your React Dev Environment with Vite

Recommended resources

Welcome to the beginning of your journey to build a web component library! This course is designed to equip you with the essential skills in React, starting from the ground up, to achieve that goal. In this first module, we'll focus on the absolute fundamentals of React.

Today's lesson marks the very first step: setting up a professional development environment. By the end of this session, you will be able to create a brand new React project on your local machine using a modern tool called Vite. We will explore why Vite is the tool of choice for many developers today and walk through the entire setup process, from the command line to seeing your first React application running in the browser.

What is Vite and Why Do We Use It?

Before we start typing commands, it's important to understand the "why" behind our tools. In web development, a "build tool" is essential for taking the many source files we write (JavaScript, CSS, images) and bundling them into an optimized format that a browser can understand efficiently. For years, the go-to tool for React projects was Create React App (CRA), which uses a bundler called Webpack under the hood.

However, as applications grow, Webpack can become slow. Every time you save a file, it has to re-bundle a large portion of your code, which can lead to noticeable delays during development.

This is the problem that Vite (French for "fast," pronounced /vit/) solves. Vite takes a different, more modern approach. Instead of bundling everything every time you make a change, it leverages native ES modules directly in the browser during development. This results in an almost instantaneous startup time for the development server and incredibly fast updates when you change your code, a feature known as Hot Module Replacement (HMR).

To get a clear picture of how Vite achieves this speed and how it differs from traditional tools, the following video provides an excellent conceptual overview.

Vite Crash Course | Faster Alternative To CRA

Watch this segment from the "Vite Crash Course" by Traversy Media. It explains the core ideas behind Vite and contrasts its modern approach with the traditional bundling process used by tools like Create React App.

Pay close attention to the explanation from how Vite works differently in development compared to bundlers like Webpack. The video also covers the production build process from bundling for production.

Understanding this distinction is key to appreciating the streamlined and efficient development experience Vite provides, which is why we're using it as the foundation for our component library.


Setting Up Your First React Project

Now that you understand the "why," let's get our hands dirty with the "how." The process involves using npm, the Node Package Manager, which will be familiar from your experience with Node.js.

Prerequisites

Vite requires a recent version of Node.js to be installed (version 18 or higher is recommended). You can quickly check your installed version by running the following command in your terminal:

node -v

If you have a compatible version, you're ready to proceed.

Creating the Application

We'll follow the official recommended process for scaffolding a new project. The following resource from the Mozilla Developer Network (MDN) is an excellent, reliable guide for this.

Getting started with React - Learn web development

This MDN article provides a clear, step-by-step guide to initializing a new React application using Vite. We will follow its instructions to create our project.

Please read the section on setting up. Follow the instructions to: Run npm create vite@latest in your terminal. You'll be prompted to enter a project name (e.g., react-component-library), select React as the framework, and JavaScript as the variant. Navigate into your new project directory using cd <project-name>. Install the necessary dependencies with npm install. Start the development server using npm run dev.

Once the development server is running, your terminal will display a local URL, typically http://localhost:5173. Open this URL in your web browser. You should see the default Vite + React starter page.

This is the default application you should see in your browser after successfully starting the development server.

Congratulations, you now have a fully functional React development environment!


Exploring the Project Structure

A key part of being productive is understanding the layout of your project. Vite creates a clean and intuitive file structure. Let's briefly examine the most important files and folders.

The MDN article you just read provides a good overview of this structure.

Getting started with React - Learn web development

This part of the MDN article explains the purpose of the initial files and directories generated by Vite.

Read the section titled Application structure. Focus on understanding the roles of: index.html: The main HTML file in the project root. src directory: Where all your application's source code will live. src/main.jsx: The entry point of your React application. package.json: Manages your project's dependencies and scripts.

To summarize the key points:

  • package.json: Defines your project's metadata, dependencies (react, react-dom), development dependencies (vite), and scripts (dev, build, preview). Your experience with Node.js makes you familiar with its role.
  • vite.config.js: This is the configuration file for Vite. You can use it to customize Vite's behavior, but for now, the default settings are perfect.
  • index.html: This is the actual page served to the browser. Unlike older setups, it's located in the root of your project, not in a public folder. Notice the <script type="module" src="/src/main.jsx"></script> tag, which is the magic that allows Vite to use native ES modules.
  • src/: This is where you'll spend most of your time. It contains all the source code for your React application.
  • src/main.jsx: This is the main JavaScript file. It finds the <div> with the ID of root in index.html and tells React to render your main application component (<App />) inside it.

This simple and logical structure is one of the many reasons developers enjoy working with Vite.


Conclusion

In this lesson, we laid the critical groundwork for our entire course. You've not only set up a modern React development environment but also understood the principles that make it so fast and efficient.

Here are the key takeaways:

  • Vite is a modern build tool that provides a significantly faster development experience compared to older tools like Create React App, primarily by using native ES modules in the browser.
  • You can scaffold a new React project with a single command: npm create vite@latest.
  • The core development workflow involves three commands: cd <project-folder>, npm install to set up dependencies, and npm run dev to start the live development server.
  • The project structure is straightforward, with index.html as the browser entry point and the src folder containing all your React application code.

Now that your environment is ready, our next lesson will dive into the code itself. We'll explore JSX, the special syntax that allows us to write HTML-like structures directly within our JavaScript files, and understand how it is fundamental to building React components.