React is a popular JavaScript library for building user interfaces, developed by Facebook. React’s main purpose is to simplify the creation of dynamic, interactive web applications with large amounts of data that changes in real-time.
React solves the following key problems:
React is a library for building user interfaces that allows developers to create web applications with dynamic content. Unlike frameworks (such as Angular), React focuses only on the view layer.
React was introduced by Facebook in 2013 and has since become one of the most popular technologies for frontend development. Here are React’s key features:
// Example of a simple React component
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Using the component
const element = <Welcome name="Alexander" />;
One of the main problems in web development is slow DOM operations. Every change to the DOM triggers a page repaint, which can be resource-intensive.
The Problem:
// Traditional approach - direct DOM manipulation
document.getElementById('counter').innerHTML = 'Counter: ' + count;
document.getElementById('counter').style.color = count > 10 ? 'red' : 'black';
React Solution:
// React uses Virtual DOM for optimization
function Counter({ count }) {
return (
<div style={{ color: count > 10 ? 'red' : 'black' }}>
Counter: {count}
</div>
);
}
React solves this problem through Virtual DOM:
In traditional web applications, managing interface state becomes complex as the application grows.
The Problem:
// Without React - difficult to track state
let isVisible = true;
let items = [];
let selectedId = null;
// When any value changes, DOM needs to be updated manually
function toggleVisibility() {
isVisible = !isVisible;
// Update DOM manually
document.getElementById('dropdown').style.display =
isVisible ? 'block' : 'none';
}
React Solution:
// With React - state is managed declaratively
function Dropdown() {
const [isVisible, setIsVisible] = useState(true);
return (
<div>
<button onClick={() => setIsVisible(!isVisible)}>
Toggle
</button>
{isVisible && <div>Dropdown content</div>}
</div>
);
}
Creating reusable UI components in traditional JavaScript requires a lot of boilerplate code.
The Problem:
// Without React - complex reusability
function createButton(text, onClick, type) {
const button = document.createElement('button');
button.textContent = text;
button.addEventListener('click', onClick);
button.className = 'btn btn-' + type;
return button;
}
// For each new button, needs to be created from scratch
const saveButton = createButton('Save', saveHandler, 'primary');
const cancelButton = createButton('Cancel', cancelHandler, 'secondary');
React Solution:
// With React - simple creation of reusable components
function Button({ text, onClick, type = 'primary' }) {
return (
<button className={`btn btn-${type}`} onClick={onClick}>
{text}
</button>
);
}
// Easy to reuse the component
<Button text="Save" onClick={saveHandler} type="primary" />
<Button text="Cancel" onClick={cancelHandler} type="secondary" />
Managing asynchronous operations (API requests, timers) in traditional JavaScript requires a lot of attention to details.
The Problem:
// Without React - difficult to manage asynchronous operations
let users = [];
let isLoading = false;
function loadUsers() {
isLoading = true;
// Update DOM manually
document.getElementById('loading').style.display = 'block';
fetch('/api/users')
.then(response => response.json())
.then(data => {
users = data;
isLoading = false;
// Update DOM manually
document.getElementById('loading').style.display = 'none';
renderUsers(users);
})
.catch(error => {
isLoading = false;
// Update DOM manually
document.getElementById('loading').style.display = 'none';
document.getElementById('error').textContent = error.message;
});
}
React Solution:
// With React - declarative state management
function UserList() {
const [users, setUsers] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
setIsLoading(true);
fetch('/api/users')
.then(response => response.json())
.then(data => {
setUsers(data);
setIsLoading(false);
})
.catch(error => {
setError(error.message);
setIsLoading(false);
});
}, []);
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
return (
<ul>
{users.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
Components are the building blocks of React applications. They allow you to divide the UI into independent, reusable parts.
// Functional component
function UserProfile({ user }) {
return (
<div className="user-profile">
<img src={user.avatar} alt={user.name} />
<h2>{user.name}</h2>
<p>{user.email}</p>
</div>
);
}
// Can use the component multiple times
<UserProfile user={user1} />
<UserProfile user={user2} />
State is data that can change over time and affects component rendering.
function Counter() {
// useState - hook for state management
const [count, setCount] = useState(0);
return (
<div>
<p>Counter: {count}</p>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
</div>
);
}
Props are a way to pass data from parent component to child component.
function Greeting({ name, age }) {
return (
<div>
<h1>Hello, {name}!</h1>
<p>Your age: {age}</p>
</div>
);
}
// Pass data through props
<Greeting name="Alexander" age={25} />
React manages component lifecycle, allowing code execution at different stages.
function DataFetcher({ userId }) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
// useEffect - hook for side effects
useEffect(() => {
// Executes on mount and update
setLoading(true);
fetchData(userId)
.then(result => {
setData(result);
setLoading(false);
});
// Cleanup function (executes on unmount)
return () => {
// Cancel requests, clear timers, etc.
};
}, [userId]); // Dependencies - component updates when they change
if (loading) return <div>Loading...</div>;
return <div>{JSON.stringify(data)}</div>;
}
Thanks to Virtual DOM and the reconciliation algorithm, React minimizes operations with the real DOM.
React allows you to describe how the interface should look depending on the state, rather than how to change it.
// Declarative approach
function TodoList({ todos }) {
return (
<ul>
{todos.map(todo => (
<li key={todo.id} className={todo.completed ? 'completed' : ''}>
{todo.text}
</li>
))}
</ul>
);
}
React has a huge community and rich ecosystem of libraries:
The component-based approach allows creating libraries of reusable components.
React Native allows using React knowledge to create mobile applications.
React is particularly useful in the following cases:
React is a powerful library for building user interfaces that solves key web development problems:
Main Advantages:
Key Concepts:
React doesn’t solve all frontend development problems, but significantly simplifies creating complex interactive interfaces, making code more predictable and maintainable.
Want more articles for interview preparation? Subscribe to EasyAdvice, bookmark the site, and improve yourself every day 💪