Pendora takes a person's official pension and insurance snapshot and turns the raw industry data feed into a plain dashboard of what they own and what they are covered for. It also has a phone-friendly wizard for signing the consent form that lets the data be pulled. This note covers a failure in the detail view. A change made as an optimisation stopped the page from rendering.
Why does a hook placed after an early return crash a React component?
React identifies hooks by the order in which they are called during a render, not by name. Every render of a component must call the same hooks in the same sequence. An early return exits the component before the rest of its body runs, so everything below it becomes conditional. A typical example is a guard that returns a placeholder when some input is absent. That example is generic and does not describe the guards in this case.
If a hook sits below such a guard, renders that take the early return call fewer hooks than renders that pass it. When the same mounted component switches from one path to the other, the number of hooks changes between renders. React treats that as an error rather than something to reconcile. This is why the Rules of Hooks require hooks to be called only at the top level of a component, before any conditional return.
What happened in Pendora's detail view?
The round-4 work added memoisation to the detail view component. The memoisation sat below the component's existing early-return guards. The view crashed with React's changing-hook-count error. The record does not say which conditions the guards check or which sequence of renders triggered the crash. It establishes only the placement and the resulting error.
Why was the memoisation removed rather than moved above the guards?
The usual repair for this class of bug is to move the hooks above every early return. That keeps the memoisation, but the hooks then run on every render, including renders where the component is about to return early. Their inputs therefore have to tolerate those cases.
Pendora's fix took a different route. It dropped the memoisation entirely and replaced it with plain inline computation. The stated grounds were that the arrays involved are small enough that memoising them was never worth a hook. Inline computation adds no hook, so the ordering constraint no longer applies to that code. No guard placement can reintroduce the error through it.
When is memoisation actually worth a hook?
In general, a memo hook keeps a cached value and compares its dependencies on every render. It is also subject to the same call-order rules as any other hook, and this failure shows that cost. Common reasons a memo hook earns its place:
- The computation is expensive. Examples are large collections, heavy transforms, or work repeated many times per render.
- Referential stability matters downstream. A memoised child component or an effect may depend on a value keeping the same identity between renders.
- Profiling shows a real cost. A measured hotspot is a stronger basis for memoising than a guess.
When none of these hold, inline computation is simpler to read and has no hook-ordering constraints.
What should teams check before adding memoisation to an existing component?
- Locate every early return first. Before adding a hook to an existing component, find each guard and conditional return. A new hook belongs above all of them.
- Ask what the hook buys. Consider how large the data is and how costly the work is. If the inputs are small, inline computation may be all that is needed.
- Exercise path changes, not only individual states. A hook-count error fires when one mounted instance moves between a guarded path and the full path. A test that only ever takes one path can miss it.
- Let static checks enforce hook rules. Hook-ordering mistakes are mechanical. Lint rules for hooks can catch them, and they are most useful when configured as errors rather than warnings.
A memo hook changes the structure of a component as well as its performance profile. When adding one, check where it sits relative to every return path. Also check whether the data justifies a hook at all.