What is React and what problems does it solve?

👨‍💻 Frontend Developer 🟢 Almost Certain 🎚️ Medium
#React

Brief Answer

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:

  • Simplifies user interface state management
  • Ensures high performance through Virtual DOM
  • Enables creation of reusable components
  • Simplifies development of complex interactive interfaces

What is React

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.

Key Characteristics

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:

  1. Component-based approach — interface is divided into independent, reusable components
  2. Virtual DOM — virtual representation of DOM for update optimization
  3. Unidirectional data flow — data flows from top to bottom, simplifying debugging
  4. JSX — JavaScript syntax extension for describing UI
// Example of a simple React component
function Welcome(props) {
  return <h1>Hello, {props.name}!</h1>;
}
 
// Using the component
const element = <Welcome name="Alexander" />;

Problems React Solves

1. DOM Manipulation Performance

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:

  • Creates a virtual representation of the DOM in memory
  • When state changes, calculates minimal changes
  • Applies only necessary updates to the real DOM

2. State Management Complexity

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>
  );
}

3. Component Reusability

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" />

4. Complexity of Asynchronous Operations

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>
  );
}

Core React Concepts

1. Components

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} />

2. State

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>
  );
}

3. Props (Properties)

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} />

4. Component Lifecycle

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>;
}

React Advantages

1. High Performance

Thanks to Virtual DOM and the reconciliation algorithm, React minimizes operations with the real DOM.

2. Declarative Nature

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>
  );
}

3. Rich Ecosystem

React has a huge community and rich ecosystem of libraries:

  • React Router for routing
  • Redux for state management
  • Styled Components for styling
  • Create React App for quick start

4. Reusability

The component-based approach allows creating libraries of reusable components.

5. Mobile Development Support

React Native allows using React knowledge to create mobile applications.


When to Use React

React is particularly useful in the following cases:

  1. Complex interactive interfaces — applications with frequent data updates
  2. Single-page applications (SPA) — web applications that work like desktop applications
  3. Team development — component-based approach simplifies collaboration
  4. Long-term projects — mature ecosystem and community

When React Might Be Overkill

  1. Simple static websites — for sites without interactivity, React might be overkill
  2. SEO-critical projects — without server-side rendering, it might be problematic for SEO
  3. Small projects — for small applications, React might be too complex

Summary

React is a powerful library for building user interfaces that solves key web development problems:

Main Advantages:

  • ✅ High performance through Virtual DOM
  • ✅ Component-based approach for reusability
  • ✅ Declarative programming style
  • ✅ Rich ecosystem and community

Key Concepts:

  • Components — building blocks of the application
  • State — data that can change
  • Props — way to pass data between components
  • Lifecycle — stages of component existence

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 💪