Skip to content

@parlajs/react

Framework adapter

pnpm add @parlajs/react

Requires React 19. Re-exports @parlajs/core, with rich text typed as ReactNode.

import { createParla, ParlaProvider, useLocale, useParla, useParlaLoaded } from '@parlajs/react';
const parla = createParla({ catalog });
createRoot(document.querySelector('#root')!).render(
<ParlaProvider parla={parla}>
<Suspense fallback={null}>
<App />
</Suspense>
</ParlaProvider>,
);
Export
createParla(options) Core’s, with rich text rendered as React nodes
<ParlaProvider parla> Puts the instance in context
useParla(list) { t }. Suspends until loaded; loaded namespaces never suspend
useParlaLoaded(list) { t }, undefined until loaded. Never suspends
useLocale() { locale, locales, switchLocale }; re-renders on switch
Translations<typeof ns> The type of t, with ReactNode rich text
  • A failed download reaches the nearest error boundary; a failed switch rejects switchLocale.
  • The hooks throw outside <ParlaProvider>.
  • In Server Components (the react-server condition) there are no hooks: await parla.load([…]) in an async component.

useParla suspends until its namespaces load. useParlaLoaded never suspends: t is undefined until then.

Use useParla by default, and useParlaLoaded for a placeholder of your own, or where a fallback would hide what is on screen.

function Cart() {
const { t } = useParla([checkout]); // suspends until loaded
return <h1>{t.checkout.title()}</h1>;
}
function CartBadge() {
const { t } = useParlaLoaded([checkout]); // never suspends
if (!t) return <span>…</span>;
return <span>{t.checkout.title()}</span>;
}

Any component can call the hooks or take t as a prop, and a namespace loads once per locale: see Components and routes.

switchLocale loads first, then switches and re-renders: nothing suspends.

function Switcher() {
const { locale, locales, switchLocale } = useLocale();
const [pending, startTransition] = useTransition();
return locales.map((l) => (
<button
key={l}
disabled={pending || l === locale}
onClick={() => startTransition(() => switchLocale(l).catch(reportError))}>
{l}
</button>
));
}

Each tag’s callback returns a ReactNode:

t.home.terms({ link: (c) => <a href="/terms">{c}</a> });
createParla({ catalog, markup: { strong: (c) => <strong>{c}</strong> } });

Loading…

The first line uses the instance’s strong. See Rich text.