sfnext-state-management
БесплатноБез исполняемых скриптовНе проверенChoose the right state mechanism in a Storefront Next project: loader and action data, useRouteLoaderData, URL state with useSearchParams, cookies and…
от SalesforceCommerceCloud★ 58 звёзд на GitHubОб этом скилле
Storefront Next State Management
Server state lives in loaders and actions. Client state is small and local. There is no global client store library in the template (Zustand was removed): use the tools below in this order. docs/README-STATE.md and docs/README-DATA.md in your project cover each in depth.
Decision table
| State | Use |
|---|---|
| Data from SCAPI | loader + loaderData; action for writes |
| Another route's loader data | useRouteLoaderData('routes/_app') (route id = file path without extension) |
| Filters, sort, pagination, tab | URL: useSearchParams / useParams (shareable, SSR-friendly) |
| Must survive requests, read by server | cookies (createCookie from @/lib/cookie-utils.server) or session helpers |
| Pending or optimistic result | fetcher.formData, useNavigation, useOptimistic |
| Form fields, toggles, open/close in one component | useState / useReducer |
| Shared by a subtree | React context with a provider at the owning layout |
| Shared app-wide and read outside React, or high-frequency | module-level useSyncExternalStore store |
| Request-scoped server values (site, auth, basket) | router context (middleware), read with context.get(...) |
Prefer the earliest row that works. Never copy loader data into useState to "cache" it.
Existing providers and stores
| Need | API |
|---|---|
| Basket | BasketProvider in src/providers/basket.tsx: useBasket({ autoLoad }), useBasketSnapshot, useBasketHydrated, useBasketError, useBasketUpdater, useBasketReset, useBasketLoader, useBasketReconcile; BasketCookieReconciler syncs the cookie |
| Session info | useAuth() from @/providers/auth |
| Product / variant selection | @/providers/product-context, @/providers/product-view |
| Image widths | DynamicImageProvider (@/providers/dynamic-image) |
| Wishlist | src/providers/wishlist.ts (module-level store) |
| Mini-cart open state | useMiniCartStore, setMiniCartOpen in src/hooks/mini-cart-store.ts |
| Cart toast | src/hooks/cart-mutation-toast-store.ts |
| Shopper context | useShopperContext (@/hooks/use-shopper-context) |
The basket is loaded through useScapiFetcher('shopperBasketsV2', 'getBasket') and is updated from action results via updateBasketResource (server side, @/middlewares/basket.server), so after a basket action components simply re-render from useBasket(). The __sfdc_basket cookie carries the basket id; __sfdc_usertype is the user-type hint. Do not read these directly in components; use the hooks. Server reads: getBasket, getBasketSnapshot, ensureBasketId, destroyBasket, and getAuth, updateAuth, destroyAuth from @/middlewares/auth.server.
Optimistic UI
const fetcher = useFetcher<typeof action>();
const removing = fetcher.formData?.get('itemId') === item.itemId;
return <li hidden={removing}>...</li>;
Use useOptimistic for values the server will confirm, and useNavigation for URL-driven pending states. Roll back by reading the confirmed loaderData/basket value; never hand-maintain a parallel copy.
A small external store
Use when state is global, changes often, or must be set outside React. Follow src/hooks/mini-cart-store.ts:
import { useSyncExternalStore } from 'react';
interface State { open: boolean }
let state: State = { open: false };
const SERVER_STATE: State = Object.freeze({ open: false });
const listeners = new Set<() => void>();
const subscribe = (l: () => void) => { listeners.add(l); return () => void listeners.delete(l); };
export const setOpen = (open: boolean) => {
if (open === state.open) return; // skip no-op writes
state = { ...state, open };
listeners.forEach((l) => l());
};
export function useDrawerStore<T>(select: (s: State) => T): T {
return useSyncExternalStore(subscribe, () => select(state), () => select(SERVER_STATE));
}
Rules:
getSnapshotmust return a referentially stable value when nothing changed. Select primitives; a selector that allocates per call loops forever.- Provide a server snapshot and mutate only from events or effects so SSR and the first client render match (no hydration warnings).
- Split stores by independent writers so each consumer subscribes to a slice.
- For an instance-based store (one per widget), create it in a factory and provide it by context, as
createStoreLocatorStoredoes in the store-locator extension.
Context providers
- Put the provider at the lowest layout that contains all consumers.
- Memoize the
value(useMemo,useCallback) and split read-only state from updaters into separate contexts so updater-only consumers do not re-render. - Use a selector pattern for large contexts (see README-STATE "Context Selector Pattern").
- Values used only inside callbacks belong in
useRef, not context state. - Compose providers with
src/providers/compose-providers.tsx.
More patterns and anti-patterns in [PATTERNS.md](references/PATTER
Установить sfnext-state-management в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл sfnext-state-management?
Choose the right state mechanism in a Storefront Next project: loader and action data, useRouteLoaderData, URL state with useSearchParams, cookies and sessions, optimistic UI (fetcher.formData, useNavigation, useOptimistic), React context providers (BasketProvider, AuthProvider, wishlist, product-context), and small useSyncExternalStore stores such as the mini-cart store. Use for "where should this state live", "useBasket", "useAuth", "share state between components", "optimistic add to cart", "global client state", "hydration mismatch from a store", or "do I need Zustand". Do not use for fetching or mutating data (use `storefront-next:sfnext-data-fetching`), controlling loader re-runs (use `storefront-next:sfnext-revalidation`), or login flows and sessions (use `storefront-next:sfnext-authentication`).
Как установить скилл sfnext-state-management?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл sfnext-state-management запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
skill-creator
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
от Anthropicdiscernment-nudge
After you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
от Anthropicdata-consolidate
Joins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
от Anthropicinspection-to-capa
Runs the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…
от Anthropic