Skip to content

feat(rsc): add React Server Components and Next.js App Router compatibility #1014

@ctrlplusb

Description

@ctrlplusb

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions