tanstack-react-store
4
总安装量
4
周安装量
#48561
全站排名
安装命令
npx skills add https://github.com/fellipeutaka/leon --skill tanstack-react-store
Agent 安装分布
opencode
4
gemini-cli
4
claude-code
4
github-copilot
4
codex
4
kimi-cli
4
Skill 文档
TanStack React Store
Framework-agnostic reactive data store with a React adapter. Install @tanstack/react-store.
Core API
createStore
Create a writable store with an initial value, or a readonly derived store with a getter function.
import { createStore } from "@tanstack/react-store";
// Writable store
const countStore = createStore(0);
countStore.setState(() => 1);
console.log(countStore.state); // 1
// Object store
const appStore = createStore({ dogs: 0, cats: 0 });
// Derived (readonly) store â auto-updates when dependencies change
const doubled = createStore(() => countStore.state * 2);
Store class
store.state/store.get()â read current valuestore.setState((prev) => next)â update with updater fn (not available on derived/readonly stores)store.subscribe((value) => void)â listen to changes, returns{ unsubscribe }
Derived stores â previous value
Access the previous derived value via the prev argument:
const sum = createStore<number>((prev) => count.state + (prev ?? 0));
batch
Batch multiple setState calls; subscribers fire once at the end:
import { batch } from "@tanstack/react-store";
batch(() => {
countStore.setState(() => 1);
countStore.setState(() => 2);
});
React Integration
useStore
Subscribe a React component to a store. Accepts a selector for fine-grained re-renders.
import { createStore, useStore } from "@tanstack/react-store";
const store = createStore({ dogs: 0, cats: 0 });
function Display({ animal }: { animal: "dogs" | "cats" }) {
// Only re-renders when state[animal] changes
const count = useStore(store, (state) => state[animal]);
return <div>{`${animal}: ${count}`}</div>;
}
Signature:
function useStore<TAtom extends AnyAtom | undefined, T>(
atom: TAtom,
selector: (snapshot: AtomState) => T,
compare?: (a: T, b: T) => boolean,
): T;
atomâ store or atom instanceselectorâ derive the slice of state needed (keeps re-renders minimal)compareâ optional custom equality check (default:Object.is)
shallow
Use shallow as the compare function when selecting objects/arrays to avoid unnecessary re-renders:
import { useStore, shallow } from "@tanstack/react-store";
const items = useStore(store, (s) => s.items, shallow);
Updating state from event handlers
Call setState outside of React â no hooks needed:
const updateState = (animal: string) => {
store.setState((state) => ({
...state,
[animal]: state[animal] + 1,
}));
};
Best Practices
- Define stores outside components â stores are singletons; instantiate at module level.
- Use selectors â always pass a selector to
useStoreto avoid full-state re-renders. - Use
shallowâ when selecting objects/arrays, passshallowas the compare fn. - Batch related updates â wrap multiple
setStatecalls inbatch()to notify subscribers once. - Prefer derived stores over manual sync â use
createStore(() => ...)for computed values instead of manually keeping stores in sync. - Immutable updates â
setStateupdater must return a new reference (spread objects/arrays). - Cleanup subscriptions â call
unsubscribe()returned bystore.subscribe()when done.
API Reference
For detailed type signatures and advanced APIs (atoms, async atoms, observers), see references/api-reference.md.