Skip to main content

Using with Next.js

React Honey Hooks works with both Next.js routers.

App Router. Hooks can only run in Client Components, so call them from a file that starts with the "use client" directive. Server Components can import that component as usual:

"use client";

import { useWindowScroll } from "react-honey-hooks";

export function BackToTop() {
const { y } = useWindowScroll();
return y > 400 && <button onClick={() => window.scrollTo({ top: 0 })}>Back to top</button>;
}

Pages Router. Import and call the hooks in any page or component.

Next.js renders Client Components on the server first, which is why every hook is SSR-safe: each hook's page says what the hook returns during server rendering. To show content that only makes sense in the browser, such as a value read from localStorage or the user's time zone, without a hydration mismatch, use useIsClient.

useQuery and useQueryState read window.location directly and don't see navigations made with next/link or useRouter. In a Next.js app, prefer useSearchParams from next/navigation for query params the Next.js router changes.