# Declarative vs. Imperative UI

> Stop writing steps to mutate the DOM. Start describing the final state.

Subject: React | Lesson 1 of 48 | 3 min read
Interactive lesson: https://codewithanimation.in/react/declarative-ui
Next: [JSX & Compilation](https://codewithanimation.in/react/jsx-and-compilation.md)

## What it is

In traditional web development, you write **imperative** code. You write exact, step-by-step instructions telling the browser how to mutate the DOM: find this element, add this class, update this text node.

React introduces a **declarative** model. Instead of writing steps, you describe what the UI should look like for a given state. When the state changes, React figures out the exact steps needed to update the DOM to match your description.

## Why it matters

Imperative code becomes impossible to manage as an application grows. If five different events can change a button's state, you have to write manual DOM updates for every possible transition.

By using a declarative model, you eliminate an entire class of bugs. You no longer worry about how the UI transitions from one state to another. You simply declare the rules, and React guarantees the DOM matches your state.

## How it works

Imagine a button that turns blue when active. In vanilla JavaScript, you would query the element and manually call `classList.add('blue')` when clicked.

In React, you define a variable (like `isActive`). Then, you conditionally render the class name inside your JSX: `className={isActive ? 'blue' : ''}`.

React watches that variable. The moment it flips to true, React automatically updates the real DOM for you.

## Common mistake: The manual mutation trap

Never mix manual DOM mutations (like `document.getElementById().style`) inside a React component. If you manually change a node that React is managing, React's internal map of the DOM gets out of sync with reality, leading to unpredictable UI bugs.

## Tip: Think of UI as a pure function

The best React developers treat the UI as a pure mathematical function of their data. If you can define all possible visual states purely based on variables, React will handle all the messy transitions.

## Try it: The Paradigm Shift

Compare the traditional imperative approach (left) to the modern declarative React approach (right). Notice how React never touches the DOM directly.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/declarative-ui

## Check yourself

### Question 1

What is the primary difference between declarative and imperative programming in UI?

- A. Declarative describes what the UI should look like; imperative describes the steps to build it.
- B. Declarative is significantly faster for the browser to render.
- C. Declarative means writing HTML instead of JavaScript.

Correct answer: A

Imperative code issues step-by-step commands to mutate the DOM. Declarative code simply describes the final desired state and lets the framework figure out how to achieve it.

- Why not B: Declarative is an abstraction for the developer. Under the hood, React still uses imperative DOM methods to actually update the page.
- Why not C: Declarative refers to the paradigm of describing state, not the file type. You can write declarative code entirely in JavaScript.

### Question 2

Why is it dangerous to manually mutate the DOM using `document.getElementById` inside a React component?

- A. It causes a security vulnerability.
- B. React's internal state will fall out of sync with the real DOM, causing UI bugs.
- C. The browser will throw a strict SyntaxError.

Correct answer: B

React relies on a Virtual DOM to track changes. If you manually mutate the real DOM, React doesn't know about it, causing its internal representation to mismatch the screen.

- Why not A: DOM manipulation itself is not a security flaw, but it fundamentally breaks React's reconciliation engine.
- Why not C: The browser allows it perfectly fine, but React will get confused when it tries to update a node you changed behind its back.

### Question 3

How should you handle a button that needs to toggle an 'active' CSS class?

- A. Use a ref to find the button and call `classList.add('active')`.
- B. Write a CSS pseudo-class like `:active`.
- C. Store a boolean in state and conditionally render the class name.

Correct answer: C

In a declarative model, the UI is a reflection of state. You store an `isActive` boolean and let React compute the final class name based on that value.

- Why not A: That is an imperative approach. In React, you want to drive the class via state.
- Why not B: While `:active` styles the physical click moment, it does not represent persistent application state.

## Remember this

- React is a declarative library. You describe the UI, React handles the DOM updates.
- Never mix manual DOM mutations (`document.querySelector`) with React components.
- Always treat your UI as a pure reflection of your data.
