React · Lesson 1 of 15 · 3 min
Introduction to React & Development Environment
This lesson covers the "why React?" and "what is the Virtual DOM?" questions that open almost every frontend interview.
Why React Exists: Declarative UI
React's main purpose is to let you describe your UI declaratively. You tell React what the UI should look like for a given state, not how to update the screen step-by-step.
With vanilla JavaScript, you write imperative code: find an element, create a new one, set its text, append it. This gets complex and error-prone as the UI grows. You manage both the state and the DOM mutations yourself.
React abstracts this away. You return a description of the UI from your component, and React handles the DOM updates. This makes your code more predictable and easier to reason about.
The interview question: "What problem does React solve?" or "Explain declarative vs. imperative UI." Your answer should focus on this shift in thinking. You describe the end state, and the library figures out the most efficient way to get there. This reduces bugs and cognitive overhead.
The Core Abstraction: Components
React builds UIs from small, reusable pieces called components. A component is typically a JavaScript function that returns a description of a piece of the UI, written in JSX.
Think of a web page as a tree of components. You might have a NavBar component, which contains NavButton components. The entire HomePage is a component that composes these smaller ones. This composition is powerful and is the foundation of building complex applications.
This model encourages you to build a design system of reusable parts, which improves consistency and development speed.
How React is Fast: The Virtual DOM
Directly manipulating the browser's DOM is slow. React avoids this bottleneck with an in-memory representation of the UI called the Virtual DOM (VDOM).
When your application's state changes, React follows a process called reconciliation:
- It creates a new VDOM tree describing the next UI state.
- It "diffs" this new tree against the previous one to find the minimum number of changes.
- It batches these changes and applies them to the real DOM in one go.
This prevents layout thrashing and makes updates feel fast, even in complex applications.
The trap: Don't just say "the Virtual DOM is faster." It's not always faster for initial render or trivial updates. Its primary benefit is enabling a declarative API by abstracting away manual DOM manipulation. This, in turn, leads to better performance on complex, dynamic UIs because the diffing algorithm is highly optimized.
Setting Up a Modern React Project
The old standard, create-react-app, is now legacy. For new projects, the community standard is Vite. It's significantly faster for development because it uses native ES modules and a super-fast bundler (esbuild).
To create a new project, run this in your terminal:
npm create vite@latest my-react-app -- --template react
This scaffolds a minimal, modern React project. Navigate into the directory (cd my-react-app), install dependencies (npm install), and start the dev server (npm run dev).
The two key files are src/main.jsx (the entry point) and src/App.jsx (the root component). Here's a simplified App.jsx that shows the basic structure. Note the .jsx extension, which is standard practice for files containing JSX.
// src/App.jsx
import reactLogo from './assets/react.svg'
import './App.css'
function App() {
const appName = "My First React App";
return (
<div className="App">
<header className="App-header">
<img src={reactLogo} className="logo react" alt="React logo" />
<h1>{appName}</h1>
<p>
Edit <code>src/App.jsx</code> and save to see changes.
</p>
</header>
</div>
)
}
export default App;
Notice className instead of class. This is because JSX is JavaScript, and class is a reserved keyword. This is a common beginner mistake and a quick check for attention to detail.
Try it
Scaffold a new React project using Vite. Delete all the boilerplate code inside the App component's return statement. Replace it with an h1 containing your name and a p tag that says "My first React app!".
Next: We'll properly break down the syntax you just used and build custom components in the next lesson on JSX, Components & Props.
Lock it in — apply this lesson under interview conditions.