If you are stepping into modern web development, you have undoubtedly heard of React. Created by Facebook in 2013, React completely revolutionized how developers build user interfaces (UIs).
But why was it created? What problem did it actually solve? And what makes it so powerful? Let’s break down the core principles of React.js in a way that makes sense, even if you are just starting out.
![]()
The Problem: The "Spaghetti DOM" Nightmare
Before React, developers used Vanilla JavaScript or libraries like jQuery to build interactive websites. The standard approach was Imperative Programming: you had to give the browser step-by-step instructions on how to change the screen.
Imagine a simple "Like" button. In the old days, the logic looked like this:
- Find the button element in the HTML (the DOM).
- Listen for a click.
- When clicked, find the text element showing the number of likes.
- Read that number, convert it to math, add 1.
- Inject the new number back into the HTML element.
- Change the button's color to blue.
As applications grew (like Facebook's massive newsfeed), managing thousands of these manual DOM updates became a nightmare. Code became tangled "spaghetti," and bugs were everywhere—like a message showing as "read" in one part of the screen but "unread" in another.
React was born to solve this chaos.
Core Principle 1: Declarative UI
React shifts the paradigm from Imperative to Declarative.
Instead of telling the browser how to update the UI step-by-step, you simply tell React what the UI should look like based on the current data (state). React figures out the most efficient way to update the screen.
Analogy:
- Imperative (Old way): Going into a kitchen, chopping tomatoes, kneading dough, baking it at 400 degrees to make a pizza.
- Declarative (React): Ordering a pizza from a menu. You declare what you want; the kitchen (React) handles the steps to make it happen.
Core Principle 2: Component-Based Architecture
React forces you to break your UI down into small, isolated, and reusable pieces called Components.
Think of components as custom Lego bricks. You build a Navbar brick, a Sidebar brick, and a Button brick, and then snap them together to build a complex page.
In React, a component is just a JavaScript function that returns UI (using a syntax called JSX, which lets you write HTML inside JavaScript).
// A simple reusable Button component
function CustomButton() {
return (
<button className="bg-blue-500 text-white padding-4">
Click Me!
</button>
);
}
// Using the component to build a page
function App() {
return (
<div>
<h1>Welcome to my website</h1>
<CustomButton />
<CustomButton />
</div>
);
}
Core Principle 3: State and Reactivity
If components are Lego bricks, State is the memory inside those bricks. State holds data that might change over time (like user input, a fetched list of posts, or whether a modal is open).
When a component's state changes, React automatically re-renders that component to reflect the new data. This is called reactivity. You never have to manually touch the HTML again.
Here is how you add state using React's useState hook:
import { useState } from 'react';
function LikeButton() {
// 1. Declare a state variable 'likes', starting at 0.
// 2. 'setLikes' is the function used to update it.
const [likes, setLikes] = useState(0);
return (
<div>
<p>This post has {likes} likes.</p>
{/* When clicked, we just update the state. React handles the UI update! */}
<button onClick={() => setLikes(likes + 1)}>
👍 Like
</button>
</div>
);
}
Core Principle 4: The Virtual DOM
Directly manipulating the browser's actual DOM (Document Object Model) is slow and computationally expensive. If you have a list of 1,000 items and one item changes, older frameworks would often re-draw the entire list.
React solves this with the Virtual DOM.
The Virtual DOM is a lightweight copy of the actual UI kept in the browser's memory. When a component's state changes:
- React creates a new Virtual DOM.
- It compares the new Virtual DOM with the old Virtual DOM (a process called "diffing").
- It figures out exactly which tiny piece of the UI changed.
- It updates only that specific piece in the real browser DOM.
This makes React incredibly fast and efficient, even for massive applications.
Core Principle 5: Unidirectional Data Flow
In React, data only flows in one direction: downwards, from parent components to child components.
You pass data down using something called Props (short for properties).
// Child Component receives 'name' as a prop
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Parent Component passes the data down
function App() {
return (
<div>
<Greeting name="Alice" />
<Greeting name="Bob" />
</div>
);
}
Because data only flows down, it is much easier to debug your application. If a child component is showing the wrong data, you know exactly where to look: up the tree to the parent that provided it.
Summary
React won the frontend wars because it offered a highly logical, elegant solution to UI complexity. By combining Declarative UIs, Reusable Components, Automated State Reactivity, and the blazing-fast Virtual DOM, React allows developers to build massive, interactive applications without descending into spaghetti code.
Once you grasp the concept that UI is simply a reflection of your Data (State), you are ready to master React.