Flux and Redux → SSR, SSG and meta-frameworks
2014 – today · 2016 – today
What changed
- Where the state lives
Flux and Redux
In one Redux store:
{ todos, filter }, built by two reducers joined withcombineReducers. Only the half-typed text stays in the input, read through a ref.SSR, SSG and meta-frameworks
In the page component's
this.state, started from the props the server loaded withgetInitialProps(); a copy of those props travels with the HTML as JSON.- How the page is rendered
Flux and Redux
React still renders the components;
connectrendersTodoAppagain when the props it maps from the store change.SSR, SSG and meta-frameworks
The server renders the whole page to HTML with
renderToString(); the browser hydrates it withhydrateRoot()and renders every later change itself.- What happens on a change
Flux and Redux
A handler calls a prop like
onToggle(id), which dispatchestoggleTodo(id); the reducer returns a new state and the store notifies its subscribers.SSR, SSG and meta-frameworks
After hydration everything happens in the browser: a handler calls
this.setState()and React re-renders the page component. Nothing goes back to the server.- How the code is organised
Flux and Redux
Action types and creators, reducers, the store setup and a connected function component, each in its own file.
SSR, SSG and meta-frameworks
A page in
pages/written once for both sides, plus framework code: a server that renders and serializes, and a client entry that hydrates.
Why it changed
What hurt in each era along the way, and the era that came next.
Flux and Redux→Component frameworks (Vue, Angular, Svelte)
Every small feature needed the same pieces in several files: an action type constant, an action creator, a case in a reducer, and
mapStateToProps/mapDispatchToPropsto connect a component, plus objects copied by hand so that nothing was mutated. Setting up a store with middleware and the DevTools took more code still. Redux itself later answered with Redux Toolkit; component frameworks like Vue, Angular and Svelte offered reactive state you change directly and HTML templates that update themselves, with much less ceremony.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.
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.
Flux and Redux
React 19.3.0, React DOM 19.3.0, Redux 5.0.1, React Redux 9.3.1
/src/App.jsx · 91 lines
// A container component: it owns no data. connect() passes it the store's state and the actions as props.
// JSX then compiled to React.createElement, so React had to be imported; the automatic runtime used here does not need it.
import React from "react";
import { connect } from "react-redux";
import { addTodo, toggleTodo, setFilter } from "./actions.js";
const FILTERS = { all: "All", active: "Active", done: "Done" };
// A plain function of props, as in the Redux docs of the time: the typed text is read from
// the input through a ref, so the component needs no state of its own.
function TodoApp({ todos, left, filter, onAdd, onToggle, onFilter }) {
let input;
function handleSubmit(event) {
event.preventDefault();
const title = input.value.trim();
if (!title) return;
onAdd(title);
input.value = "";
}
return (
<main className="todo-app">
<h1>Todos</h1>
<form id="todo-form" onSubmit={handleSubmit}>
<input
id="new-todo"
ref={(node) => {
input = node;
}}
placeholder="What needs doing?"
autoComplete="off"
aria-label="New todo"
/>
<button id="add" type="submit">
Add
</button>
</form>
<ul id="todo-list">
{todos.map((todo) => (
<li key={todo.id} className={todo.done ? "done" : undefined}>
<label>
<input
type="checkbox"
data-toggle
checked={todo.done}
onChange={() => onToggle(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.keys(FILTERS).map((name) => (
<button
key={name}
type="button"
data-filter={name}
className={filter === name ? "selected" : undefined}
onClick={() => onFilter(name)}
>
{FILTERS[name]}
</button>
))}
</div>
</div>
</main>
);
}
// Picks what the component reads from the state. Derived values are computed, not stored.
const mapStateToProps = (state) => ({
todos: state.todos.filter((t) => state.filter === "all" || (state.filter === "done") === t.done),
left: state.todos.filter((t) => !t.done).length,
filter: state.filter,
});
// The component never changes data itself: its callbacks dispatch actions to the store.
const mapDispatchToProps = (dispatch) => ({
onAdd: (title) => dispatch(addTodo(title)),
onToggle: (id) => dispatch(toggleTodo(id)),
onFilter: (name) => dispatch(setFilter(name)),
});
// connect() subscribes to the store, runs mapStateToProps when the state changes and renders
// TodoApp again when the result differs. @deprecated since React-Redux 9.3 (hooks are
// recommended), it still works the same and prints nothing (legacy_connect is the same).
export default connect(mapStateToProps, mapDispatchToProps)(TodoApp);SSR, SSG and meta-frameworks
React 19.3.0, React DOM 19.3.0
/pages/index.jsx · 101 lines
// A page as Next.js apps wrote it in 2016–2018: a class component with a static data method.
// The same file runs twice: on the server (to HTML) and in the browser (hydration).
import { Component } from "react";
const FILTERS = { all: "All", active: "Active", done: "Done" };
export default class TodoPage extends Component {
// Called on the server before the first render; what it returns becomes the page's props.
// A real page would read a database or an API here.
static async getInitialProps() {
return {
todos: [
{ id: 1, title: "Read the docs", done: true },
{ id: 2, title: "Write the app", done: false },
],
};
}
constructor(props) {
super(props);
// The first state comes only from props, so the server and the browser render the same HTML.
this.state = { todos: props.todos, filter: "all", title: "" };
this.nextId = Math.max(0, ...props.todos.map((t) => t.id)) + 1;
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ title: event.target.value });
}
handleSubmit(event) {
event.preventDefault();
const title = this.state.title.trim();
if (!title) return;
const todo = { id: this.nextId++, title, done: false };
this.setState((state) => ({ todos: [...state.todos, todo], title: "" }));
}
toggle(id) {
this.setState((state) => ({
todos: state.todos.map((t) => (t.id === id ? { ...t, done: !t.done } : t)),
}));
}
render() {
const { todos, filter, title } = this.state;
const shown = todos.filter((t) => filter === "all" || (filter === "done") === t.done);
const left = todos.filter((t) => !t.done).length;
return (
<main className="todo-app">
<h1>Todos</h1>
<form id="todo-form" onSubmit={this.handleSubmit}>
<input
id="new-todo"
value={title}
onChange={this.handleChange}
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={() => this.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={() => this.setState({ filter: name })}
>
{label}
</button>
))}
</div>
</div>
</main>
);
}
}