redux-to-swr

Migrate React components from Redux and redux-saga data fetching to SWR hooks.

32.4k|4.1k|Updated Feb 6, 2014
One-click install
npx skills add https://github.com/cockroachdb/cockroach --skill redux-to-swr
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: redux-to-swr
Source: https://github.com/cockroachdb/cockroach/tree/main/pkg/ui/.claude/skills/redux-to-swr
Command: npx skills add https://github.com/cockroachdb/cockroach --skill redux-to-swr

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Converting React components from Redux + redux-saga data fetching to SWR hooks involves many coordinated changes across reducers, sagas, selectors, connected components, and tests, and missing any step leaves stale store slices or broken selectors behind.

Core Features & Use Cases

  • Step-by-step migration workflow: Create SWR hooks with useSwrWithClusterId, convert connected components to hooks, simplify wrapper components, and delete Redux artifacts in the correct order.
  • SWR configuration patterns: Guidance on keys, polling via refreshInterval, conditional fetching with null keys or fetchers, immutable data, and cache invalidation after mutations using bound mutate.
  • Testing and cleanup guidance: Patterns for mocking SWR hooks with jest.spyOn, testing loading/error/role states, extracting pure functions from selectors, and gradual migration via an SWR-to-Redux bridge.
  • Use Case: A developer modernizing a CockroachDB DB Console page replaces a CachedDataReducer, its sagas, and a connect() HOC with a single co-located SWR hook and a functional component, then removes the store registrations and updates tests.

Quick Start

Migrate the schema insights page in cluster-ui from Redux and sagas to SWR hooks following this migration guide.

Frequently Asked Questions about redux-to-swr

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

FAQPage Schema
How do I migrate a React component from Redux to SWR?

Create an SWR hook wrapping the existing fetcher, replace mapStateToProps and mapDispatchToProps with direct hook calls, then delete the reducer, saga, and selector files. Finally remove the slice from combineReducers and the saga fork registration.

How do I replace redux-saga with SWR for data fetching?

Replace the saga's watch-and-dispatch flow with a useSwrWithClusterId hook that calls the API fetcher directly. SWR handles caching, deduplication, and revalidation, eliminating the need for success/failure actions and reducer updates.

How do I test React components that use SWR hooks with Jest?

Mock the hook with jest.spyOn on the API module and return the full SWR shape including data, isLoading, error, mutate, and isValidating. Wrap components in MemoryRouter when they use useParams or useHistory.

Can SWR and Redux coexist during a gradual migration?

Yes, use a bridge component that reads SWR hook state and dispatches it into a minimal Redux slice so existing selectors and alert banners keep working. Remove the bridge once all consumers are migrated.

How do I refresh SWR cache after a mutation?

Destructure the bound mutate from the read hook and call it after a successful write performed via useSwrMutationWithClusterId. Prefer the bound mutate over SWR's global mutate so the correct cache key is revalidated.