Skip to content
beginnerPhase 36 · React

Why React

Understand React's component model, declarative UI, and virtual DOM benefits.

30m
0 problems
Topic Progress0%

Component Model

Component Model

React uses a component-based architecture where UIs are built from small, reusable pieces called components.

What is a Component?

A component is a self-contained piece of UI that can:

  • Accept inputs (props)
  • Manage internal state
  • Render UI
  • Be composed with other components

Component Example

function Welcome({ name }) {
  return <h1>Hello, {name}!</h1>;
}

// Usage
<Welcome name="Alice" />
<Welcome name="Bob" />

Benefits

  • Reusability: Use components across your app
  • Separation of concerns: Each component handles one thing
  • Testability: Components can be tested in isolation
  • Maintainability: Changes are localized to components

Declarative UI

Declarative UI

React is declarative—you describe what the UI should look like, not how to update it.

Imperative vs Declarative

// Imperative (vanilla JS)
const element = document.createElement("div");
element.textContent = "Hello";
element.className = "greeting";
document.body.appendChild(element);

// When state changes
if (isVisible) {
  element.style.display = "block";
} else {
  element.style.display = "none";
}
// Declarative (React)
function Greeting({ name, isVisible }) {
  if (!isVisible) return null;
  return <div className="greeting">Hello, {name}!</div>;
}

// Just call it again when state changes
<Greeting name="Alice" isVisible={true} />

Benefits

  • Easier to understand: Code reads like a description of UI
  • Less bugs: No manual DOM manipulation
  • Better state management: UI automatically reflects state

Virtual DOM

Virtual DOM

React uses a Virtual DOM to efficiently update the real DOM.

How It Works

  1. State changes trigger a re-render
  2. React creates a Virtual DOM tree (lightweight copy)
  3. React diffs the new tree against the previous one
  4. React reconciles only the changed parts to the real DOM

Benefits

  • Performance: Minimizes expensive DOM operations
  • Batching: Multiple state changes are batched into one update
  • Cross-platform: Virtual DOM enables React Native

Simple Example

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>
    </div>
  );
}

When count changes:

  1. React creates new Virtual DOM with updated count
  2. React compares with previous Virtual DOM
  3. Only the <p> tag's text content is updated
  4. The button and div are not touched

Practice Problems

0/3solved
Build Why React Component

Create a reusable React component implementing Why React. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Why React Testing

Write unit and integration tests for Why React using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Why React Performance

Optimize Why React for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What is the core concept of React's architecture?

Question 1 options

2. What does 'declarative' mean in React?

Question 2 options

3. What is the Virtual DOM?

Question 3 options

4. Why is React's approach better than manual DOM manipulation?

Question 4 options

Flashcards

Question

What is a React component?

Answer

A reusable piece of UI that accepts props and returns JSX

Question

What is declarative UI?

Answer

Describing what the UI should look like, not how to update it

Question

What is the Virtual DOM?

Answer

A lightweight copy of the real DOM for efficient updates

Question

What is reconciliation?

Answer

The process of comparing Virtual DOM trees and updating the real DOM

Question

What is Why React?

Answer

Why React is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.React uses a component-based architecture
  • 2.Declarative UI describes what the UI should look like
  • 3.Virtual DOM enables efficient updates
  • 4.Components are reusable and composable
  • 5.React handles DOM manipulation automatically

Interview Tips

  • Explain the difference between imperative and declarative approaches
  • Describe how the Virtual DOM works
  • Discuss benefits of component-based architecture

Cheat Sheet

Cheat Sheet

Component Model

  • Reusable UI pieces
  • Accept props, manage state
  • Can be composed together

Declarative UI

  • Describe what UI should look like
  • React handles DOM updates
  • Less manual DOM manipulation

Virtual DOM

  1. State changes trigger re-render
  2. New Virtual DOM created
  3. Diff against previous tree
  4. Update only changed parts

Benefits

  • Reusability
  • Maintainability
  • Performance
  • Testability