Summary
Ensure easy-peasy works correctly when imported in React Server Component environments (Next.js App Router, etc.).
Parent issue: #1004
Background
easy-peasy is fundamentally a client-side state management library. RSC compatibility means ensuring it doesn't break when imported in a server context, not making it work in server components. Currently, importing easy-peasy in a Next.js App Router project without 'use client' will fail because the hooks use useContext.
Changes
1. Add 'use client' directive
- Add
'use client' to src/index.js (main entry point)
- Ensures bundlers correctly mark the module as client-only
2. Optional server-only export
- Consider exposing
easy-peasy/server with just createStore (no hooks, no context)
- Useful for server-side store initialization in RSC apps (e.g. pre-populating state before streaming)
Existing SSR safeguards (already in place)
getBrowserStorage in persistence.js guards against window being undefined
getServerSnapshot is set to store.getState in hooks — correct for SSR hydration
Testing
- Test importing easy-peasy in a minimal Next.js App Router setup
- Verify no breakage in existing SSR scenarios
- Verify server export works for store initialization without React hooks
Summary
Ensure easy-peasy works correctly when imported in React Server Component environments (Next.js App Router, etc.).
Parent issue: #1004
Background
easy-peasy is fundamentally a client-side state management library. RSC compatibility means ensuring it doesn't break when imported in a server context, not making it work in server components. Currently, importing easy-peasy in a Next.js App Router project without
'use client'will fail because the hooks useuseContext.Changes
1. Add
'use client'directive'use client'tosrc/index.js(main entry point)2. Optional server-only export
easy-peasy/serverwith justcreateStore(no hooks, no context)Existing SSR safeguards (already in place)
getBrowserStorageinpersistence.jsguards againstwindowbeing undefinedgetServerSnapshotis set tostore.getStatein hooks — correct for SSR hydrationTesting