Skip to content

Global state — StoreProvider

When several components need to read or change the same state, wrap them in a StoreProvider and reach for useStore (reads) and useActions (writes). The action vocabulary is identical to the local hook.

Two sibling components below share one store. Clicking in Controls updates Display — no prop drilling, no callback wiring.

Display

0

Controls

import {
createSimpleStore,
StoreProvider,
useStore,
useActions,
} from '@input-actions/core';
const store = createSimpleStore({ count: 0 });
export default function App() {
return (
<StoreProvider store={store}>
<Display />
<Controls />
</StoreProvider>
);
}
function Display() {
const count = useStore((s) => s.count);
return <p>Count: {count}</p>;
}
function Controls() {
const actions = useActions();
return (
<>
<button onClick={actions(['action/decrement', ['count']])}>−</button>
<button onClick={actions(['action/increment', ['count']])}>+</button>
<button onClick={actions(['action/increment', ['count'], 10])}>+10</button>
<button onClick={actions(['action/reset'])}>Reset</button>
</>
);
}
  • createSimpleStore(initial) builds the built-in adapter. It holds the state object you passed in.
  • <StoreProvider store={store}> makes that store available to anything underneath via React context. One provider per tree.
  • useStore((s) => s.count) reads a slice. The selector lets the hook subscribe to only that slice — re-renders happen when the slice’s value changes, not on every dispatch.
  • useActions() returns the same handler factory as the local hook. Action tuples in, event handlers out.

The built-in SimpleStore is fine for most apps. When you want time-travel, devtools, persistence, observability, or fine-grained reactivity, install one of the adapter packages — same <StoreProvider> and useStore / useActions on top.

Terminal window
pnpm add @input-actions/redux @reduxjs/toolkit
import { createReduxStoreSync } from '@input-actions/redux';
const store = createReduxStoreSync({ count: 0 });

All adapters expose the same Store interface. Components don’t change when you swap backends — the action vocabulary is the contract.

useActions accepts every action variant, including async ('action/async-prevent', etc.) and state-machine actions ('machine/transition', etc.). When you’re ready, register asyncHandlers and stateMachines on the same StoreProvider:

<StoreProvider
store={store}
asyncHandlers={{ submit: submitForm }}
stateMachines={[checkoutMachine]}
>
…
</StoreProvider>

The full chat example exercises both layers; see the chat example.