Server components
Use the packages in React Server Components, for example the Next.js App Router.
From 0.5.1, the packages work in React Server Components. Components with state, effects or events are client components: their files start with "use client". Functions and constants are in files without it, so server code can call them. Every exported function and constant works on the server, except hooks, toast(), dismissToast() and fieldContext.
What runs where
| Part | In a server component | In a client component |
|---|---|---|
Components (Button, Money, Stack, …) | Yes, with data props only | Yes |
Function props (onClick, onValueChange, link, text) | No | Yes |
Functions and constants (formatMoney, buttonClass, isVatNumber, THEME_SCRIPT, …) | Yes | Yes |
Hooks (useLang, useStrings, useTheme, …) | No | Yes |
toast(), dismissToast() | No | Yes |
PercentProvider | Only in a client file of your app | Yes |
Set up the provider
PercentProvider takes functions (link, text), and a server component cannot pass functions. Put the provider in a client file of your app, and use that file in the root layout. THEME_SCRIPT is a string, so the layout can put it in <head>.
"use client";
import NextLink from "next/link";
import { PercentProvider, Toaster, type Lang } from "@percentsa/ui";
export function Providers({ lang, children }: { lang: Lang; children: React.ReactNode }) {
return (
<PercentProvider lang={lang} link={({ href, ...p }) => <NextLink href={href!} {...p} />}>
{children}
<Toaster />
</PercentProvider>
);
}import { THEME_SCRIPT } from "@percentsa/ui";
import { Providers } from "./providers";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
// THEME_SCRIPT can change data-theme before React loads.
<html lang="ar" dir="rtl" data-theme="dark" suppressHydrationWarning>
<head>
<script dangerouslySetInnerHTML={{ __html: THEME_SCRIPT }} />
</head>
<body>
<Providers lang="ar">{children}</Providers>
</body>
</html>
);
}A server page
The page reads its data on the server and renders the components. Client components in it, such as Money and Button, also render on the server first; then the browser loads their code.
import NextLink from "next/link";
import { Badge, buttonClass, Heading, isVatNumber, Money, Stack } from "@percentsa/ui";
import { getInvoice } from "@/lib/invoices";
import { PayButton } from "./pay-button";
export default async function InvoicePage({ params }: { params: Promise<{ id: string }> }) {
const invoice = await getInvoice((await params).id);
return (
<Stack gap={4}>
<Heading level={1}>{invoice.number}</Heading>
<Money amount={invoice.total} />
{!isVatNumber(invoice.vat) && <Badge>VAT number missing</Badge>}
<NextLink href="/invoices" className={buttonClass("secondary")}>
All invoices
</NextLink>
<PayButton invoiceId={invoice.id} />
</Stack>
);
}Events
A server component cannot pass onClick. Put the event in a small client component, and give it only data (here, the invoice id).
"use client";
import { Button, toast } from "@percentsa/ui";
export function PayButton({ invoiceId }: { invoiceId: string }) {
return (
<Button
onClick={async () => {
await fetch(`/api/invoices/${invoiceId}/pay`, { method: "POST" });
toast({ title: "Paid", tone: "success" });
}}
>
Pay
</Button>
);
}With Cache Components on (cacheComponents in next.config), a part that reads the current time or the URL must be inside <Suspense>: TimeText, and a provider file that reads useSearchParams() for location. These parts then show after the page loads.
Apps without server components (React Router, Vite) need no change: the "use client" lines have no effect there.