Skip to content
FrontDepth, Home

Component frameworks (Vue, Angular, Svelte) → Hooks

2014 – today · 2018 – today

Older era
Newer era
Compare

What changed

Where the state lives

Component frameworks (Vue, Angular, Svelte)

In the component's data(): plain objects and arrays that Vue makes reactive. shown and left are computed, cached until the data they read changes.

Hooks

In useState: the list lives in the useTodos() hook, the filter and the text field in the component.

How the page is rendered

Component frameworks (Vue, Angular, Svelte)

Vue compiles the HTML template (written in the page here) into a render function. When data it read changes, the component re-renders and Vue patches only what differs.

Hooks

React renders the whole App from state on every change and updates only the DOM that differs.

What happens on a change

Component frameworks (Vue, Angular, Svelte)

Methods and v-model change the data directly (this.todos.push, todo.done); Vue buffers the DOM update until the next tick, a microtask.

Hooks

A handler calls a setter with a new array; React re-runs the component and recomputes shown and left.

How the code is organised

Component frameworks (Vue, Angular, Svelte)

One component object of options (data, computed, methods) plus its template. Larger apps nest such components into a tree.

Hooks

A function component plus a custom hook: logic is grouped by feature and can be reused.

Why it changed

What hurt in each era along the way, and the era that came next.

  1. Component frameworks (Vue, Angular, Svelte)→SSR, SSG and meta-frameworks

    These apps still rendered in the browser. The server sent an almost empty page, and nothing useful appeared until the framework and the app's JavaScript had downloaded and run; crawlers and link previews that do not run JavaScript saw nothing. Routing, server rendering, code splitting and data loading were left to each team to put together. Meta-frameworks such as Next.js and Nuxt bundled those pieces around the component framework and rendered pages on the server or at build time.

  2. SSR, SSG and meta-frameworks→Hooks

    Pages now arrived rendered, but the components were still classes: one feature's logic was spread over componentDidMount, componentDidUpdate and componentWillUnmount, sharing stateful logic needed higher-order components or render props, and every handler had to be bound to this (see the constructor). Hooks (React 16.8, 2019) gave function components state and effects that can be shared as custom hooks.

    Hydration had its own cost: the browser downloads and runs the code of the whole page, even of parts that never change, and the data travels twice, as HTML and as JSON. Islands and server components later went after that.

The same app, side by side

The main file of each version. Both apps pass the same test: same markup, same behaviour, different ideas.

The apps run in sandboxes in your browser.

Component frameworks (Vue, Angular, Svelte)
Hooks

Component frameworks (Vue, Angular, Svelte)

Vue 3.5.45

/src/main.js · 41 lines

JavaScript
// Component-framework era (Vue): the component declares its state, derived values and
// methods; the HTML template in index.html describes the page. Vue keeps the two in sync.
import { createApp } from "vue";

let nextId = 3;

createApp({
  // data() returns the state. Vue makes it reactive: while rendering it records which
  // properties were read, and any later change to them re-renders the component.
  data() {
    return {
      todos: [
        { id: 1, title: "Read the docs", done: true },
        { id: 2, title: "Write the app", done: false },
      ],
      filter: "all",
      title: "",
    };
  },

  // Computed values are cached and re-evaluated only when the data they read changes.
  computed: {
    shown() {
      return this.todos.filter((t) => this.filter === "all" || (this.filter === "done") === t.done);
    },
    left() {
      return this.todos.filter((t) => !t.done).length;
    },
  },

  methods: {
    addTodo() {
      const title = this.title.trim();
      if (!title) return;
      // A plain push is enough: no new array, no setState. Vue sees the change itself.
      this.todos.push({ id: nextId++, title, done: false });
      this.title = "";
    },
  },
  // There is no template option, so mount() uses the HTML inside #app as the template.
}).mount("#app");

Hooks

React 19.3.0, React DOM 19.3.0

/src/App.jsx · 73 lines

JSX
// Hooks era: function components hold state with useState; the UI is computed from it.
import { useState } from "react";
import { useTodos } from "./useTodos.js";

const FILTERS = { all: "All", active: "Active", done: "Done" };

export default function App() {
  const { todos, add, toggle } = useTodos();
  const [filter, setFilter] = useState("all");
  const [title, setTitle] = useState("");

  // Derived values are computed on every render, not stored as state.
  const shown = todos.filter((t) => filter === "all" || (filter === "done") === t.done);
  const left = todos.filter((t) => !t.done).length;

  function handleSubmit(event) {
    event.preventDefault();
    const text = title.trim();
    if (!text) return;
    add(text);
    setTitle("");
  }

  return (
    <main className="todo-app">
      <h1>Todos</h1>
      <form id="todo-form" onSubmit={handleSubmit}>
        <input
          id="new-todo"
          value={title}
          onChange={(event) => setTitle(event.target.value)}
          placeholder="What needs doing?"
          autoComplete="off"
          aria-label="New todo"
        />
        <button id="add" type="submit">
          Add
        </button>
      </form>
      <ul id="todo-list">
        {shown.map((todo) => (
          <li key={todo.id} className={todo.done ? "done" : undefined}>
            <label>
              <input
                type="checkbox"
                data-toggle
                checked={todo.done}
                onChange={() => toggle(todo.id)}
              />{" "}
              <span className="title">{todo.title}</span>
            </label>
          </li>
        ))}
      </ul>
      <div className="footer">
        <span id="todo-count">{left} left</span>
        <div className="filters">
          {Object.entries(FILTERS).map(([name, label]) => (
            <button
              key={name}
              type="button"
              data-filter={name}
              className={filter === name ? "selected" : undefined}
              onClick={() => setFilter(name)}
            >
              {label}
            </button>
          ))}
        </div>
      </div>
    </main>
  );
}