React 16
Fiber-era APIs: Error Boundaries, Fragments, Portals, the new Context API, memo, lazy, Suspense beginnings, and Strict Mode. · 9 lessons · click a card to expand
Part of the React 16 → 19.3 evolution reference. Expand a card, then use chapter links in the sidebar.
Why React 16 mattered: it replaced the stack reconciler with Fiber, which made Error Boundaries, Fragments, Portals, and later Hooks/Suspense possible. Most 16.x minors are small; the table at the end lists them so you do not invent APIs for 16.10–16.12.
Why this release mattered
1 item01React 16 is a new engine with a familiar APIReact 16
setState — it was an engine that could split work, catch render errors, and return multiple children without a dummy DOM node.Before React 16
- Render errors unmounted the whole tree
- Every component had to return one node (usually a wrapping
div) - Modals were
position: fixedhacks inside overflow-hidden parents - Context used undocumented
contextTypesthat were easy to break
After React 16
Error Boundaries, Fragments, Portals, and (in 16.3) a supported Context API. Fiber is underneath all of them.
Real-world examples
1. SaaS shell — a crash in a widget must not blank the nav. Error Boundaries (16.0).
2. Design system Table — Table / tr / td cannot have extra divs. Fragments (16.0 arrays, 16.2 <>).
3. Checkout modal — render the dialog into document.body so the page scroll lock and z-index work. Portals (16.0).
lazy/Suspense.Error Boundaries
1 item02Error Boundaries catch render-time failuresReact 16
componentDidCatch and/or static getDerivedStateFromError (the latter added in 16.6) to render a fallback when a child throws during render.try/catch does not wrap render of children. React 16 runs render in a try path and looks up the Fiber tree for a boundary.What is it?
class WidgetBoundary extends React.Component {
state = { error: null };
static getDerivedStateFromError(error) {
return { error };
}
componentDidCatch(error, info) {
reportToSentry(error, info.componentStack);
}
render() {
if (this.state.error) {
return <p role="alert">This widget failed. Try refresh.</p>;
}
return this.props.children;
}
}getDerivedStateFromError (16.6) is for updating state to show a fallback. componentDidCatch (16.0) is for side effects (logging). You usually want both.
Why was it introduced?
In React 15, a throw in render often left the UI in a corrupted state. Production users saw a blank page. Fiber can unmount the broken subtree and keep the rest.
Before React 16
function Dashboard() {
return (
<div>
<RevenueChart /> {/* throw → entire app unmounts */}
<ActivityFeed />
</div>
);
}With Error Boundaries
function Dashboard() {
return (
<div>
<WidgetBoundary>
<RevenueChart />
</WidgetBoundary>
<ActivityFeed />
</div>
);
}How it works
Throw during render of a descendant → React unwinds fibers → finds the nearest boundary → re-renders it with fallback. Throws in event handlers are *not* caught (use try/catch there). Throws in useEffect are not caught by Error Boundaries in the same way — they are effect-time errors (in 18+, some are reported; still wrap async work yourself).
Real-world examples
1. Analytics dashboard — one third-party chart throws on bad data. Isolate each card.
2. E-commerce PDP — recommended-products widget fails; the buy box must stay.
3. Chat app — message parser throws on a malformed payload; the composer stays mounted.
When should I use it?
Around untrusted, optional, or third-party subtrees. Route-level boundaries in a SPA so a page crash keeps the chrome.
When should I NOT use it?
- Do not wrap every tiny component (you will hide bugs)
- Do not use them to handle expected validation (
email is invalidis not an exception) - Function components cannot be boundaries (no
getDerivedStateFromErroron functions).react-error-boundaryis a class wrapper.
Common mistakes
- Catching fetch failures with a boundary instead of rendering
{error && <p>…</p>} - Expecting
onClickthrows to be caught - Forgetting a reset (
key={location}on the boundary) so the fallback never goes away after navigation
Performance considerations
Boundaries are cheap. Logging in componentDidCatch should be async (beacon), not a sync 200ms HTTP call.
Related APIs
Suspense (sibling idea: “not ready yet” vs “threw”), React 18 hydrateRoot error options, React 19 improved error reporting
Interview notes
- Class-only.
- Catches render/lifecycle/constructor errors in descendants, not events.
getDerivedStateFromError= 16.6.
key on the boundary that changes when the user navigates or hits Retry.onClick and confirm the boundary does *not* catch it.Fragments
1 item03Fragments return multiple children without a DOM nodeReact 16.2
React.Fragment / arrays; the <> short syntax landed in 16.2. A Fragment groups elements without adding a wrapper node.<tr> cannot sit in a random <div>). CSS has rules (flex/grid children). Dummy wrappers break both.What is it?
function Columns() {
return (
<>
<td>SKU</td>
<td>Qty</td>
</>
);
}Keyed fragments need the long form:
<React.Fragment key={group.id}>
{group.cells}
</React.Fragment><> cannot take a key.
Why was it introduced?
React 15 forced a single root. Teams wrapped everything in <div className="fragment-hack"> and broke tables and CSS.
Before
function UserMeta({ user }) {
return (
<div>
<dt>Role</dt>
<dd>{user.role}</dd>
</div>
);
}That extra div inside a <dl> is invalid-ish and can wreck grid layouts.
After
function UserMeta({ user }) {
return (
<>
<dt>Role</dt>
<dd>{user.role}</dd>
</>
);
}Real-world examples
1. Admin table — Row component returns several <td>s.
2. Design-system ButtonGroup — fragment of buttons as flex children of the parent.
3. Auth layout — <> <SeoTitle /> <Main /> </> without a wrapper that breaks min-height: 100%.
When should I use it?
Whenever the parent should own the DOM structure.
When should I NOT?
If you need a hook target for CSS (display: flex on *this* group) or a ref on a box, use a real element. (Fragment refs are a 19.3 feature — React 19.3.)
Common mistakes
- Putting
keyon<> - Using a fragment when a
<div>is the correct layout box
Performance considerations
Fewer nodes → slightly less DOM. Not a reason to fragment everything.
Related APIs
Arrays as children (16.0), Fragment refs (19.3)
Interview notes
- 16.0 Fragment component; 16.2 short syntax.
- Only
React.Fragmentacceptskey.
Fragment, that is expected — it is not a DOM node.<td>s from a component inside a <tr> with and without a wrapping div. Inspect the table.Portals
1 item04Portals render children into a different DOM nodeReact 16
createPortal(child, container) mounts child under container while keeping React parent context (events, context).import { createPortal } from "react-dom";
function Modal({ children, onClose }) {
return createPortal(
<div className="modal-backdrop" onClick={onClose}>
<div className="modal" onClick={(e) => e.stopPropagation()}>
{children}
</div>
</div>,
document.getElementById("modal-root"),
);
}Why was it introduced?
overflow: hidden, transform, and stacking contexts trap position: absolute UI. Tooltips, dialogs, and toasts need to escape.
Before
// Inside a sidebar with overflow: auto — modal is clipped
function Sidebar() {
return (
<aside className="overflow-auto">
{open && <div className="modal-absolute">…</div>}
</aside>
);
}After
Portal to body / #modal-root. React still treats the modal as a child of Sidebar for context and event bubbling in the React tree.
Event bubbling: a click inside the portal bubbles to React parents, not necessarily through the DOM ancestors of #modal-root. That is intentional.
Real-world examples
1. Checkout: confirm address dialog — must cover the full viewport.
2. Data table: row action menu — must not clip at the table’s overflow.
3. Chat: emoji picker — anchored visually to the composer, DOM under body for z-index.
When should I use it?
Dialogs, toasts, popovers, full-screen lightboxes, hover cards that would clip.
When should I NOT?
Normal layout. Portals make focus management and SSR harder (document is missing on the server — gate with useEffect or a framework modal).
Common mistakes
- Forgetting an accessible title / focus trap /
Escto close - Creating a new
containerevery render - Assuming DOM bubbling equals React bubbling
Performance considerations
Portals are cheap. Re-creating portal roots is not.
Related APIs
createRoot on a node, React 19 document metadata (different problem: <title> in the tree)
Interview notes
- Portal = DOM escape hatch, React tree preserved.
- Context still flows from the React parent.
#modal-root in index.html next to #root.onClick on the React parent; confirm the parent still receives the React click.Refs, forwarding, Context, Strict Mode
1 item05createRef, forwardRef, and the supported Context APIReact 16.3
createRef replaced string refs. forwardRef lets a child attach a parent’s ref to a DOM node. createContext replaced legacy context.createRef (classes) / useRef later
class SearchBox extends React.Component {
inputRef = React.createRef();
componentDidMount() {
this.inputRef.current.focus();
}
render() {
return <input ref={this.inputRef} />;
}
}String refs (ref="input") are gone. Function components use useRef (16.8). React 19 also allows ref as a normal prop — React 19.
forwardRef
const FancyInput = React.forwardRef(function FancyInput({ error }, ref) {
return <input ref={ref} aria-invalid={!!error} />;
});Needed because ref was not a regular prop. Libraries (design systems) must forward refs so parents can focus the inner <input>.
New Context API
const ThemeContext = React.createContext("light");
function App() {
return (
<ThemeContext.Provider value="dark">
<Page />
</ThemeContext.Provider>
);
}
function Button() {
const theme = useContext(ThemeContext); // 16.8; or ThemeContext.Consumer / class contextType (16.6)
return <button className={theme}>Save</button>;
}Legacy childContextTypes re-rendered poorly and was error-prone. The 16.3 API bails out if value is referentially equal.
Strict Mode (16.3)
<React.StrictMode> is a dev-only wrapper. In 16.3 it warned about unsafe lifecycles, legacy context, and findDOMNode. In React 18 it also double-invokes render/effects in development. See Concurrent.
Real-world examples
1. Design system `TextField` — forwardRef so a form can ref.current.focus() on validation error.
2. Auth session — UserContext from a login shell instead of prop-drilling user through 12 layers.
3. Theme / i18n — one provider at the app root; buttons consume without props.
When should I use Context?
For rarely changing, tree-wide values (theme, locale, current user id). Not for high-frequency mouse coordinates.
When should I NOT use forwardRef?
Leaf DOM components you never focus. React 19 reduces the need for forwardRef on new components (ref as prop).
Common mistakes
- New object as context
valueevery render (value={{ user }}}without memo) → all consumers re-render - Using Context instead of a state library for the entire store
- Forgetting
displayNameonforwardRefcomponents (DevTools)
Related APIs
useContext (16.8), contextType (16.6), React 19 <Context> without .Provider
Interview notes
- 16.3 = createRef, forwardRef, createContext, StrictMode, getDerivedStateFromProps, getSnapshotBeforeUpdate.
- Legacy context vs new context is a classic question.
getDerivedStateFromProps is overused. Prefer fully controlled components.value={{ theme }} is inline vs useMemo.memo, lazy, Suspense
1 item06React.memo, lazy, and Suspense for code splittingReact 16.6
React.memo is a pure-component for functions. React.lazy + Suspense load a component bundle on demand. In 16.x, Suspense did not wait for data — only for lazy(). Data Suspense is a later (18+) story.React.memo
const PriceCell = React.memo(function PriceCell({ amount, currency }) {
return <td>{formatMoney(amount, currency)}</td>;
});Shallow-compares props. Skip render if they are equal. Same idea as PureComponent.
Fundamentals: Lesson 27.
lazy + Suspense (code splitting)
const Editor = React.lazy(() => import("./MarkdownEditor"));
function ArticlePage() {
return (
<Suspense fallback={<p>Loading editor…</p>}>
<Editor />
</Suspense>
);
}lazy requires a default export. The fallback is committed until the promise resolves. Error if the import fails — pair with an Error Boundary.
Why it was needed
Class PureComponent had no function equivalent. Huge admin bundles shipped the JSON editor to every page.
Before
import Editor from "./MarkdownEditor"; // always in the main bundle
function ArticlePage() {
return <Editor />;
}After
Dynamic import() with a declared loading UI. Route-based splitting (React Router) uses the same primitives.
Real-world examples
1. CMS: Markdown editor — load CodeMirror only on /articles/:id/edit.
2. Settings: billing — load Stripe.js UI when the user opens the billing tab.
3. Social: emoji picker — large dataset; lazy the picker on first open.
When should I use memo?
When a component is pure, re-renders often with the same props, and profiling shows it is hot. Not on every component.
When should I NOT use lazy?
Tiny components (the request costs more than the module). Anything needed for LCP on first paint (hero). SSR needs framework support (lazy + SSR was painful until 18 streaming).
Common mistakes
memoof a component that receives new inlineonClick={() => ...}every time- Nested
Suspensemissing so one slow lazy blanks a whole page - Using 16.x Suspense to wrap
fetch(not supported then)
Performance considerations
memo adds a compare. lazy adds a network round trip. Measure.
Related APIs
Concurrent Suspense for data (18), useTransition to avoid hiding the whole page
Interview notes
memo≈PureComponent.- 16.6 Suspense = code splitting only.
lazy+ named exports need a re-export wrapper.
export default function Editor. Named: lazy(() => import('./E').then(m => ({ default: m.Editor }))).lazy. Confirm the fallback.Notable 16.x minors
3 items07What actually changed in 16.4–16.14React 16.4
| Version | What React developers actually felt |
|---|---|
| 16.0 | Fiber, Error Boundaries, Portals, fragments as arrays, custom DOM attributes, error recovery |
| 16.1 | Bugfixes, React.createContext experimental prep |
| 16.2 | <> Fragment syntax |
| 16.3 | Context, createRef, forwardRef, StrictMode, getDerivedStateFromProps, getSnapshotBeforeUpdate |
| 16.4 | Pointer Events (onPointerDown …), getDerivedStateFromProps bugfix, experimental Profiler |
| 16.5 | <Profiler> for measuring render cost; DevTools integration |
| 16.6 | memo, lazy, Suspense, contextType, getDerivedStateFromError, StrictMode additions |
| 16.7 | Bugfixes (Hooks were *not* in 16.7 — they shipped in 16.8) |
| 16.8 | Hooks |
| 16.9 | UNSAFE_ lifecycle names, act() testing helper, <Profiler> id |
| 16.10–16.12 | Mostly internals / experimental concurrent work — not new public APIs you should study as features |
| 16.13 | Warnings: some deprecated patterns, forwardRef + memo stacking |
| 16.14 | Backport of the new JSX transform so React 16 apps can skip import React from 'react' |
Profiler (16.5)
<Profiler id="Checkout" onRender={(id, phase, actualDuration) => {
if (actualDuration > 16) logSlow(id, actualDuration);
}}>
<CheckoutFlow />
</Profiler>Use in performance investigations, not in every production view without sampling.
Pointer Events (16.4)
onPointerUp unifies mouse/touch/pen. Prefer them for drawing tools and drag handles.
167 trap
Tip: If an interview asks “when did Hooks ship?”, say 16.8. If they ask Fiber, 16.0.
Try it: Check package.json of an old app: react 16.7 means no Hooks yet.
package.json of an old app: react 16.7 means no Hooks yet.08Strict Mode’s original jobReact 16.3
- Unsafe lifecycles (
componentWillMount, …) - Legacy string refs
- Legacy context
- Unexpected side effects (later expanded)
It is a tree wrapper, not a global compiler flag:
createRoot(node).render(
<React.StrictMode>
<App />
</React.StrictMode>,
);You can wrap only a subtree (a new widget) to harden it first.
Real-world examples
1. Gradual migration of a design system — StrictMode around new packages only.
2. Finding double-fetch bugs — 18’s double-invoke of effects (later behavior) exposes missing cleanup.
3. Third-party maps — a vendor widget that breaks under Strict Mode is not Strict Mode’s bug; it is missing cleanup.
When should I use it?
Always in development for new apps. On legacy apps, wrap incrementally.
When should I NOT?
Do not disable Strict Mode to “fix” double effects — fix the effect.