Skip to content
FrontDepth, Home

2013 – today

React and the virtual DOM

Components describe the UI from their state; on every change React renders again and updates only the DOM that differs.

JuniorMiddleRuns on: React 19.3.0, React DOM 19.3.0

The problem

Keeping the page in step with the data was the hard part. With jQuery or Backbone views, code had to find the right elements and patch them after every change. With AngularJS's two-way binding, one change could set off watchers that changed other values, and in a large app it was hard to tell what had updated what.

The idea

React treats the UI as a function of the state: render() returns a description of what the page should look like now, as React elements written in JSX. When this.setState changes the state, React calls render() again, compares the new elements with the previous ones (the virtual DOM, kept in memory) and applies only the differences to the real DOM. This process is called reconciliation.

Data flows one way. App owns the state and passes pieces of it down as props; a TodoItem cannot change its todo, it calls the onToggle callback its parent gave it.

What hurt next

State that several components need has to live in their closest common parent and travel down as props through every component in between, even those that only pass it on, while callbacks travel down the same way to change it. In a large app, setState calls in many components, triggered by clicks, server responses and timers, all changed what the screen showed, so it was hard to follow why it looked the way it did. Flux and Redux moved the shared state out of the components into a store that changes only through actions.

Next era: Flux and Redux

How close this is to the original

Runs React 19.3.0. Class components, this.state and this.setState work as they did in 2015–2018; react.dev still documents them but recommends functions for new code. The demo uses ES6 classes (React 0.13) with handlers bound in the constructor, as the docs showed; class fields (handleSubmit = () => {}) were the other common way. React.createClass, the original way to define components, left React in version 16 (it lives on as the create-react-class package). The files import React as code of the time did, when JSX compiled to React.createElement; here JSX compiles with the automatic runtime (React 17), which no longer needs it. The entry is newer than the era: it uses createRoot (React 18), because React 19 removed ReactDOM.render.

The todo app in this era