Get Started
Install @k8ordo/effects, then build a header that says when the visitor is offline and a keyboard shortcut: one hook that reads the browser, and one that listens to window.
On this page
Install
npm install @k8ordo/effectsIt runs with:
- React 19.3 or later
- TypeScript 7 or later, when you write TypeScript
The guide for AI agents ships at node_modules/@k8ordo/effects/docs/GUIDE.md. Point your AGENTS.md or the like at that path.
Every hook is exported from a 'use client' module. Call them from Client Components.
Reading the browser
src/components/header.tsx'use client';
import { useOnline, useScrollDirection } from '@k8ordo/effects';
export function Header() {
const online = useOnline();
`true` on the server and while hydrating
const hidden = useScrollDirection({ threshold: 64 }) === 'down';
Scrolling down within 64px of the top does not count
return (
<header data-hidden={hidden}>
{online ? null : <p>You are offline.</p>}
</header>
);
}useOnline() reads navigator.onLine and updates on the online and offline events. useScrollDirection() returns which way the page last scrolled, 'up' or 'down'.
The server knows neither the visitor’s connection nor the scroll position, so the server render and the hydration render get true and null. The browser’s values arrive in the render right after, and hydration never mismatches.
The other value hooks, and what each returns on the server, are in Browser state.
Events on window
src/components/search-shortcut.tsx'use client';
import { useWindowEvent } from '@k8ordo/effects';
export function SearchShortcut({ onOpen }: { onOpen: () => void }) {
useWindowEvent('keydown', (event) => {
if (event.key === '/') onOpen();
Calls the current `onOpen`
});
return null;
}useWindowEvent adds a listener to window while the component is mounted. The event is typed from the first argument: 'keydown' gives a KeyboardEvent.
There is no dependency array. The listener always calls the latest render’s handler, so a new onOpen prop never adds the listener again.
Note
Before reaching for a hook, check whether React or the browser already does it. The list is in Instead of Effects.