context-pattern

Create a type-safe React Context pattern using useSyncExternalStore.

Updated May 20, 2025
One-click install
npx skills add https://github.com/trae-op/electron-password-generation --skill context-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: context-pattern
Source: https://github.com/trae-op/electron-password-generation/tree/main/.github/skills/%D1%81ontext-pattern
Command: npx skills add https://github.com/trae-op/electron-password-generation --skill context-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill provides a reusable, type-safe React Context pattern using useSyncExternalStore to minimize re-renders and simplify consumption via selectors and dispatchers.

Core Features & Use Cases

  • Type-safe Context pattern with separate files for Context.tsx, types.ts, useContext.ts, and useSelectors.ts
  • useSyncExternalStore-based selectors to subscribe to specific state slices without unnecessary re-renders
  • Lightweight, scalable shared state management suitable for large React apps and codebase refactors

Quick Start

Create a new feature context by following the prescribed file structure under context/ and implement the four files: Context.tsx, types.ts, useContext.ts, and useSelectors.ts. Then wrap components with the Provider and use the provided hooks and selectors to access and modify state.

Frequently Asked Questions about context-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I minimize re-renders with React Context and useSyncExternalStore?

To minimize re-renders with React Context, use a useSyncExternalStore-based pattern to let components subscribe to specific state slices via selectors. This prevents unnecessary renders when unrelated context values change.

What is the best way to structure a type-safe React Context for large apps?

A type-safe React Context pattern for large apps requires a four-file structure: Context.tsx, types.ts, useContext.ts, and useSelectors.ts. This separates type definitions, context creation, and hook consumption for scalable shared state.

How do I create a selector hook for React state management without external libraries?

Create a selector hook by leveraging useSyncExternalStore with a Set for subscribers and useRef for state storage. Wrap dispatchers in useCallback to provide a lightweight, type-safe state management solution without external dependencies.

Can I use useSyncExternalStore to replace Redux for shared state in React?

Yes, you can use useSyncExternalStore to build a type-safe React Context pattern that replaces heavier state management libraries. It provides scalable shared state with selectors and dispatchers suitable for large app refactors.

When should I not use React Context for state management?

Avoid standard React Context for state management when frequent updates occur, as it triggers re-renders for all consumers. Instead, apply a useSyncExternalStore pattern with selectors to subscribe only to necessary state slices.