React Fundamentals
Introduction to React
What is React?
React is a JavaScript library for building user interfaces. Think of it like a set of Lego blocks for websites. Instead of building a whole webpage in one massive file, you create small, reusable pieces called components. A component could be anything from a single button to an entire search bar or a user profile.
React allows us to create reusable UI components.
You can then assemble these components to build complex interfaces. This approach keeps your code organized and easy to manage. If you need to change how a button looks, you only have to update the button component, and the change will apply everywhere that button is used.
React is also declarative. This means you tell React what you want the user interface to look like based on its current state, and React handles the rest. You don't have to write step-by-step instructions to update the page. You just change the state, and the UI automatically updates to match. It's like telling a GPS your destination instead of giving it every single turn-by-turn direction.
The Virtual DOM
Web browsers use a structure called the Document Object Model (DOM) to represent a webpage. The DOM is like a tree of all the HTML elements on the page. Directly changing this tree can be slow, especially for complex applications with lots of updates, like a social media feed.
React introduces a clever solution: the Virtual DOM. It's a lightweight copy of the real DOM that exists only in memory.
When the state of a component changes, React first updates the Virtual DOM. Then, it compares this new Virtual DOM with a snapshot of the old one. This comparison process is called "diffing."
React identifies exactly which parts of the UI have changed and then makes the smallest possible number of updates to the real DOM. This efficiency is what makes React applications feel so fast and responsive.
Setting Up Your Space
The fastest way to start a new React project is with a tool called Create React App. It sets up a complete development environment for you with just one command. To use it, you'll need to have Node.js and npm (Node Package Manager) installed on your computer. Npm comes bundled with Node.js.
Once that's ready, open your terminal or command prompt and run the following commands.
npx create-react-app my-first-react-app
cd my-first-react-app
npm start
Here’s what’s happening:
npx create-react-app my-first-react-appcreates a new directory calledmy-first-react-appwith all the necessary files and configurations.cd my-first-react-appnavigates you into that new directory.npm startlaunches a local development server. A new browser tab will open with your running React app.
Your First Component
If you open the src folder in your new project, you'll find a file named App.js. This is your first React component. It's a JavaScript function that returns some HTML-like code. This syntax is called JSX (JavaScript XML), and it lets you write markup directly inside your JavaScript files.
import React from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
Try changing the text inside the <p> tag to "Hello, World!". Save the file, and you'll see your browser update automatically. Congratulations, you've just edited your first React component!
Notice that instead of class, JSX uses className. This is because class is a reserved word in JavaScript. There are a few small differences like this, but for the most part, JSX looks just like HTML.
What is the primary purpose of the React library?
Why does React use a Virtual DOM?
