Skip to content
OpenSmartRoute
Skillv1.0.0

recoil

You are an expert in Recoil, the state management library by Meta for React applications. You help developers manage application state with atoms (shared state units), selectors (derived/async state),

by terminalskills(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from terminalskills/skills (skills/recoil/SKILL.md). Install upstream with npx skills add terminalskills/skills --skill recoil. Copyright stays with the author (Apache-2.0).

Recoil — State Management for React

You are an expert in Recoil, the state management library by Meta for React applications. You help developers manage application state with atoms (shared state units), selectors (derived/async state), and atom families — providing a graph-based state model that integrates naturally with React's concurrent features, avoids unnecessary re-renders, and handles async data fetching as first-class state.

Core Capabilities

Atoms and Selectors

import { atom, selector, useRecoilState, useRecoilValue, RecoilRoot } from "recoil";

// Atoms — units of state
const todosAtom = atom<Todo[]>({
  key: "todos",
  default: [],
});

const filterAtom = atom<"all" | "active" | "completed">({
  key: "todoFilter",
  default: "all",
});

// Selectors — derived state (auto-updates when dependencies change)
const filteredTodosSelector = selector({
  key: "filteredTodos",
  get: ({ get }) => {
    const todos = get(todosAtom);
    const filter = get(filterAtom);
    switch (filter) {
      case "active": return todos.filter((t) => !t.done);
      case "completed": return todos.filter((t) => t.done);
      default: return todos;
    }
  },
});

const statsSelector = selector({
  key: "todoStats",
  get: ({ get }) => {
    const todos = get(todosAtom);
    return {
      total: todos.length,
      completed: todos.filter((t) => t.done).length,
      percent: todos.length ? Math.round(todos.filter((t) => t.done).length / todos.length * 100) : 0,
    };
  },
});

// Components
function TodoList() {
  const filteredTodos = useRecoilValue(filteredTodosSelector);  // Only re-renders when filtered list changes
  return (
    <ul>
      {filteredTodos.map((todo) => <TodoItem key={todo.id} id={todo.id} />)}
    </ul>
  );
}

function TodoStats() {
  const stats = useRecoilValue(statsSelector);
  return <p>{stats.completed}/{stats.total} done ({stats.percent}%)</p>;
}

function FilterButtons() {
  const [filter, setFilter] = useRecoilState(filterAtom);
  return (
    <div>
      {(["all", "active", "completed"] as const).map((f) => (
        <button key={f} onClick={() => setFilter(f)}
          style={{ fontWeight: filter === f ? "bold" : "normal" }}>
          {f}
        </button>
      ))}
    </div>
  );
}

function App() {
  return (
    <RecoilRoot>
      <TodoStats />
      <FilterButtons />
      <TodoList />
    </RecoilRoot>
  );
}

Async Selectors and Atom Families

// Async selector — fetches data, integrates with Suspense
const userProfileSelector = selector({
  key: "userProfile",
  get: async ({ get }) => {
    const userId = get(currentUserIdAtom);
    const response = await fetch(`/api/users/${userId}`);
    return response.json();
  },
});

// Atom families — parameterized atoms (one atom per ID)
const todoItemAtom = atomFamily<Todo | null, string>({
  key: "todoItem",
  default: null,
});

// Selector families — parameterized selectors
const userByIdSelector = selectorFamily<User, string>({
  key: "userById",
  get: (userId) => async ({ get }) => {
    const response = await fetch(`/api/users/${userId}`);
    return response.json();
  },
});

// Usage with Suspense
function UserProfile() {
  const profile = useRecoilValue(userProfileSelector);  // Suspends until loaded
  return <h1>{profile.name}</h1>;
}

function App() {
  return (
    <RecoilRoot>
      <Suspense fallback={<Spinner />}>
        <UserProfile />
      </Suspense>
    </RecoilRoot>
  );
}

Installation

npm install recoil

Best Practices

  1. Unique keys — Every atom and selector needs a globally unique key string; use descriptive names
  2. Selectors for derived state — Never compute derived state in components; use selectors for memoized computation
  3. Atom families — Use atomFamily for collections (todo items, user profiles); one atom per entity
  4. Async selectors — Return promises from selector get; pairs with React Suspense for loading states
  5. Fine-grained atoms — Prefer many small atoms over one large one; minimizes re-renders
  6. RecoilRoot — Wrap app once; provides state context; can nest for isolated state subtrees
  7. useRecoilValue — Use when you only read; useRecoilState when you also write; avoids unnecessary subscriptions
  8. Concurrent mode — Recoil is designed for React concurrent features; async selectors work with Suspense transitions

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/terminalskills-skills-recoil/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

terminalskills-skills-recoil.ocm.jsonjson
{
  "ocm": "1",
  "id": "terminalskills-skills-recoil",
  "kind": "skill",
  "name": "recoil",
  "description": "You are an expert in Recoil, the state management library by Meta for React applications. You help developers manage application state with atoms (shared state units), selectors (derived/async state), and atom families — providing a graph-based state model that integrates naturally with React's concurrent features, avoids unnecessary re-renders, and handles async data fetching as first-class state.",
  "publisher": "terminalskills",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "coding"
    ],
    "tags": [
      "skill-md",
      "state-management",
      "react",
      "atoms",
      "selectors",
      "meta",
      "async",
      "graph",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "You are an expert in Recoil, the state management library by Meta for React applications. You help developers manage application state with atoms (shared state units), selectors (derived/async state), and atom families — providing a graph-based state model that integrates naturally with React's concurrent features, avoids unnecessary re-renders, and handles async data fetching as first-class state."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/terminalskills/skills",
      "path": "skills/recoil/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/terminalskills/skills/blob/HEAD/skills/recoil/SKILL.md",
      "key": "terminalskills/skills/skills/recoil/SKILL.md"
    },
    "license": "Apache-2.0"
  },
  "instructions": "# Recoil — State Management for React\n\nYou are an expert in Recoil, the state management library by Meta for React applications. You help developers manage application state with atoms (shared state units), selectors (derived/async state), and atom families — providing a graph-based state model that integrates naturally with React's concurrent features, avoids unnecessary re-renders, and handles async data fetching as first-class state.\n\n## Core Capabilities\n\n### Atoms and Selectors\n\n```tsx\nimport { atom, selector, useRecoilState, useRecoilValue, RecoilRoot } from \"recoil\";\n\n// Atoms — units o",
  "cost": {
    "context_tokens": 1101
  }
}

Fetch it by URL: GET /api/v1/registry/terminalskills-skills-recoil/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.