Instead of Effects
Before writing a useEffect, check what React, the browser or another package already does. What this package has no hook for is written one of these ways.
On this page
What React does
<title> and <meta>
src/components/talk-page.tsxexport function TalkPage({ talk }: { talk: Talk }) {
return (
<article>
<title>{talk.title}</title>
<meta content={talk.summary} name="description" />
<h1>{talk.title}</h1>
</article>
);
}Write the document’s title or a <meta> inside the component. React 19 hoists them into <head>.
Computed values
todo-list.tsxconst visible = todos.filter((todo) => showDone || !todo.done);A value computed from props or state is computed during render, not copied into state by an effect.
Responding to the visitor
Something to do because the visitor did something goes in the event handler, not in an effect that reacts to the state it set.
Data from the server
src/components/talk.tsximport { Suspense } from 'react';
import { getComments } from '../lib/comments';
import { Comments } from './comments';
export function Talk({ slug }: { slug: string }) {
return (
<Suspense fallback={<p>Loading comments…</p>}>
<Comments comments={getComments(slug)} />
</Suspense>
);
}src/components/comments.tsx'use client';
import { use } from 'react';
type Props = { comments: Promise<Comment[]> };
export function Comments({ comments }: Props) {
return <CommentList items={use(comments)} />;
}Data the server can read starts loading in a Server Component, which passes the promise down as a prop. The Client Component reads it with use().
Changes on the server
subscribe-form.tsxconst [error, action, isPending] = useActionState(subscribe, null);
<form action={action}>
<input name="email" type="email" />
<button disabled={isPending} type="submit">
Subscribe
</button>
{error}
</form>;A change on the server is an Action: a Server Function called through useActionState or useTransition, which hold the pending state and the result.
Render errors
A render error is caught by an error boundary. To report it, use the root’s onCaughtError and onUncaughtError.
An effect of your own
If you still write an effect, read the latest props with useEffectEvent. First, though, check whether one of this package’s hooks is that effect.
What the platform does
Element events and observers
An element’s events go in its on* props, and observers in a ref callback. Both are shown in Events.
Scrolling to a rendered element
To scroll to an element once it renders, call scrollIntoView() in a ref callback on it.
Viewport and container size
layout.module.css.sidebar {
display: none;
@media (width >= 48rem) {
display: block;
}
}
.card {
container-type: inline-size;
}
@container (inline-size >= 30rem) {
.body {
display: grid;
grid-template-columns: 1fr 2fr;
}
}Layout by viewport or container size is CSS media queries and container queries. There is no useMediaQuery or useWindowSize.
Opening and closing
menu.tsx<button popoverTarget="menu" type="button">
Menu
</button>
<div id="menu" popover="auto">
<MenuItems />
</div>Opening and closing is popover and <dialog>, and a button opens a <dialog> with the command and commandfor attributes. There is no useDisclosure.
A browser-only subtree
A subtree only the browser can render passes browser() from react-dom to use(), inside a <Suspense>. No mounted flag, and no hook that says whether hydration has happened. It is shown in Browser state.
Registering a service worker
Register a service worker with one call from the client entry, outside any component.
What other packages own
What another k8ordo package owns has no hook here either.
- What is kept in localStorage, sessionStorage, cookies or the URL is
@k8ordo/state’s. - The colour scheme and
prefers-color-schemeare@k8ordo/color-scheme’s. - The pathname and navigation are
@k8ordo/router’s. - Copying to the clipboard is
@k8ordo/ui’sCopyButton.
Hooks that are not here
There is no useMount, useEffectOnce or useUpdateEffect. Every hook here names the system it talks to, and a hook that names none is a useEffect under another name.
Nor are there React state helpers such as useToggle, useDebounce or usePrevious. They wrap useState, which is the one way to write them.