Next.js and SSR
The hooks and StoreProvider are client code, and their modules are marked 'use client'. Everything server-side stays in server components: fetching data, reading the session, and Server Actions. This page sets up the App Router; the RWA example is a complete Next 16 app built this way.
Put the provider in a client component
Section titled “Put the provider in a client component”StoreProvider takes a store, handlers and machine definitions, which are objects and functions, and those can’t be passed from a server component. Create them in a client component of your own:
'use client';
import { useState, type ReactNode } from 'react';import { StoreProvider, createSimpleStore } from '@input-actions/core';import { handlers } from '@/lib/handlers';import { checkoutMachine } from '@/lib/machines';import { appBindings } from '@/lib/bindings';import { buildInitialState } from '@/lib/state';import type { AppState, InitialData } from '@/lib/types';
export function AppProviders({ initialData, children,}: { initialData: InitialData; children: ReactNode;}) { // One store per mount, created on the first render. Never at module scope. const [store] = useState(() => createSimpleStore<AppState>(buildInitialState(initialData)), );
return ( <StoreProvider store={store} asyncHandlers={handlers} stateMachines={[checkoutMachine]} bindings={appBindings} > {children} </StoreProvider> );}Then render it from a server layout, passing only serializable data:
// app/layout.tsx (a server component)import { AppProviders } from '@/components/AppProviders';import { readSession } from '@/lib/server/session';
export default async function RootLayout({ children }: { children: React.ReactNode }) { const user = await readSession(); return ( <html lang="en"> <body> <AppProviders initialData={{ user }}>{children}</AppProviders> </body> </html> );}Which adapter
Section titled “Which adapter”Use a factory that returns the store synchronously, so it can be created during render: createSimpleStore, or an adapter’s createXStoreSync (createReduxStoreSync, createZustandStoreSync, and so on). The async createXStore forms resolve immediately too, but they return a promise, which a render can’t wait for.
Seed per-page data with HydrateStore
Section titled “Seed per-page data with HydrateStore”A layout creates the store once, but each page fetches its own data. HydrateStore merges a page’s server data into the existing store. Use it in the page’s server component, around the client tree that reads the data:
// app/banks/page.tsx (a server component)import { HydrateStore, type Action } from '@input-actions/core';import { getBanks } from '@/lib/server/backend';import BanksClient from './BanksClient';
export default async function BanksPage() { const banks = await getBanks(); return ( <HydrateStore actions={[ ['action/batch', banks.map((b): Action => ['action/save', ['banks', b.id], b])], ]} > <BanksClient /> </HydrateStore> );}- On the server,
HydrateStoredispatches its actions during render, so the HTML already shows the data. - On the client, it dispatches the same actions on its first render, so the store matches what the server rendered and there’s no flash of empty state.
- It dispatches once per mount, Strict Mode included. Use
action/saveandaction/merge, which give the same result if they run twice.
The actions are sent to the client as props, so they have to be serializable: plain data, no functions or class instances.
Server Actions as async handlers
Section titled “Server Actions as async handlers”An async handler is any function that returns a promise, so a Server Action can be one directly, or be called from one:
import type { AsyncHandlers } from '@input-actions/core';import { addBankAction } from '@/app/actions'; // 'use server'
export const handlers: AsyncHandlers = { addBank: (name: string) => addBankAction(name),};The handler’s last argument is { signal }, an AbortSignal. Don’t forward it to the Server Action: it can’t be serialized. Cancelling still works on the client: the cancel strategy ignores the cancelled call’s result, though the server request itself runs to completion.
Check permissions inside the Server Action, as you would for any endpoint. State in the client store is for rendering, not for authorization.
State machines on first render
Section titled “State machines on first render”A machine needs no slice in the initial state: its mode is read as the definition’s initialState until the first transition writes it. The server and the client therefore render the same initial mode with nothing extra to seed (ADR 0025).
Pages Router
Section titled “Pages Router”The same rules apply. Create the store with useState in _app.tsx, or in a provider component it renders, never at module scope. Build its initial state from the page props that getServerSideProps or getStaticProps return.