[
  {
    "path": ".github/workflows/react-best-practices-ci.yml",
    "content": "name: React Best Practices CI\n\non:\n  push:\n    branches: [main]\n    paths:\n      - 'skills/react-best-practices/**'\n      - 'packages/react-best-practices-build/**'\n  pull_request:\n    branches: [main]\n    paths:\n      - 'skills/react-best-practices/**'\n      - 'packages/react-best-practices-build/**'\n\njobs:\n  validate:\n    runs-on: ubuntu-latest\n    defaults:\n      run:\n        working-directory: packages/react-best-practices-build\n    steps:\n      - uses: actions/checkout@v4\n      - uses: pnpm/action-setup@v2\n        with:\n          version: 10.24.0\n      - uses: actions/setup-node@v4\n        with:\n          node-version: '20'\n          cache: 'pnpm'\n          cache-dependency-path: packages/react-best-practices-build/pnpm-lock.yaml\n      - run: pnpm install\n      - run: pnpm validate\n      - run: pnpm build\n"
  },
  {
    "path": ".gitignore",
    "content": ".DS_Store\n.vercel\n.env*.local\n"
  },
  {
    "path": "AGENTS.md",
    "content": "# AGENTS.md\n\nThis file provides guidance to AI coding agents (Claude Code, Cursor, Copilot, etc.) when working with code in this repository.\n\n## Repository Overview\n\nA collection of skills for Claude.ai and Claude Code for working with Vercel deployments. Skills are packaged instructions and scripts that extend Claude's capabilities.\n\n## Creating a New Skill\n\n### Directory Structure\n\n```\nskills/\n  {skill-name}/           # kebab-case directory name\n    SKILL.md              # Required: skill definition\n    scripts/              # Required: executable scripts\n      {script-name}.sh    # Bash scripts (preferred)\n  {skill-name}.zip        # Required: packaged for distribution\n```\n\n### Naming Conventions\n\n- **Skill directory**: `kebab-case` (e.g., `vercel-deploy`, `log-monitor`)\n- **SKILL.md**: Always uppercase, always this exact filename\n- **Scripts**: `kebab-case.sh` (e.g., `deploy.sh`, `fetch-logs.sh`)\n- **Zip file**: Must match directory name exactly: `{skill-name}.zip`\n\n### SKILL.md Format\n\n```markdown\n---\nname: {skill-name}\ndescription: {One sentence describing when to use this skill. Include trigger phrases like \"Deploy my app\", \"Check logs\", etc.}\n---\n\n# {Skill Title}\n\n{Brief description of what the skill does.}\n\n## How It Works\n\n{Numbered list explaining the skill's workflow}\n\n## Usage\n\n```bash\nbash /mnt/skills/user/{skill-name}/scripts/{script}.sh [args]\n```\n\n**Arguments:**\n- `arg1` - Description (defaults to X)\n\n**Examples:**\n{Show 2-3 common usage patterns}\n\n## Output\n\n{Show example output users will see}\n\n## Present Results to User\n\n{Template for how Claude should format results when presenting to users}\n\n## Troubleshooting\n\n{Common issues and solutions, especially network/permissions errors}\n```\n\n### Best Practices for Context Efficiency\n\nSkills are loaded on-demand — only the skill name and description are loaded at startup. The full `SKILL.md` loads into context only when the agent decides the skill is relevant. To minimize context usage:\n\n- **Keep SKILL.md under 500 lines** — put detailed reference material in separate files\n- **Write specific descriptions** — helps the agent know exactly when to activate the skill\n- **Use progressive disclosure** — reference supporting files that get read only when needed\n- **Prefer scripts over inline code** — script execution doesn't consume context (only output does)\n- **File references work one level deep** — link directly from SKILL.md to supporting files\n\n### Script Requirements\n\n- Use `#!/bin/bash` shebang\n- Use `set -e` for fail-fast behavior\n- Write status messages to stderr: `echo \"Message\" >&2`\n- Write machine-readable output (JSON) to stdout\n- Include a cleanup trap for temp files\n- Reference the script path as `/mnt/skills/user/{skill-name}/scripts/{script}.sh`\n\n### Creating the Zip Package\n\nAfter creating or updating a skill:\n\n```bash\ncd skills\nzip -r {skill-name}.zip {skill-name}/\n```\n\n### End-User Installation\n\nDocument these two installation methods for users:\n\n**Claude Code:**\n```bash\ncp -r skills/{skill-name} ~/.claude/skills/\n```\n\n**claude.ai:**\nAdd the skill to project knowledge or paste SKILL.md contents into the conversation.\n\nIf the skill requires network access, instruct users to add required domains at `claude.ai/settings/capabilities`.\n"
  },
  {
    "path": "README.md",
    "content": "# Agent Skills\n\nA collection of skills for AI coding agents. Skills are packaged instructions and scripts that extend agent capabilities.\n\nSkills follow the [Agent Skills](https://agentskills.io/) format.\n\n## Available Skills\n\n### react-best-practices\n\nReact and Next.js performance optimization guidelines from Vercel Engineering. Contains 40+ rules across 8 categories, prioritized by impact.\n\n**Use when:**\n- Writing new React components or Next.js pages\n- Implementing data fetching (client or server-side)\n- Reviewing code for performance issues\n- Optimizing bundle size or load times\n\n**Categories covered:**\n- Eliminating waterfalls (Critical)\n- Bundle size optimization (Critical)\n- Server-side performance (High)\n- Client-side data fetching (Medium-High)\n- Re-render optimization (Medium)\n- Rendering performance (Medium)\n- JavaScript micro-optimizations (Low-Medium)\n\n### web-design-guidelines\n\nReview UI code for compliance with web interface best practices. Audits your code for 100+ rules covering accessibility, performance, and UX.\n\n**Use when:**\n- \"Review my UI\"\n- \"Check accessibility\"\n- \"Audit design\"\n- \"Review UX\"\n- \"Check my site against best practices\"\n\n**Categories covered:**\n- Accessibility (aria-labels, semantic HTML, keyboard handlers)\n- Focus States (visible focus, focus-visible patterns)\n- Forms (autocomplete, validation, error handling)\n- Animation (prefers-reduced-motion, compositor-friendly transforms)\n- Typography (curly quotes, ellipsis, tabular-nums)\n- Images (dimensions, lazy loading, alt text)\n- Performance (virtualization, layout thrashing, preconnect)\n- Navigation & State (URL reflects state, deep-linking)\n- Dark Mode & Theming (color-scheme, theme-color meta)\n- Touch & Interaction (touch-action, tap-highlight)\n- Locale & i18n (Intl.DateTimeFormat, Intl.NumberFormat)\n\n### react-native-guidelines\n\nReact Native best practices optimized for AI agents. Contains 16 rules across 7 sections covering performance, architecture, and platform-specific patterns.\n\n**Use when:**\n- Building React Native or Expo apps\n- Optimizing mobile performance\n- Implementing animations or gestures\n- Working with native modules or platform APIs\n\n**Categories covered:**\n- Performance (Critical) - FlashList, memoization, heavy computation\n- Layout (High) - flex patterns, safe areas, keyboard handling\n- Animation (High) - Reanimated, gesture handling\n- Images (Medium) - expo-image, caching, lazy loading\n- State Management (Medium) - Zustand patterns, React Compiler\n- Architecture (Medium) - monorepo structure, imports\n- Platform (Medium) - iOS/Android specific patterns\n\n### composition-patterns\n\nReact composition patterns that scale. Helps avoid boolean prop proliferation through compound components, state lifting, and internal composition.\n\n**Use when:**\n- Refactoring components with many boolean props\n- Building reusable component libraries\n- Designing flexible APIs\n- Reviewing component architecture\n\n**Patterns covered:**\n- Extracting compound components\n- Lifting state to reduce props\n- Composing internals for flexibility\n- Avoiding prop drilling\n\n### vercel-deploy-claimable\n\nDeploy applications and websites to Vercel instantly. Designed for use with claude.ai and Claude Desktop to enable deployments directly from conversations. Deployments are \"claimable\" - users can transfer ownership to their own Vercel account.\n\n**Use when:**\n- \"Deploy my app\"\n- \"Deploy this to production\"\n- \"Push this live\"\n- \"Deploy and give me the link\"\n\n**Features:**\n- Auto-detects 40+ frameworks from `package.json`\n- Returns preview URL (live site) and claim URL (transfer ownership)\n- Handles static HTML projects automatically\n- Excludes `node_modules` and `.git` from uploads\n\n**How it works:**\n1. Packages your project into a tarball\n2. Detects framework (Next.js, Vite, Astro, etc.)\n3. Uploads to deployment service\n4. Returns preview URL and claim URL\n\n**Output:**\n```\nDeployment successful!\n\nPreview URL: https://skill-deploy-abc123.vercel.app\nClaim URL:   https://vercel.com/claim-deployment?code=...\n```\n\n## Installation\n\n```bash\nnpx skills add vercel-labs/agent-skills\n```\n\n## Usage\n\nSkills are automatically available once installed. The agent will use them when relevant tasks are detected.\n\n**Examples:**\n```\nDeploy my app\n```\n```\nReview this React component for performance issues\n```\n```\nHelp me optimize this Next.js page\n```\n\n## Skill Structure\n\nEach skill contains:\n- `SKILL.md` - Instructions for the agent\n- `scripts/` - Helper scripts for automation (optional)\n- `references/` - Supporting documentation (optional)\n\n## License\n\nMIT\n"
  },
  {
    "path": "packages/react-best-practices-build/.gitignore",
    "content": "node_modules/\n"
  },
  {
    "path": "packages/react-best-practices-build/package.json",
    "content": "{\n  \"name\": \"react-best-practices-build\",\n  \"version\": \"1.0.0\",\n  \"description\": \"Build tooling for React Best Practices and React Native Guidelines skills\",\n  \"type\": \"module\",\n  \"scripts\": {\n    \"build\": \"pnpm build-agents && pnpm extract-tests\",\n    \"build-agents\": \"tsx src/build.ts\",\n    \"build-all\": \"tsx src/build.ts --all\",\n    \"build-react\": \"tsx src/build.ts --skill=react-best-practices\",\n    \"build-rn\": \"tsx src/build.ts --skill=react-native-skills\",\n    \"build-composition\": \"tsx src/build.ts --skill=composition-patterns\",\n    \"validate\": \"tsx src/validate.ts\",\n    \"extract-tests\": \"tsx src/extract-tests.ts\",\n    \"migrate\": \"tsx src/migrate.ts\",\n    \"dev\": \"pnpm build && pnpm validate\"\n  },\n  \"keywords\": [\n    \"react\",\n    \"performance\",\n    \"guidelines\",\n    \"llm\",\n    \"agents\"\n  ],\n  \"license\": \"MIT\",\n  \"devDependencies\": {\n    \"@types/node\": \"^20.0.0\",\n    \"tsx\": \"^4.7.0\",\n    \"typescript\": \"^5.3.0\"\n  }\n}\n"
  },
  {
    "path": "packages/react-best-practices-build/src/build.ts",
    "content": "#!/usr/bin/env node\n/**\n * Build script to compile individual rule files into AGENTS.md\n */\n\nimport { readdir, readFile, writeFile } from 'fs/promises'\nimport { join } from 'path'\nimport { Rule, Section, GuidelinesDocument, ImpactLevel } from './types.js'\nimport { parseRuleFile, RuleFile } from './parser.js'\nimport { SKILLS, SkillConfig, DEFAULT_SKILL } from './config.js'\n\n// Parse command line arguments\nconst args = process.argv.slice(2)\nconst upgradeVersion = args.includes('--upgrade-version')\nconst skillArg = args.find((arg) => arg.startsWith('--skill='))\nconst skillName = skillArg ? skillArg.split('=')[1] : null\nconst buildAll = args.includes('--all')\n\n/**\n * Increment a semver-style version string (e.g., \"0.1.0\" -> \"0.1.1\", \"1.0\" -> \"1.1\")\n */\nfunction incrementVersion(version: string): string {\n  const parts = version.split('.').map(Number)\n  // Increment the last part\n  parts[parts.length - 1]++\n  return parts.join('.')\n}\n\n/**\n * Generate markdown from rules\n */\nfunction generateMarkdown(\n  sections: Section[],\n  metadata: {\n    version: string\n    organization: string\n    date: string\n    abstract: string\n    references?: string[]\n  },\n  skillConfig: SkillConfig\n): string {\n  let md = `# ${skillConfig.title}\\n\\n`\n  md += `**Version ${metadata.version}**  \\n`\n  md += `${metadata.organization}  \\n`\n  md += `${metadata.date}\\n\\n`\n  md += `> **Note:**  \\n`\n  md += `> This document is mainly for agents and LLMs to follow when maintaining,  \\n`\n  md += `> generating, or refactoring ${skillConfig.description}. Humans  \\n`\n  md += `> may also find it useful, but guidance here is optimized for automation  \\n`\n  md += `> and consistency by AI-assisted workflows.\\n\\n`\n  md += `---\\n\\n`\n  md += `## Abstract\\n\\n`\n  md += `${metadata.abstract}\\n\\n`\n  md += `---\\n\\n`\n  md += `## Table of Contents\\n\\n`\n\n  // Generate TOC\n  sections.forEach((section) => {\n    md += `${section.number}. [${section.title}](#${\n      section.number\n    }-${section.title.toLowerCase().replace(/\\s+/g, '-')}) — **${\n      section.impact\n    }**\\n`\n    section.rules.forEach((rule) => {\n      // GitHub generates anchors from the full heading text: \"1.1 Title\" -> \"#11-title\"\n      const anchor = `${rule.id} ${rule.title}`\n        .toLowerCase()\n        .replace(/\\s+/g, '-')\n        .replace(/[^\\w-]/g, '') // Remove special characters except hyphens\n      md += `   - ${rule.id} [${rule.title}](#${anchor})\\n`\n    })\n  })\n\n  md += `\\n---\\n\\n`\n\n  // Generate sections\n  sections.forEach((section) => {\n    md += `## ${section.number}. ${section.title}\\n\\n`\n    md += `**Impact: ${section.impact}${\n      section.impactDescription ? ` (${section.impactDescription})` : ''\n    }**\\n\\n`\n    if (section.introduction) {\n      md += `${section.introduction}\\n\\n`\n    }\n\n    section.rules.forEach((rule) => {\n      md += `### ${rule.id} ${rule.title}\\n\\n`\n      md += `**Impact: ${rule.impact}${\n        rule.impactDescription ? ` (${rule.impactDescription})` : ''\n      }**\\n\\n`\n      md += `${rule.explanation}\\n\\n`\n\n      rule.examples.forEach((example) => {\n        if (example.description) {\n          md += `**${example.label}: ${example.description}**\\n\\n`\n        } else {\n          md += `**${example.label}:**\\n\\n`\n        }\n        // Only generate code block if there's actual code\n        if (example.code && example.code.trim()) {\n          md += `\\`\\`\\`${example.language || 'typescript'}\\n`\n          md += `${example.code}\\n`\n          md += `\\`\\`\\`\\n\\n`\n        }\n        if (example.additionalText) {\n          md += `${example.additionalText}\\n\\n`\n        }\n      })\n\n      if (rule.references && rule.references.length > 0) {\n        md += `Reference: ${rule.references\n          .map((ref) => `[${ref}](${ref})`)\n          .join(', ')}\\n\\n`\n      }\n    })\n\n    md += `---\\n\\n`\n  })\n\n  // Add references section\n  if (metadata.references && metadata.references.length > 0) {\n    md += `## References\\n\\n`\n    metadata.references.forEach((ref, i) => {\n      md += `${i + 1}. [${ref}](${ref})\\n`\n    })\n  }\n\n  return md\n}\n\n/**\n * Build a single skill\n */\nasync function buildSkill(skillConfig: SkillConfig) {\n  console.log(`\\nBuilding ${skillConfig.name}...`)\n  console.log(`  Rules directory: ${skillConfig.rulesDir}`)\n  console.log(`  Output file: ${skillConfig.outputFile}`)\n\n  // Read all rule files (exclude files starting with _ and README.md)\n  const files = await readdir(skillConfig.rulesDir)\n  const ruleFiles = files\n    .filter((f) => f.endsWith('.md') && !f.startsWith('_') && f !== 'README.md')\n    .sort() // Sort filenames for consistent ordering across systems\n\n  const ruleData: RuleFile[] = []\n  for (const file of ruleFiles) {\n    const filePath = join(skillConfig.rulesDir, file)\n    try {\n      const parsed = await parseRuleFile(filePath, skillConfig.sectionMap)\n      ruleData.push(parsed)\n    } catch (error) {\n      console.error(`  Error parsing ${file}:`, error)\n    }\n  }\n\n  // Group rules by section\n  const sectionsMap = new Map<number, Section>()\n\n  ruleData.forEach(({ section, rule }) => {\n    if (!sectionsMap.has(section)) {\n      sectionsMap.set(section, {\n        number: section,\n        title: `Section ${section}`, // Will be overridden by section metadata\n        impact: rule.impact,\n        rules: [],\n      })\n    }\n    sectionsMap.get(section)!.rules.push(rule)\n  })\n\n  // Sort rules within each section by title (using en-US locale for consistency across environments)\n  sectionsMap.forEach((section) => {\n    section.rules.sort((a, b) =>\n      a.title.localeCompare(b.title, 'en-US', { sensitivity: 'base' })\n    )\n\n    // Assign IDs based on sorted order\n    section.rules.forEach((rule, index) => {\n      rule.id = `${section.number}.${index + 1}`\n      rule.subsection = index + 1\n    })\n  })\n\n  // Convert to array and sort\n  const sections = Array.from(sectionsMap.values()).sort(\n    (a, b) => a.number - b.number\n  )\n\n  // Read section metadata from consolidated _sections.md file\n  const sectionsFile = join(skillConfig.rulesDir, '_sections.md')\n  try {\n    const sectionsContent = await readFile(sectionsFile, 'utf-8')\n\n    // Parse sections using regex to match each section block\n    const sectionBlocks = sectionsContent\n      .split(/(?=^## \\d+\\. )/m)\n      .filter(Boolean)\n\n    for (const block of sectionBlocks) {\n      // Extract section number and title, removing section ID in parentheses\n      const headerMatch = block.match(/^## (\\d+)\\.\\s+(.+?)(?:\\s+\\([^)]+\\))?$/m)\n      if (!headerMatch) continue\n\n      const sectionNumber = parseInt(headerMatch[1])\n      const sectionTitle = headerMatch[2].trim() // Strip (id) for output\n\n      // Extract impact (format: **Impact:** CRITICAL)\n      const impactMatch = block.match(/\\*\\*Impact:\\*\\*\\s+(\\w+(?:-\\w+)?)/i)\n      const impactLevel = impactMatch\n        ? (impactMatch[1].toUpperCase().replace(/-/g, '-') as ImpactLevel)\n        : 'MEDIUM'\n\n      // Extract description (format: **Description:** text)\n      const descMatch = block.match(/\\*\\*Description:\\*\\*\\s+(.+?)(?=\\n\\n##|$)/s)\n      const description = descMatch ? descMatch[1].trim() : ''\n\n      // Update section if it exists\n      const section = sections.find((s) => s.number === sectionNumber)\n      if (section) {\n        section.title = sectionTitle\n        section.impact = impactLevel\n        section.introduction = description\n      }\n    }\n  } catch (error) {\n    console.warn('  Warning: Could not read _sections.md, using defaults')\n  }\n\n  // Read metadata\n  let metadata\n  try {\n    const metadataContent = await readFile(skillConfig.metadataFile, 'utf-8')\n    metadata = JSON.parse(metadataContent)\n  } catch {\n    metadata = {\n      version: '1.0.0',\n      organization: 'Engineering',\n      date: new Date().toLocaleDateString('en-US', {\n        month: 'long',\n        year: 'numeric',\n      }),\n      abstract: `Performance optimization guide for ${skillConfig.description}, ordered by impact.`,\n    }\n  }\n\n  // Upgrade version if flag is passed\n  if (upgradeVersion) {\n    const oldVersion = metadata.version\n    metadata.version = incrementVersion(oldVersion)\n    console.log(`  Upgrading version: ${oldVersion} -> ${metadata.version}`)\n\n    // Write updated metadata.json\n    await writeFile(\n      skillConfig.metadataFile,\n      JSON.stringify(metadata, null, 2) + '\\n',\n      'utf-8'\n    )\n    console.log(`  ✓ Updated metadata.json`)\n\n    // Update SKILL.md frontmatter if it exists\n    const skillFile = join(skillConfig.skillDir, 'SKILL.md')\n    try {\n      const skillContent = await readFile(skillFile, 'utf-8')\n      const updatedSkillContent = skillContent.replace(\n        /^(---[\\s\\S]*?version:\\s*)\"[^\"]*\"([\\s\\S]*?---)$/m,\n        `$1\"${metadata.version}\"$2`\n      )\n      await writeFile(skillFile, updatedSkillContent, 'utf-8')\n      console.log(`  ✓ Updated SKILL.md`)\n    } catch {\n      // SKILL.md doesn't exist, skip\n    }\n  }\n\n  // Generate markdown\n  const markdown = generateMarkdown(sections, metadata, skillConfig)\n\n  // Write output\n  await writeFile(skillConfig.outputFile, markdown, 'utf-8')\n\n  console.log(\n    `  ✓ Built AGENTS.md with ${sections.length} sections and ${ruleData.length} rules`\n  )\n}\n\n/**\n * Main build function\n */\nasync function build() {\n  try {\n    console.log('Building AGENTS.md from rules...')\n\n    if (buildAll) {\n      // Build all skills\n      for (const skill of Object.values(SKILLS)) {\n        await buildSkill(skill)\n      }\n    } else if (skillName) {\n      // Build specific skill\n      const skill = SKILLS[skillName]\n      if (!skill) {\n        console.error(`Unknown skill: ${skillName}`)\n        console.error(`Available skills: ${Object.keys(SKILLS).join(', ')}`)\n        process.exit(1)\n      }\n      await buildSkill(skill)\n    } else {\n      // Build default skill (backwards compatibility)\n      await buildSkill(SKILLS[DEFAULT_SKILL])\n    }\n\n    console.log('\\n✓ Build complete')\n  } catch (error) {\n    console.error('Build failed:', error)\n    process.exit(1)\n  }\n}\n\nbuild()\n"
  },
  {
    "path": "packages/react-best-practices-build/src/config.ts",
    "content": "/**\n * Configuration for the build tooling\n */\n\nimport { join, dirname } from 'path'\nimport { fileURLToPath } from 'url'\n\nconst __dirname = dirname(fileURLToPath(import.meta.url))\n\n// Base paths\nexport const SKILLS_DIR = join(__dirname, '../../..', 'skills')\nexport const BUILD_DIR = join(__dirname, '..')\n\n// Skill configurations\nexport interface SkillConfig {\n  name: string\n  title: string\n  description: string\n  skillDir: string\n  rulesDir: string\n  metadataFile: string\n  outputFile: string\n  sectionMap: Record<string, number>\n}\n\nexport const SKILLS: Record<string, SkillConfig> = {\n  'react-best-practices': {\n    name: 'react-best-practices',\n    title: 'React Best Practices',\n    description: 'React and Next.js codebases',\n    skillDir: join(SKILLS_DIR, 'react-best-practices'),\n    rulesDir: join(SKILLS_DIR, 'react-best-practices/rules'),\n    metadataFile: join(SKILLS_DIR, 'react-best-practices/metadata.json'),\n    outputFile: join(SKILLS_DIR, 'react-best-practices/AGENTS.md'),\n    sectionMap: {\n      async: 1,\n      bundle: 2,\n      server: 3,\n      client: 4,\n      rerender: 5,\n      rendering: 6,\n      js: 7,\n      advanced: 8,\n    },\n  },\n  'react-native-skills': {\n    name: 'react-native-skills',\n    title: 'React Native Skills',\n    description: 'React Native codebases',\n    skillDir: join(SKILLS_DIR, 'react-native-skills'),\n    rulesDir: join(SKILLS_DIR, 'react-native-skills/rules'),\n    metadataFile: join(SKILLS_DIR, 'react-native-skills/metadata.json'),\n    outputFile: join(SKILLS_DIR, 'react-native-skills/AGENTS.md'),\n    sectionMap: {\n      rendering: 1,\n      'list-performance': 2,\n      animation: 3,\n      scroll: 4,\n      navigation: 5,\n      'react-state': 6,\n      state: 7,\n      'react-compiler': 8,\n      ui: 9,\n      'design-system': 10,\n      monorepo: 11,\n      imports: 12,\n      js: 13,\n      fonts: 14,\n    },\n  },\n  'composition-patterns': {\n    name: 'composition-patterns',\n    title: 'React Composition Patterns',\n    description: 'React codebases using composition',\n    skillDir: join(SKILLS_DIR, 'composition-patterns'),\n    rulesDir: join(SKILLS_DIR, 'composition-patterns/rules'),\n    metadataFile: join(SKILLS_DIR, 'composition-patterns/metadata.json'),\n    outputFile: join(SKILLS_DIR, 'composition-patterns/AGENTS.md'),\n    sectionMap: {\n      architecture: 1,\n      state: 2,\n      patterns: 3,\n      react19: 4,\n    },\n  },\n}\n\n// Default skill (for backwards compatibility)\nexport const DEFAULT_SKILL = 'react-best-practices'\n\n// Legacy exports for backwards compatibility\nexport const SKILL_DIR = SKILLS[DEFAULT_SKILL].skillDir\nexport const RULES_DIR = SKILLS[DEFAULT_SKILL].rulesDir\nexport const METADATA_FILE = SKILLS[DEFAULT_SKILL].metadataFile\nexport const OUTPUT_FILE = SKILLS[DEFAULT_SKILL].outputFile\n\n// Test cases are build artifacts, not part of the skill\nexport const TEST_CASES_FILE = join(BUILD_DIR, 'test-cases.json')\n"
  },
  {
    "path": "packages/react-best-practices-build/src/extract-tests.ts",
    "content": "#!/usr/bin/env node\n/**\n * Extract test cases from rules for LLM evaluation\n */\n\nimport { readdir, writeFile } from 'fs/promises'\nimport { join } from 'path'\nimport { Rule, TestCase } from './types.js'\nimport { parseRuleFile } from './parser.js'\nimport { RULES_DIR, TEST_CASES_FILE } from './config.js'\n\n/**\n * Extract test cases from a rule\n */\nfunction extractTestCases(rule: Rule): TestCase[] {\n  const testCases: TestCase[] = []\n  \n  rule.examples.forEach((example, index) => {\n    const isBad = example.label.toLowerCase().includes('incorrect') || \n                  example.label.toLowerCase().includes('wrong') ||\n                  example.label.toLowerCase().includes('bad')\n    const isGood = example.label.toLowerCase().includes('correct') ||\n                   example.label.toLowerCase().includes('good')\n    \n    if (isBad || isGood) {\n      testCases.push({\n        ruleId: rule.id,\n        ruleTitle: rule.title,\n        type: isBad ? 'bad' : 'good',\n        code: example.code,\n        language: example.language || 'typescript',\n        description: example.description || `${example.label} example for ${rule.title}`\n      })\n    }\n  })\n  \n  return testCases\n}\n\n/**\n * Main extraction function\n */\nasync function extractTests() {\n  try {\n    console.log('Extracting test cases from rules...')\n    console.log(`Rules directory: ${RULES_DIR}`)\n    console.log(`Output file: ${TEST_CASES_FILE}`)\n    \n    const files = await readdir(RULES_DIR)\n    const ruleFiles = files.filter(f => f.endsWith('.md') && !f.startsWith('_') && f !== 'README.md')\n    \n    const allTestCases: TestCase[] = []\n    \n    for (const file of ruleFiles) {\n      const filePath = join(RULES_DIR, file)\n      try {\n        const { rule } = await parseRuleFile(filePath)\n        const testCases = extractTestCases(rule)\n        allTestCases.push(...testCases)\n      } catch (error) {\n        console.error(`Error processing ${file}:`, error)\n      }\n    }\n    \n    // Write test cases as JSON\n    await writeFile(TEST_CASES_FILE, JSON.stringify(allTestCases, null, 2), 'utf-8')\n    \n    console.log(`✓ Extracted ${allTestCases.length} test cases to ${TEST_CASES_FILE}`)\n    console.log(`  - Bad examples: ${allTestCases.filter(tc => tc.type === 'bad').length}`)\n    console.log(`  - Good examples: ${allTestCases.filter(tc => tc.type === 'good').length}`)\n  } catch (error) {\n    console.error('Extraction failed:', error)\n    process.exit(1)\n  }\n}\n\nextractTests()\n"
  },
  {
    "path": "packages/react-best-practices-build/src/migrate.ts",
    "content": "#!/usr/bin/env node\n/**\n * Migration script to split RPG.md into individual rule files\n * This is a one-time script to help migrate existing content\n */\n\nimport { readFile, writeFile, mkdir } from 'fs/promises'\nimport { join } from 'path'\nimport { existsSync } from 'fs'\nimport { SKILL_DIR, RULES_DIR } from './config.js'\n\nconst RPG_FILE = join(SKILL_DIR, 'RPG.md')\n\n/**\n * Extract section number and title from heading\n */\nfunction parseSectionHeading(line: string): { number: number; title: string } | null {\n  const match = line.match(/^##\\s+(\\d+)\\.\\s+(.+)$/)\n  if (match) {\n    return {\n      number: parseInt(match[1]),\n      title: match[2].trim()\n    }\n  }\n  return null\n}\n\n/**\n * Extract rule number and title from heading\n */\nfunction parseRuleHeading(line: string): { section: number; subsection: number; title: string } | null {\n  const match = line.match(/^###\\s+(\\d+)\\.(\\d+)\\s+(.+)$/)\n  if (match) {\n    return {\n      section: parseInt(match[1]),\n      subsection: parseInt(match[2]),\n      title: match[3].trim()\n    }\n  }\n  return null\n}\n\n/**\n * Extract impact from line\n */\nfunction extractImpact(line: string): { impact: string; description?: string } | null {\n  const match = line.match(/\\*\\*Impact:\\s*(\\w+(?:-\\w+)?)\\s*(?:\\(([^)]+)\\))?/i)\n  if (match) {\n    return {\n      impact: match[1].toUpperCase().replace(/-/g, '-'),\n      description: match[2]\n    }\n  }\n  return null\n}\n\nasync function migrate() {\n  try {\n    console.log('Migrating RPG.md to individual rule files...')\n    \n    if (!existsSync(RPG_FILE)) {\n      console.error(`RPG.md not found at ${RPG_FILE}`)\n      process.exit(1)\n    }\n    \n    // Ensure rules directory exists\n    if (!existsSync(RULES_DIR)) {\n      await mkdir(RULES_DIR, { recursive: true })\n    }\n    \n    const content = await readFile(RPG_FILE, 'utf-8')\n    const lines = content.split('\\n')\n    \n    let currentSection: { number: number; title: string; impact?: string; introduction?: string } | null = null\n    let currentRule: { section: number; subsection: number; title: string; content: string[] } | null = null\n    let inCodeBlock = false\n    \n    for (let i = 0; i < lines.length; i++) {\n      const line = lines[i]\n      \n      // Check for section heading\n      const sectionInfo = parseSectionHeading(line)\n      if (sectionInfo) {\n        // Save previous section if exists\n        if (currentSection) {\n          const sectionFile = join(RULES_DIR, `section-${currentSection.number}.md`)\n          let sectionContent = `# ${currentSection.number}. ${currentSection.title}\\n\\n`\n          if (currentSection.impact) {\n            sectionContent += `**Impact: ${currentSection.impact}**\\n\\n`\n          }\n          if (currentSection.introduction) {\n            sectionContent += `## Introduction\\n\\n${currentSection.introduction}\\n`\n          }\n          await writeFile(sectionFile, sectionContent, 'utf-8')\n        }\n        \n        currentSection = sectionInfo\n        currentRule = null\n        \n        // Look for impact on next few lines\n        for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {\n          const impactInfo = extractImpact(lines[j])\n          if (impactInfo) {\n            currentSection.impact = impactInfo.impact\n            break\n          }\n        }\n        \n        // Collect introduction text until first rule\n        let introduction: string[] = []\n        for (let j = i + 1; j < lines.length; j++) {\n          if (parseRuleHeading(lines[j])) {\n            break\n          }\n          if (!lines[j].match(/^###/)) {\n            introduction.push(lines[j])\n          }\n        }\n        currentSection.introduction = introduction.join('\\n').trim()\n        continue\n      }\n      \n      // Check for rule heading\n      const ruleInfo = parseRuleHeading(line)\n      if (ruleInfo) {\n        // Save previous rule if exists\n        if (currentRule && currentSection) {\n          const ruleFile = join(RULES_DIR, `section-${currentRule.section}-rule-${currentRule.subsection}.md`)\n          const ruleContent = currentRule.content.join('\\n')\n          await writeFile(ruleFile, ruleContent, 'utf-8')\n          console.log(`Created ${ruleFile}`)\n        }\n        \n        currentRule = {\n          ...ruleInfo,\n          content: [line]\n        }\n        continue\n      }\n      \n      // Accumulate content for current rule\n      if (currentRule) {\n        currentRule.content.push(line)\n      }\n    }\n    \n    // Save last rule\n    if (currentRule && currentSection) {\n      const ruleFile = join(RULES_DIR, `section-${currentRule.section}-rule-${currentRule.subsection}.md`)\n      const ruleContent = currentRule.content.join('\\n')\n      await writeFile(ruleFile, ruleContent, 'utf-8')\n      console.log(`Created ${ruleFile}`)\n    }\n    \n    // Save last section\n    if (currentSection) {\n      const sectionFile = join(RULES_DIR, `section-${currentSection.number}.md`)\n      let sectionContent = `# ${currentSection.number}. ${currentSection.title}\\n\\n`\n      if (currentSection.impact) {\n        sectionContent += `**Impact: ${currentSection.impact}**\\n\\n`\n      }\n      if (currentSection.introduction) {\n        sectionContent += `## Introduction\\n\\n${currentSection.introduction}\\n`\n      }\n      await writeFile(sectionFile, sectionContent, 'utf-8')\n      console.log(`Created ${sectionFile}`)\n    }\n    \n    console.log('\\n✓ Migration complete!')\n    console.log('Note: You may need to manually add frontmatter to rule files.')\n  } catch (error) {\n    console.error('Migration failed:', error)\n    process.exit(1)\n  }\n}\n\nmigrate()\n"
  },
  {
    "path": "packages/react-best-practices-build/src/parser.ts",
    "content": "/**\n * Parser for rule markdown files\n */\n\nimport { readFile } from 'fs/promises'\nimport { basename } from 'path'\nimport { Rule, ImpactLevel } from './types.js'\n\nexport interface RuleFile {\n  section: number\n  subsection?: number\n  rule: Rule\n}\n\n/**\n * Parse a rule markdown file into a Rule object\n */\nexport async function parseRuleFile(\n  filePath: string,\n  sectionMap?: Record<string, number>\n): Promise<RuleFile> {\n  const rawContent = await readFile(filePath, 'utf-8')\n  // Normalize Windows CRLF line endings to LF for consistent parsing\n  const content = rawContent.replace(/\\r\\n/g, '\\n')\n  const lines = content.split('\\n')\n\n  // Extract frontmatter if present\n  let frontmatter: Record<string, any> = {}\n  let contentStart = 0\n\n  if (content.startsWith('---')) {\n    const frontmatterEnd = content.indexOf('---', 3)\n    if (frontmatterEnd !== -1) {\n      const frontmatterText = content.slice(3, frontmatterEnd).trim()\n      frontmatterText.split('\\n').forEach((line) => {\n        const [key, ...valueParts] = line.split(':')\n        if (key && valueParts.length) {\n          const value = valueParts.join(':').trim()\n          frontmatter[key.trim()] = value.replace(/^[\"']|[\"']$/g, '')\n        }\n      })\n      contentStart = frontmatterEnd + 3\n    }\n  }\n\n  // Parse the rule content\n  const ruleContent = content.slice(contentStart).trim()\n  const ruleLines = ruleContent.split('\\n')\n\n  // Extract title (first # or ## heading)\n  let title = ''\n  let titleLine = 0\n  for (let i = 0; i < ruleLines.length; i++) {\n    if (ruleLines[i].startsWith('##')) {\n      title = ruleLines[i].replace(/^##+\\s*/, '').trim()\n      titleLine = i\n      break\n    }\n  }\n\n  // Extract impact\n  let impact: Rule['impact'] = 'MEDIUM'\n  let impactDescription = ''\n  let explanation = ''\n  let examples: Rule['examples'] = []\n  let references: string[] = []\n\n  // Parse content after title\n  let currentExample: {\n    label: string\n    description?: string\n    code: string\n    language?: string\n    additionalText?: string\n  } | null = null\n  let inCodeBlock = false\n  let codeBlockLanguage = 'typescript'\n  let codeBlockContent: string[] = []\n  let afterCodeBlock = false\n  let additionalText: string[] = []\n  let hasCodeBlockForCurrentExample = false\n\n  for (let i = titleLine + 1; i < ruleLines.length; i++) {\n    const line = ruleLines[i]\n\n    // Impact line\n    if (line.includes('**Impact:')) {\n      const match = line.match(\n        /\\*\\*Impact:\\s*(\\w+(?:-\\w+)?)\\s*(?:\\(([^)]+)\\))?/i\n      )\n      if (match) {\n        impact = match[1].toUpperCase().replace(/-/g, '-') as ImpactLevel\n        impactDescription = match[2] || ''\n      }\n      continue\n    }\n\n    // Code block start\n    if (line.startsWith('```')) {\n      if (inCodeBlock) {\n        // End of code block\n        if (currentExample) {\n          currentExample.code = codeBlockContent.join('\\n')\n          currentExample.language = codeBlockLanguage\n        }\n        codeBlockContent = []\n        inCodeBlock = false\n        afterCodeBlock = true\n      } else {\n        // Start of code block\n        inCodeBlock = true\n        hasCodeBlockForCurrentExample = true\n        codeBlockLanguage = line.slice(3).trim() || 'typescript'\n        codeBlockContent = []\n        afterCodeBlock = false\n      }\n      continue\n    }\n\n    if (inCodeBlock) {\n      codeBlockContent.push(line)\n      continue\n    }\n\n    // Example label (Incorrect, Correct, Example, Usage, Implementation, etc.)\n    // Match pattern: **Label:** or **Label (description):** at end of line\n    // This distinguishes example labels from inline bold text like \"**Trade-off:** some text\"\n    const labelMatch = line.match(/^\\*\\*([^:]+?):\\*?\\*?$/)\n    if (labelMatch) {\n      // Save previous example if it exists\n      if (currentExample) {\n        if (additionalText.length > 0) {\n          currentExample.additionalText = additionalText.join('\\n\\n')\n          additionalText = []\n        }\n        examples.push(currentExample)\n      }\n      afterCodeBlock = false\n      hasCodeBlockForCurrentExample = false\n\n      const fullLabel = labelMatch[1].trim()\n      // Try to extract description from parentheses if present (handles simple cases)\n      // For nested parentheses like \"Incorrect (O(n) per lookup)\", we keep the full label\n      const descMatch = fullLabel.match(\n        /^([A-Za-z]+(?:\\s+[A-Za-z]+)*)\\s*\\(([^()]+)\\)$/\n      )\n      currentExample = {\n        label: descMatch ? descMatch[1].trim() : fullLabel,\n        description: descMatch ? descMatch[2].trim() : undefined,\n        code: '',\n        language: codeBlockLanguage,\n      }\n      continue\n    }\n\n    // Reference links\n    if (line.startsWith('Reference:') || line.startsWith('References:')) {\n      // Save current example before processing references\n      if (currentExample) {\n        if (additionalText.length > 0) {\n          currentExample.additionalText = additionalText.join('\\n\\n')\n          additionalText = []\n        }\n        examples.push(currentExample)\n        currentExample = null\n      }\n\n      const refMatch = line.match(/\\[([^\\]]+)\\]\\(([^)]+)\\)/g)\n      if (refMatch) {\n        references.push(\n          ...refMatch.map((ref) => {\n            const m = ref.match(/\\[([^\\]]+)\\]\\(([^)]+)\\)/)\n            return m ? m[2] : ref\n          })\n        )\n      }\n      continue\n    }\n\n    // Regular text (explanation or additional context after examples)\n    if (line.trim() && !line.startsWith('#')) {\n      if (!currentExample && !inCodeBlock) {\n        // Main explanation before any examples\n        explanation += (explanation ? '\\n\\n' : '') + line\n      } else if (\n        currentExample &&\n        (afterCodeBlock || !hasCodeBlockForCurrentExample)\n      ) {\n        // Text after a code block, or text in a section without a code block\n        // (e.g., \"When NOT to use this pattern:\" with bullet points instead of code)\n        additionalText.push(line)\n      }\n    }\n  }\n\n  // Handle last example if still open\n  if (currentExample) {\n    if (additionalText.length > 0) {\n      currentExample.additionalText = additionalText.join('\\n\\n')\n    }\n    examples.push(currentExample)\n  }\n\n  // Infer section from filename patterns\n  // Pattern: area-description.md where area determines section\n  const filename = basename(filePath)\n\n  // Default section map (for backwards compatibility)\n  const defaultSectionMap: Record<string, number> = {\n    async: 1,\n    bundle: 2,\n    server: 3,\n    client: 4,\n    rerender: 5,\n    rendering: 6,\n    js: 7,\n    advanced: 8,\n  }\n\n  const effectiveSectionMap = sectionMap || defaultSectionMap\n\n  // Extract area from filename - try longest prefix match first\n  // This handles prefixes like \"list-performance\" vs \"list\"\n  const filenameParts = filename.replace('.md', '').split('-')\n  let section = 0\n\n  // Try progressively shorter prefixes to find the best match\n  for (let len = filenameParts.length; len > 0; len--) {\n    const prefix = filenameParts.slice(0, len).join('-')\n    if (effectiveSectionMap[prefix] !== undefined) {\n      section = effectiveSectionMap[prefix]\n      break\n    }\n  }\n\n  // Fall back to frontmatter section if specified\n  section = frontmatter.section || section || 0\n\n  const rule: Rule = {\n    id: '', // Will be assigned by build script based on sorted order\n    title: frontmatter.title || title,\n    section: section,\n    subsection: undefined,\n    impact: frontmatter.impact || impact,\n    impactDescription: frontmatter.impactDescription || impactDescription,\n    explanation: frontmatter.explanation || explanation.trim(),\n    examples,\n    references: frontmatter.references\n      ? frontmatter.references.split(',').map((r: string) => r.trim())\n      : references,\n    tags: frontmatter.tags\n      ? frontmatter.tags.split(',').map((t: string) => t.trim())\n      : undefined,\n  }\n\n  return {\n    section,\n    subsection: 0,\n    rule,\n  }\n}\n"
  },
  {
    "path": "packages/react-best-practices-build/src/types.ts",
    "content": "/**\n * Type definitions for React Performance Guidelines rules\n */\n\nexport type ImpactLevel = 'CRITICAL' | 'HIGH' | 'MEDIUM-HIGH' | 'MEDIUM' | 'LOW-MEDIUM' | 'LOW'\n\nexport interface CodeExample {\n  label: string // e.g., \"Incorrect\", \"Correct\", \"Example\"\n  description?: string // Optional description before code\n  code: string\n  language?: string // Default: 'typescript' or 'tsx'\n  additionalText?: string // Optional text after code block (explanations, reasons)\n}\n\nexport interface Rule {\n  id: string // e.g., \"1.1\", \"2.3\"\n  title: string\n  section: number // Main section number (1-8)\n  subsection?: number // Subsection number within section\n  impact: ImpactLevel\n  impactDescription?: string // e.g., \"2-10× improvement\"\n  explanation: string\n  examples: CodeExample[]\n  references?: string[] // URLs or citations\n  tags?: string[] // For categorization/search\n}\n\nexport interface Section {\n  number: number\n  title: string\n  impact: ImpactLevel\n  impactDescription?: string\n  introduction?: string\n  rules: Rule[]\n}\n\nexport interface GuidelinesDocument {\n  version: string\n  organization: string\n  date: string\n  abstract: string\n  sections: Section[]\n  references?: string[]\n}\n\nexport interface TestCase {\n  ruleId: string\n  ruleTitle: string\n  type: 'bad' | 'good'\n  code: string\n  language: string\n  description?: string\n}\n"
  },
  {
    "path": "packages/react-best-practices-build/src/validate.ts",
    "content": "#!/usr/bin/env node\n/**\n * Validate rule files follow the correct structure\n */\n\nimport { readdir } from 'fs/promises'\nimport { join } from 'path'\nimport { Rule } from './types.js'\nimport { parseRuleFile } from './parser.js'\nimport { RULES_DIR } from './config.js'\n\ninterface ValidationError {\n  file: string\n  ruleId?: string\n  message: string\n}\n\n/**\n * Validate a rule\n */\nfunction validateRule(rule: Rule, file: string): ValidationError[] {\n  const errors: ValidationError[] = []\n  \n  // Note: rule.id is auto-generated during build, not required in source files\n  \n  if (!rule.title || rule.title.trim().length === 0) {\n    errors.push({ file, ruleId: rule.id, message: 'Missing or empty title' })\n  }\n  \n  if (!rule.explanation || rule.explanation.trim().length === 0) {\n    errors.push({ file, ruleId: rule.id, message: 'Missing or empty explanation' })\n  }\n  \n  if (!rule.examples || rule.examples.length === 0) {\n    errors.push({ file, ruleId: rule.id, message: 'Missing examples (need at least one bad and one good example)' })\n  } else {\n    // Filter out informational examples (notes, trade-offs, etc.) that don't have code\n    const codeExamples = rule.examples.filter(e => e.code && e.code.trim().length > 0)\n    \n    const hasBad = codeExamples.some(e => \n      e.label.toLowerCase().includes('incorrect') || \n      e.label.toLowerCase().includes('wrong') ||\n      e.label.toLowerCase().includes('bad')\n    )\n    const hasGood = codeExamples.some(e => \n      e.label.toLowerCase().includes('correct') || \n      e.label.toLowerCase().includes('good') ||\n      e.label.toLowerCase().includes('usage') ||\n      e.label.toLowerCase().includes('implementation') ||\n      e.label.toLowerCase().includes('example')\n    )\n    \n    if (codeExamples.length === 0) {\n      errors.push({ file, ruleId: rule.id, message: 'Missing code examples' })\n    } else if (!hasBad && !hasGood) {\n      errors.push({ file, ruleId: rule.id, message: 'Missing bad/incorrect or good/correct examples' })\n    }\n  }\n  \n  const validImpacts: Rule['impact'][] = ['CRITICAL', 'HIGH', 'MEDIUM-HIGH', 'MEDIUM', 'LOW-MEDIUM', 'LOW']\n  if (!validImpacts.includes(rule.impact)) {\n    errors.push({ file, ruleId: rule.id, message: `Invalid impact level: ${rule.impact}. Must be one of: ${validImpacts.join(', ')}` })\n  }\n  \n  return errors\n}\n\n/**\n * Main validation function\n */\nasync function validate() {\n  try {\n    console.log('Validating rule files...')\n    console.log(`Rules directory: ${RULES_DIR}`)\n    \n    const files = await readdir(RULES_DIR)\n    const ruleFiles = files.filter(f => f.endsWith('.md') && !f.startsWith('_'))\n    \n    const allErrors: ValidationError[] = []\n    \n    for (const file of ruleFiles) {\n      const filePath = join(RULES_DIR, file)\n      try {\n        const { rule } = await parseRuleFile(filePath)\n        const errors = validateRule(rule, file)\n        allErrors.push(...errors)\n      } catch (error) {\n        allErrors.push({ \n          file, \n          message: `Failed to parse: ${error instanceof Error ? error.message : String(error)}` \n        })\n      }\n    }\n    \n    if (allErrors.length > 0) {\n      console.error('\\n✗ Validation failed:\\n')\n      allErrors.forEach(error => {\n        console.error(`  ${error.file}${error.ruleId ? ` (${error.ruleId})` : ''}: ${error.message}`)\n      })\n      process.exit(1)\n    } else {\n      console.log(`✓ All ${ruleFiles.length} rule files are valid`)\n    }\n  } catch (error) {\n    console.error('Validation failed:', error)\n    process.exit(1)\n  }\n}\n\nvalidate()\n"
  },
  {
    "path": "packages/react-best-practices-build/test-cases.json",
    "content": "[\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Store Event Handlers in Refs\",\n    \"type\": \"bad\",\n    \"code\": \"function useWindowEvent(event: string, handler: (e) => void) {\\n  useEffect(() => {\\n    window.addEventListener(event, handler)\\n    return () => window.removeEventListener(event, handler)\\n  }, [event, handler])\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"re-subscribes on every render\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Store Event Handlers in Refs\",\n    \"type\": \"good\",\n    \"code\": \"import { useEffectEvent } from 'react'\\n\\nfunction useWindowEvent(event: string, handler: (e) => void) {\\n  const onEvent = useEffectEvent(handler)\\n\\n  useEffect(() => {\\n    window.addEventListener(event, onEvent)\\n    return () => window.removeEventListener(event, onEvent)\\n  }, [event])\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"stable subscription\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Initialize App Once, Not Per Mount\",\n    \"type\": \"bad\",\n    \"code\": \"function Comp() {\\n  useEffect(() => {\\n    loadFromStorage()\\n    checkAuthToken()\\n  }, [])\\n\\n  // ...\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"runs twice in dev, re-runs on remount\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Initialize App Once, Not Per Mount\",\n    \"type\": \"good\",\n    \"code\": \"let didInit = false\\n\\nfunction Comp() {\\n  useEffect(() => {\\n    if (didInit) return\\n    didInit = true\\n    loadFromStorage()\\n    checkAuthToken()\\n  }, [])\\n\\n  // ...\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"once per app load\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"useEffectEvent for Stable Callback Refs\",\n    \"type\": \"bad\",\n    \"code\": \"function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {\\n  const [query, setQuery] = useState('')\\n\\n  useEffect(() => {\\n    const timeout = setTimeout(() => onSearch(query), 300)\\n    return () => clearTimeout(timeout)\\n  }, [query, onSearch])\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"effect re-runs on every callback change\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"useEffectEvent for Stable Callback Refs\",\n    \"type\": \"good\",\n    \"code\": \"import { useEffectEvent } from 'react';\\n\\nfunction SearchInput({ onSearch }: { onSearch: (q: string) => void }) {\\n  const [query, setQuery] = useState('')\\n  const onSearchEvent = useEffectEvent(onSearch)\\n\\n  useEffect(() => {\\n    const timeout = setTimeout(() => onSearchEvent(query), 300)\\n    return () => clearTimeout(timeout)\\n  }, [query])\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"using React's useEffectEvent\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Prevent Waterfall Chains in API Routes\",\n    \"type\": \"bad\",\n    \"code\": \"export async function GET(request: Request) {\\n  const session = await auth()\\n  const config = await fetchConfig()\\n  const data = await fetchData(session.user.id)\\n  return Response.json({ data, config })\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"config waits for auth, data waits for both\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Prevent Waterfall Chains in API Routes\",\n    \"type\": \"good\",\n    \"code\": \"export async function GET(request: Request) {\\n  const sessionPromise = auth()\\n  const configPromise = fetchConfig()\\n  const session = await sessionPromise\\n  const [config, data] = await Promise.all([\\n    configPromise,\\n    fetchData(session.user.id)\\n  ])\\n  return Response.json({ data, config })\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"auth and config start immediately\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Defer Await Until Needed\",\n    \"type\": \"bad\",\n    \"code\": \"async function handleRequest(userId: string, skipProcessing: boolean) {\\n  const userData = await fetchUserData(userId)\\n  \\n  if (skipProcessing) {\\n    // Returns immediately but still waited for userData\\n    return { skipped: true }\\n  }\\n  \\n  // Only this branch uses userData\\n  return processUserData(userData)\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"blocks both branches\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Defer Await Until Needed\",\n    \"type\": \"good\",\n    \"code\": \"async function handleRequest(userId: string, skipProcessing: boolean) {\\n  if (skipProcessing) {\\n    // Returns immediately without waiting\\n    return { skipped: true }\\n  }\\n  \\n  // Fetch only when needed\\n  const userData = await fetchUserData(userId)\\n  return processUserData(userData)\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"only blocks when needed\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Dependency-Based Parallelization\",\n    \"type\": \"bad\",\n    \"code\": \"const [user, config] = await Promise.all([\\n  fetchUser(),\\n  fetchConfig()\\n])\\nconst profile = await fetchProfile(user.id)\",\n    \"language\": \"typescript\",\n    \"description\": \"profile waits for config unnecessarily\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Dependency-Based Parallelization\",\n    \"type\": \"good\",\n    \"code\": \"import { all } from 'better-all'\\n\\nconst { user, config, profile } = await all({\\n  async user() { return fetchUser() },\\n  async config() { return fetchConfig() },\\n  async profile() {\\n    return fetchProfile((await this.$.user).id)\\n  }\\n})\",\n    \"language\": \"typescript\",\n    \"description\": \"config and profile run in parallel\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Promise.all() for Independent Operations\",\n    \"type\": \"bad\",\n    \"code\": \"const user = await fetchUser()\\nconst posts = await fetchPosts()\\nconst comments = await fetchComments()\",\n    \"language\": \"typescript\",\n    \"description\": \"sequential execution, 3 round trips\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Promise.all() for Independent Operations\",\n    \"type\": \"good\",\n    \"code\": \"const [user, posts, comments] = await Promise.all([\\n  fetchUser(),\\n  fetchPosts(),\\n  fetchComments()\\n])\",\n    \"language\": \"typescript\",\n    \"description\": \"parallel execution, 1 round trip\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Strategic Suspense Boundaries\",\n    \"type\": \"bad\",\n    \"code\": \"async function Page() {\\n  const data = await fetchData() // Blocks entire page\\n  \\n  return (\\n    <div>\\n      <div>Sidebar</div>\\n      <div>Header</div>\\n      <div>\\n        <DataDisplay data={data} />\\n      </div>\\n      <div>Footer</div>\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"wrapper blocked by data fetching\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Strategic Suspense Boundaries\",\n    \"type\": \"good\",\n    \"code\": \"function Page() {\\n  return (\\n    <div>\\n      <div>Sidebar</div>\\n      <div>Header</div>\\n      <div>\\n        <Suspense fallback={<Skeleton />}>\\n          <DataDisplay />\\n        </Suspense>\\n      </div>\\n      <div>Footer</div>\\n    </div>\\n  )\\n}\\n\\nasync function DataDisplay() {\\n  const data = await fetchData() // Only blocks this component\\n  return <div>{data.content}</div>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"wrapper shows immediately, data streams in\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Avoid Barrel File Imports\",\n    \"type\": \"bad\",\n    \"code\": \"import { Check, X, Menu } from 'lucide-react'\\n// Loads 1,583 modules, takes ~2.8s extra in dev\\n// Runtime cost: 200-800ms on every cold start\\n\\nimport { Button, TextField } from '@mui/material'\\n// Loads 2,225 modules, takes ~4.2s extra in dev\",\n    \"language\": \"tsx\",\n    \"description\": \"imports entire library\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Avoid Barrel File Imports\",\n    \"type\": \"good\",\n    \"code\": \"import Check from 'lucide-react/dist/esm/icons/check'\\nimport X from 'lucide-react/dist/esm/icons/x'\\nimport Menu from 'lucide-react/dist/esm/icons/menu'\\n// Loads only 3 modules (~2KB vs ~1MB)\\n\\nimport Button from '@mui/material/Button'\\nimport TextField from '@mui/material/TextField'\\n// Loads only what you use\",\n    \"language\": \"tsx\",\n    \"description\": \"imports only what you need\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Defer Non-Critical Third-Party Libraries\",\n    \"type\": \"bad\",\n    \"code\": \"import { Analytics } from '@vercel/analytics/react'\\n\\nexport default function RootLayout({ children }) {\\n  return (\\n    <html>\\n      <body>\\n        {children}\\n        <Analytics />\\n      </body>\\n    </html>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"blocks initial bundle\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Defer Non-Critical Third-Party Libraries\",\n    \"type\": \"good\",\n    \"code\": \"import dynamic from 'next/dynamic'\\n\\nconst Analytics = dynamic(\\n  () => import('@vercel/analytics/react').then(m => m.Analytics),\\n  { ssr: false }\\n)\\n\\nexport default function RootLayout({ children }) {\\n  return (\\n    <html>\\n      <body>\\n        {children}\\n        <Analytics />\\n      </body>\\n    </html>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"loads after hydration\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Dynamic Imports for Heavy Components\",\n    \"type\": \"bad\",\n    \"code\": \"import { MonacoEditor } from './monaco-editor'\\n\\nfunction CodePanel({ code }: { code: string }) {\\n  return <MonacoEditor value={code} />\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"Monaco bundles with main chunk ~300KB\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Dynamic Imports for Heavy Components\",\n    \"type\": \"good\",\n    \"code\": \"import dynamic from 'next/dynamic'\\n\\nconst MonacoEditor = dynamic(\\n  () => import('./monaco-editor').then(m => m.MonacoEditor),\\n  { ssr: false }\\n)\\n\\nfunction CodePanel({ code }: { code: string }) {\\n  return <MonacoEditor value={code} />\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"Monaco loads on demand\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Deduplicate Global Event Listeners\",\n    \"type\": \"bad\",\n    \"code\": \"function useKeyboardShortcut(key: string, callback: () => void) {\\n  useEffect(() => {\\n    const handler = (e: KeyboardEvent) => {\\n      if (e.metaKey && e.key === key) {\\n        callback()\\n      }\\n    }\\n    window.addEventListener('keydown', handler)\\n    return () => window.removeEventListener('keydown', handler)\\n  }, [key, callback])\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"N instances = N listeners\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Deduplicate Global Event Listeners\",\n    \"type\": \"good\",\n    \"code\": \"import useSWRSubscription from 'swr/subscription'\\n\\n// Module-level Map to track callbacks per key\\nconst keyCallbacks = new Map<string, Set<() => void>>()\\n\\nfunction useKeyboardShortcut(key: string, callback: () => void) {\\n  // Register this callback in the Map\\n  useEffect(() => {\\n    if (!keyCallbacks.has(key)) {\\n      keyCallbacks.set(key, new Set())\\n    }\\n    keyCallbacks.get(key)!.add(callback)\\n\\n    return () => {\\n      const set = keyCallbacks.get(key)\\n      if (set) {\\n        set.delete(callback)\\n        if (set.size === 0) {\\n          keyCallbacks.delete(key)\\n        }\\n      }\\n    }\\n  }, [key, callback])\\n\\n  useSWRSubscription('global-keydown', () => {\\n    const handler = (e: KeyboardEvent) => {\\n      if (e.metaKey && keyCallbacks.has(e.key)) {\\n        keyCallbacks.get(e.key)!.forEach(cb => cb())\\n      }\\n    }\\n    window.addEventListener('keydown', handler)\\n    return () => window.removeEventListener('keydown', handler)\\n  })\\n}\\n\\nfunction Profile() {\\n  // Multiple shortcuts will share the same listener\\n  useKeyboardShortcut('p', () => { /* ... */ }) \\n  useKeyboardShortcut('k', () => { /* ... */ })\\n  // ...\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"N instances = 1 listener\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Version and Minimize localStorage Data\",\n    \"type\": \"bad\",\n    \"code\": \"// No version, stores everything, no error handling\\nlocalStorage.setItem('userConfig', JSON.stringify(fullUserObject))\\nconst data = localStorage.getItem('userConfig')\",\n    \"language\": \"typescript\",\n    \"description\": \"Incorrect example for Version and Minimize localStorage Data\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Version and Minimize localStorage Data\",\n    \"type\": \"good\",\n    \"code\": \"const VERSION = 'v2'\\n\\nfunction saveConfig(config: { theme: string; language: string }) {\\n  try {\\n    localStorage.setItem(`userConfig:${VERSION}`, JSON.stringify(config))\\n  } catch {\\n    // Throws in incognito/private browsing, quota exceeded, or disabled\\n  }\\n}\\n\\nfunction loadConfig() {\\n  try {\\n    const data = localStorage.getItem(`userConfig:${VERSION}`)\\n    return data ? JSON.parse(data) : null\\n  } catch {\\n    return null\\n  }\\n}\\n\\n// Migration from v1 to v2\\nfunction migrate() {\\n  try {\\n    const v1 = localStorage.getItem('userConfig:v1')\\n    if (v1) {\\n      const old = JSON.parse(v1)\\n      saveConfig({ theme: old.darkMode ? 'dark' : 'light', language: old.lang })\\n      localStorage.removeItem('userConfig:v1')\\n    }\\n  } catch {}\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"Correct example for Version and Minimize localStorage Data\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Passive Event Listeners for Scrolling Performance\",\n    \"type\": \"bad\",\n    \"code\": \"useEffect(() => {\\n  const handleTouch = (e: TouchEvent) => console.log(e.touches[0].clientX)\\n  const handleWheel = (e: WheelEvent) => console.log(e.deltaY)\\n  \\n  document.addEventListener('touchstart', handleTouch)\\n  document.addEventListener('wheel', handleWheel)\\n  \\n  return () => {\\n    document.removeEventListener('touchstart', handleTouch)\\n    document.removeEventListener('wheel', handleWheel)\\n  }\\n}, [])\",\n    \"language\": \"typescript\",\n    \"description\": \"Incorrect example for Use Passive Event Listeners for Scrolling Performance\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Passive Event Listeners for Scrolling Performance\",\n    \"type\": \"good\",\n    \"code\": \"useEffect(() => {\\n  const handleTouch = (e: TouchEvent) => console.log(e.touches[0].clientX)\\n  const handleWheel = (e: WheelEvent) => console.log(e.deltaY)\\n  \\n  document.addEventListener('touchstart', handleTouch, { passive: true })\\n  document.addEventListener('wheel', handleWheel, { passive: true })\\n  \\n  return () => {\\n    document.removeEventListener('touchstart', handleTouch)\\n    document.removeEventListener('wheel', handleWheel)\\n  }\\n}, [])\",\n    \"language\": \"typescript\",\n    \"description\": \"Correct example for Use Passive Event Listeners for Scrolling Performance\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use SWR for Automatic Deduplication\",\n    \"type\": \"bad\",\n    \"code\": \"function UserList() {\\n  const [users, setUsers] = useState([])\\n  useEffect(() => {\\n    fetch('/api/users')\\n      .then(r => r.json())\\n      .then(setUsers)\\n  }, [])\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"no deduplication, each instance fetches\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use SWR for Automatic Deduplication\",\n    \"type\": \"good\",\n    \"code\": \"import useSWR from 'swr'\\n\\nfunction UserList() {\\n  const { data: users } = useSWR('/api/users', fetcher)\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"multiple instances share one request\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Avoid Layout Thrashing\",\n    \"type\": \"bad\",\n    \"code\": \"function layoutThrashing(element: HTMLElement) {\\n  element.style.width = '100px'\\n  const width = element.offsetWidth  // Forces reflow\\n  element.style.height = '200px'\\n  const height = element.offsetHeight  // Forces another reflow\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"interleaved reads and writes force reflows\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Avoid Layout Thrashing\",\n    \"type\": \"good\",\n    \"code\": \"function updateElementStyles(element: HTMLElement) {\\n  // Batch all writes together\\n  element.style.width = '100px'\\n  element.style.height = '200px'\\n  element.style.backgroundColor = 'blue'\\n  element.style.border = '1px solid black'\\n  \\n  // Read after all writes are done (single reflow)\\n  const { width, height } = element.getBoundingClientRect()\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"batch writes, then read once\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Avoid Layout Thrashing\",\n    \"type\": \"good\",\n    \"code\": \"function updateElementStyles(element: HTMLElement) {\\n  element.classList.add('highlighted-box')\\n  \\n  const { width, height } = element.getBoundingClientRect()\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"batch reads, then writes\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Cache Repeated Function Calls\",\n    \"type\": \"bad\",\n    \"code\": \"function ProjectList({ projects }: { projects: Project[] }) {\\n  return (\\n    <div>\\n      {projects.map(project => {\\n        // slugify() called 100+ times for same project names\\n        const slug = slugify(project.name)\\n        \\n        return <ProjectCard key={project.id} slug={slug} />\\n      })}\\n    </div>\\n  )\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"redundant computation\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Cache Repeated Function Calls\",\n    \"type\": \"good\",\n    \"code\": \"// Module-level cache\\nconst slugifyCache = new Map<string, string>()\\n\\nfunction cachedSlugify(text: string): string {\\n  if (slugifyCache.has(text)) {\\n    return slugifyCache.get(text)!\\n  }\\n  const result = slugify(text)\\n  slugifyCache.set(text, result)\\n  return result\\n}\\n\\nfunction ProjectList({ projects }: { projects: Project[] }) {\\n  return (\\n    <div>\\n      {projects.map(project => {\\n        // Computed only once per unique project name\\n        const slug = cachedSlugify(project.name)\\n        \\n        return <ProjectCard key={project.id} slug={slug} />\\n      })}\\n    </div>\\n  )\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"cached results\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Cache Property Access in Loops\",\n    \"type\": \"bad\",\n    \"code\": \"for (let i = 0; i < arr.length; i++) {\\n  process(obj.config.settings.value)\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"3 lookups × N iterations\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Cache Property Access in Loops\",\n    \"type\": \"good\",\n    \"code\": \"const value = obj.config.settings.value\\nconst len = arr.length\\nfor (let i = 0; i < len; i++) {\\n  process(value)\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"1 lookup total\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Cache Storage API Calls\",\n    \"type\": \"bad\",\n    \"code\": \"function getTheme() {\\n  return localStorage.getItem('theme') ?? 'light'\\n}\\n// Called 10 times = 10 storage reads\",\n    \"language\": \"typescript\",\n    \"description\": \"reads storage on every call\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Cache Storage API Calls\",\n    \"type\": \"good\",\n    \"code\": \"const storageCache = new Map<string, string | null>()\\n\\nfunction getLocalStorage(key: string) {\\n  if (!storageCache.has(key)) {\\n    storageCache.set(key, localStorage.getItem(key))\\n  }\\n  return storageCache.get(key)\\n}\\n\\nfunction setLocalStorage(key: string, value: string) {\\n  localStorage.setItem(key, value)\\n  storageCache.set(key, value)  // keep cache in sync\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"Map cache\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Combine Multiple Array Iterations\",\n    \"type\": \"bad\",\n    \"code\": \"const admins = users.filter(u => u.isAdmin)\\nconst testers = users.filter(u => u.isTester)\\nconst inactive = users.filter(u => !u.isActive)\",\n    \"language\": \"typescript\",\n    \"description\": \"3 iterations\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Combine Multiple Array Iterations\",\n    \"type\": \"good\",\n    \"code\": \"const admins: User[] = []\\nconst testers: User[] = []\\nconst inactive: User[] = []\\n\\nfor (const user of users) {\\n  if (user.isAdmin) admins.push(user)\\n  if (user.isTester) testers.push(user)\\n  if (!user.isActive) inactive.push(user)\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"1 iteration\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Early Return from Functions\",\n    \"type\": \"bad\",\n    \"code\": \"function validateUsers(users: User[]) {\\n  let hasError = false\\n  let errorMessage = ''\\n  \\n  for (const user of users) {\\n    if (!user.email) {\\n      hasError = true\\n      errorMessage = 'Email required'\\n    }\\n    if (!user.name) {\\n      hasError = true\\n      errorMessage = 'Name required'\\n    }\\n    // Continues checking all users even after error found\\n  }\\n  \\n  return hasError ? { valid: false, error: errorMessage } : { valid: true }\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"processes all items even after finding answer\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Early Return from Functions\",\n    \"type\": \"good\",\n    \"code\": \"function validateUsers(users: User[]) {\\n  for (const user of users) {\\n    if (!user.email) {\\n      return { valid: false, error: 'Email required' }\\n    }\\n    if (!user.name) {\\n      return { valid: false, error: 'Name required' }\\n    }\\n  }\\n\\n  return { valid: true }\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"returns immediately on first error\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use flatMap to Map and Filter in One Pass\",\n    \"type\": \"bad\",\n    \"code\": \"const userNames = users\\n  .map(user => user.isActive ? user.name : null)\\n  .filter(Boolean)\",\n    \"language\": \"typescript\",\n    \"description\": \"2 iterations, intermediate array\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use flatMap to Map and Filter in One Pass\",\n    \"type\": \"good\",\n    \"code\": \"const userNames = users.flatMap(user =>\\n  user.isActive ? [user.name] : []\\n)\",\n    \"language\": \"typescript\",\n    \"description\": \"1 iteration, no intermediate array\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Hoist RegExp Creation\",\n    \"type\": \"bad\",\n    \"code\": \"function Highlighter({ text, query }: Props) {\\n  const regex = new RegExp(`(${query})`, 'gi')\\n  const parts = text.split(regex)\\n  return <>{parts.map((part, i) => ...)}</>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"new RegExp every render\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Hoist RegExp Creation\",\n    \"type\": \"good\",\n    \"code\": \"const EMAIL_REGEX = /^[^\\\\s@]+@[^\\\\s@]+\\\\.[^\\\\s@]+$/\\n\\nfunction Highlighter({ text, query }: Props) {\\n  const regex = useMemo(\\n    () => new RegExp(`(${escapeRegex(query)})`, 'gi'),\\n    [query]\\n  )\\n  const parts = text.split(regex)\\n  return <>{parts.map((part, i) => ...)}</>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"memoize or hoist\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Build Index Maps for Repeated Lookups\",\n    \"type\": \"bad\",\n    \"code\": \"function processOrders(orders: Order[], users: User[]) {\\n  return orders.map(order => ({\\n    ...order,\\n    user: users.find(u => u.id === order.userId)\\n  }))\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"Incorrect (O(n) per lookup) example for Build Index Maps for Repeated Lookups\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Build Index Maps for Repeated Lookups\",\n    \"type\": \"good\",\n    \"code\": \"function processOrders(orders: Order[], users: User[]) {\\n  const userById = new Map(users.map(u => [u.id, u]))\\n\\n  return orders.map(order => ({\\n    ...order,\\n    user: userById.get(order.userId)\\n  }))\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"Correct (O(1) per lookup) example for Build Index Maps for Repeated Lookups\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Early Length Check for Array Comparisons\",\n    \"type\": \"bad\",\n    \"code\": \"function hasChanges(current: string[], original: string[]) {\\n  // Always sorts and joins, even when lengths differ\\n  return current.sort().join() !== original.sort().join()\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"always runs expensive comparison\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Early Length Check for Array Comparisons\",\n    \"type\": \"good\",\n    \"code\": \"function hasChanges(current: string[], original: string[]) {\\n  // Early return if lengths differ\\n  if (current.length !== original.length) {\\n    return true\\n  }\\n  // Only sort when lengths match\\n  const currentSorted = current.toSorted()\\n  const originalSorted = original.toSorted()\\n  for (let i = 0; i < currentSorted.length; i++) {\\n    if (currentSorted[i] !== originalSorted[i]) {\\n      return true\\n    }\\n  }\\n  return false\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"Correct (O(1) length check first) example for Early Length Check for Array Comparisons\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Loop for Min/Max Instead of Sort\",\n    \"type\": \"bad\",\n    \"code\": \"interface Project {\\n  id: string\\n  name: string\\n  updatedAt: number\\n}\\n\\nfunction getLatestProject(projects: Project[]) {\\n  const sorted = [...projects].sort((a, b) => b.updatedAt - a.updatedAt)\\n  return sorted[0]\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"Incorrect (O(n log n) - sort to find latest) example for Use Loop for Min/Max Instead of Sort\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Loop for Min/Max Instead of Sort\",\n    \"type\": \"bad\",\n    \"code\": \"function getOldestAndNewest(projects: Project[]) {\\n  const sorted = [...projects].sort((a, b) => a.updatedAt - b.updatedAt)\\n  return { oldest: sorted[0], newest: sorted[sorted.length - 1] }\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"Incorrect (O(n log n) - sort for oldest and newest) example for Use Loop for Min/Max Instead of Sort\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Loop for Min/Max Instead of Sort\",\n    \"type\": \"good\",\n    \"code\": \"function getLatestProject(projects: Project[]) {\\n  if (projects.length === 0) return null\\n  \\n  let latest = projects[0]\\n  \\n  for (let i = 1; i < projects.length; i++) {\\n    if (projects[i].updatedAt > latest.updatedAt) {\\n      latest = projects[i]\\n    }\\n  }\\n  \\n  return latest\\n}\\n\\nfunction getOldestAndNewest(projects: Project[]) {\\n  if (projects.length === 0) return { oldest: null, newest: null }\\n  \\n  let oldest = projects[0]\\n  let newest = projects[0]\\n  \\n  for (let i = 1; i < projects.length; i++) {\\n    if (projects[i].updatedAt < oldest.updatedAt) oldest = projects[i]\\n    if (projects[i].updatedAt > newest.updatedAt) newest = projects[i]\\n  }\\n  \\n  return { oldest, newest }\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"Correct (O(n) - single loop) example for Use Loop for Min/Max Instead of Sort\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Set/Map for O(1) Lookups\",\n    \"type\": \"bad\",\n    \"code\": \"const allowedIds = ['a', 'b', 'c', ...]\\nitems.filter(item => allowedIds.includes(item.id))\",\n    \"language\": \"typescript\",\n    \"description\": \"Incorrect (O(n) per check) example for Use Set/Map for O(1) Lookups\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Set/Map for O(1) Lookups\",\n    \"type\": \"good\",\n    \"code\": \"const allowedIds = new Set(['a', 'b', 'c', ...])\\nitems.filter(item => allowedIds.has(item.id))\",\n    \"language\": \"typescript\",\n    \"description\": \"Correct (O(1) per check) example for Use Set/Map for O(1) Lookups\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use toSorted() Instead of sort() for Immutability\",\n    \"type\": \"bad\",\n    \"code\": \"function UserList({ users }: { users: User[] }) {\\n  // Mutates the users prop array!\\n  const sorted = useMemo(\\n    () => users.sort((a, b) => a.name.localeCompare(b.name)),\\n    [users]\\n  )\\n  return <div>{sorted.map(renderUser)}</div>\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"mutates original array\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use toSorted() Instead of sort() for Immutability\",\n    \"type\": \"good\",\n    \"code\": \"function UserList({ users }: { users: User[] }) {\\n  // Creates new sorted array, original unchanged\\n  const sorted = useMemo(\\n    () => users.toSorted((a, b) => a.name.localeCompare(b.name)),\\n    [users]\\n  )\\n  return <div>{sorted.map(renderUser)}</div>\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"creates new array\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Animate SVG Wrapper Instead of SVG Element\",\n    \"type\": \"bad\",\n    \"code\": \"function LoadingSpinner() {\\n  return (\\n    <svg \\n      className=\\\"animate-spin\\\"\\n      width=\\\"24\\\" \\n      height=\\\"24\\\" \\n      viewBox=\\\"0 0 24 24\\\"\\n    >\\n      <circle cx=\\\"12\\\" cy=\\\"12\\\" r=\\\"10\\\" stroke=\\\"currentColor\\\" />\\n    </svg>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"animating SVG directly - no hardware acceleration\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Animate SVG Wrapper Instead of SVG Element\",\n    \"type\": \"good\",\n    \"code\": \"function LoadingSpinner() {\\n  return (\\n    <div className=\\\"animate-spin\\\">\\n      <svg \\n        width=\\\"24\\\" \\n        height=\\\"24\\\" \\n        viewBox=\\\"0 0 24 24\\\"\\n      >\\n        <circle cx=\\\"12\\\" cy=\\\"12\\\" r=\\\"10\\\" stroke=\\\"currentColor\\\" />\\n      </svg>\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"animating wrapper div - hardware accelerated\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Explicit Conditional Rendering\",\n    \"type\": \"bad\",\n    \"code\": \"function Badge({ count }: { count: number }) {\\n  return (\\n    <div>\\n      {count && <span className=\\\"badge\\\">{count}</span>}\\n    </div>\\n  )\\n}\\n\\n// When count = 0, renders: <div>0</div>\\n// When count = 5, renders: <div><span class=\\\"badge\\\">5</span></div>\",\n    \"language\": \"tsx\",\n    \"description\": \"renders \\\"0\\\" when count is 0\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Explicit Conditional Rendering\",\n    \"type\": \"good\",\n    \"code\": \"function Badge({ count }: { count: number }) {\\n  return (\\n    <div>\\n      {count > 0 ? <span className=\\\"badge\\\">{count}</span> : null}\\n    </div>\\n  )\\n}\\n\\n// When count = 0, renders: <div></div>\\n// When count = 5, renders: <div><span class=\\\"badge\\\">5</span></div>\",\n    \"language\": \"tsx\",\n    \"description\": \"renders nothing when count is 0\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Hoist Static JSX Elements\",\n    \"type\": \"bad\",\n    \"code\": \"function LoadingSkeleton() {\\n  return <div className=\\\"animate-pulse h-20 bg-gray-200\\\" />\\n}\\n\\nfunction Container() {\\n  return (\\n    <div>\\n      {loading && <LoadingSkeleton />}\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"recreates element every render\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Hoist Static JSX Elements\",\n    \"type\": \"good\",\n    \"code\": \"const loadingSkeleton = (\\n  <div className=\\\"animate-pulse h-20 bg-gray-200\\\" />\\n)\\n\\nfunction Container() {\\n  return (\\n    <div>\\n      {loading && loadingSkeleton}\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"reuses same element\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Prevent Hydration Mismatch Without Flickering\",\n    \"type\": \"bad\",\n    \"code\": \"function ThemeWrapper({ children }: { children: ReactNode }) {\\n  // localStorage is not available on server - throws error\\n  const theme = localStorage.getItem('theme') || 'light'\\n  \\n  return (\\n    <div className={theme}>\\n      {children}\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"breaks SSR\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Prevent Hydration Mismatch Without Flickering\",\n    \"type\": \"bad\",\n    \"code\": \"function ThemeWrapper({ children }: { children: ReactNode }) {\\n  const [theme, setTheme] = useState('light')\\n  \\n  useEffect(() => {\\n    // Runs after hydration - causes visible flash\\n    const stored = localStorage.getItem('theme')\\n    if (stored) {\\n      setTheme(stored)\\n    }\\n  }, [])\\n  \\n  return (\\n    <div className={theme}>\\n      {children}\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"visual flickering\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Prevent Hydration Mismatch Without Flickering\",\n    \"type\": \"good\",\n    \"code\": \"function ThemeWrapper({ children }: { children: ReactNode }) {\\n  return (\\n    <>\\n      <div id=\\\"theme-wrapper\\\">\\n        {children}\\n      </div>\\n      <script\\n        dangerouslySetInnerHTML={{\\n          __html: `\\n            (function() {\\n              try {\\n                var theme = localStorage.getItem('theme') || 'light';\\n                var el = document.getElementById('theme-wrapper');\\n                if (el) el.className = theme;\\n              } catch (e) {}\\n            })();\\n          `,\\n        }}\\n      />\\n    </>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"no flicker, no hydration mismatch\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Suppress Expected Hydration Mismatches\",\n    \"type\": \"bad\",\n    \"code\": \"function Timestamp() {\\n  return <span>{new Date().toLocaleString()}</span>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"known mismatch warnings\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Suppress Expected Hydration Mismatches\",\n    \"type\": \"good\",\n    \"code\": \"function Timestamp() {\\n  return (\\n    <span suppressHydrationWarning>\\n      {new Date().toLocaleString()}\\n    </span>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"suppress expected mismatch only\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use defer or async on Script Tags\",\n    \"type\": \"bad\",\n    \"code\": \"export default function Document() {\\n  return (\\n    <html>\\n      <head>\\n        <script src=\\\"https://example.com/analytics.js\\\" />\\n        <script src=\\\"/scripts/utils.js\\\" />\\n      </head>\\n      <body>{/* content */}</body>\\n    </html>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"blocks rendering\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use defer or async on Script Tags\",\n    \"type\": \"good\",\n    \"code\": \"import Script from 'next/script'\\n\\nexport default function Page() {\\n  return (\\n    <>\\n      <Script src=\\\"https://example.com/analytics.js\\\" strategy=\\\"afterInteractive\\\" />\\n      <Script src=\\\"/scripts/utils.js\\\" strategy=\\\"beforeInteractive\\\" />\\n    </>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"non-blocking\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Optimize SVG Precision\",\n    \"type\": \"bad\",\n    \"code\": \"<path d=\\\"M 10.293847 20.847362 L 30.938472 40.192837\\\" />\",\n    \"language\": \"svg\",\n    \"description\": \"excessive precision\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Optimize SVG Precision\",\n    \"type\": \"good\",\n    \"code\": \"<path d=\\\"M 10.3 20.8 L 30.9 40.2\\\" />\",\n    \"language\": \"svg\",\n    \"description\": \"1 decimal place\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use useTransition Over Manual Loading States\",\n    \"type\": \"bad\",\n    \"code\": \"function SearchResults() {\\n  const [query, setQuery] = useState('')\\n  const [results, setResults] = useState([])\\n  const [isLoading, setIsLoading] = useState(false)\\n\\n  const handleSearch = async (value: string) => {\\n    setIsLoading(true)\\n    setQuery(value)\\n    const data = await fetchResults(value)\\n    setResults(data)\\n    setIsLoading(false)\\n  }\\n\\n  return (\\n    <>\\n      <input onChange={(e) => handleSearch(e.target.value)} />\\n      {isLoading && <Spinner />}\\n      <ResultsList results={results} />\\n    </>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"manual loading state\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use useTransition Over Manual Loading States\",\n    \"type\": \"good\",\n    \"code\": \"import { useTransition, useState } from 'react'\\n\\nfunction SearchResults() {\\n  const [query, setQuery] = useState('')\\n  const [results, setResults] = useState([])\\n  const [isPending, startTransition] = useTransition()\\n\\n  const handleSearch = (value: string) => {\\n    setQuery(value) // Update input immediately\\n    \\n    startTransition(async () => {\\n      // Fetch and update results\\n      const data = await fetchResults(value)\\n      setResults(data)\\n    })\\n  }\\n\\n  return (\\n    <>\\n      <input onChange={(e) => handleSearch(e.target.value)} />\\n      {isPending && <Spinner />}\\n      <ResultsList results={results} />\\n    </>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"useTransition with built-in pending state\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Defer State Reads to Usage Point\",\n    \"type\": \"bad\",\n    \"code\": \"function ShareButton({ chatId }: { chatId: string }) {\\n  const searchParams = useSearchParams()\\n\\n  const handleShare = () => {\\n    const ref = searchParams.get('ref')\\n    shareChat(chatId, { ref })\\n  }\\n\\n  return <button onClick={handleShare}>Share</button>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"subscribes to all searchParams changes\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Defer State Reads to Usage Point\",\n    \"type\": \"good\",\n    \"code\": \"function ShareButton({ chatId }: { chatId: string }) {\\n  const handleShare = () => {\\n    const params = new URLSearchParams(window.location.search)\\n    const ref = params.get('ref')\\n    shareChat(chatId, { ref })\\n  }\\n\\n  return <button onClick={handleShare}>Share</button>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"reads on demand, no subscription\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Narrow Effect Dependencies\",\n    \"type\": \"bad\",\n    \"code\": \"useEffect(() => {\\n  console.log(user.id)\\n}, [user])\",\n    \"language\": \"tsx\",\n    \"description\": \"re-runs on any user field change\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Narrow Effect Dependencies\",\n    \"type\": \"good\",\n    \"code\": \"useEffect(() => {\\n  console.log(user.id)\\n}, [user.id])\",\n    \"language\": \"tsx\",\n    \"description\": \"re-runs only when id changes\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Calculate Derived State During Rendering\",\n    \"type\": \"bad\",\n    \"code\": \"function Form() {\\n  const [firstName, setFirstName] = useState('First')\\n  const [lastName, setLastName] = useState('Last')\\n  const [fullName, setFullName] = useState('')\\n\\n  useEffect(() => {\\n    setFullName(firstName + ' ' + lastName)\\n  }, [firstName, lastName])\\n\\n  return <p>{fullName}</p>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"redundant state and effect\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Calculate Derived State During Rendering\",\n    \"type\": \"good\",\n    \"code\": \"function Form() {\\n  const [firstName, setFirstName] = useState('First')\\n  const [lastName, setLastName] = useState('Last')\\n  const fullName = firstName + ' ' + lastName\\n\\n  return <p>{fullName}</p>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"derive during render\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Subscribe to Derived State\",\n    \"type\": \"bad\",\n    \"code\": \"function Sidebar() {\\n  const width = useWindowWidth()  // updates continuously\\n  const isMobile = width < 768\\n  return <nav className={isMobile ? 'mobile' : 'desktop'} />\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"re-renders on every pixel change\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Subscribe to Derived State\",\n    \"type\": \"good\",\n    \"code\": \"function Sidebar() {\\n  const isMobile = useMediaQuery('(max-width: 767px)')\\n  return <nav className={isMobile ? 'mobile' : 'desktop'} />\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"re-renders only when boolean changes\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Functional setState Updates\",\n    \"type\": \"bad\",\n    \"code\": \"function TodoList() {\\n  const [items, setItems] = useState(initialItems)\\n  \\n  // Callback must depend on items, recreated on every items change\\n  const addItems = useCallback((newItems: Item[]) => {\\n    setItems([...items, ...newItems])\\n  }, [items])  // ❌ items dependency causes recreations\\n  \\n  // Risk of stale closure if dependency is forgotten\\n  const removeItem = useCallback((id: string) => {\\n    setItems(items.filter(item => item.id !== id))\\n  }, [])  // ❌ Missing items dependency - will use stale items!\\n  \\n  return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"requires state as dependency\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Functional setState Updates\",\n    \"type\": \"good\",\n    \"code\": \"function TodoList() {\\n  const [items, setItems] = useState(initialItems)\\n  \\n  // Stable callback, never recreated\\n  const addItems = useCallback((newItems: Item[]) => {\\n    setItems(curr => [...curr, ...newItems])\\n  }, [])  // ✅ No dependencies needed\\n  \\n  // Always uses latest state, no stale closure risk\\n  const removeItem = useCallback((id: string) => {\\n    setItems(curr => curr.filter(item => item.id !== id))\\n  }, [])  // ✅ Safe and stable\\n  \\n  return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"stable callbacks, no stale closures\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Lazy State Initialization\",\n    \"type\": \"bad\",\n    \"code\": \"function FilteredList({ items }: { items: Item[] }) {\\n  // buildSearchIndex() runs on EVERY render, even after initialization\\n  const [searchIndex, setSearchIndex] = useState(buildSearchIndex(items))\\n  const [query, setQuery] = useState('')\\n  \\n  // When query changes, buildSearchIndex runs again unnecessarily\\n  return <SearchResults index={searchIndex} query={query} />\\n}\\n\\nfunction UserProfile() {\\n  // JSON.parse runs on every render\\n  const [settings, setSettings] = useState(\\n    JSON.parse(localStorage.getItem('settings') || '{}')\\n  )\\n  \\n  return <SettingsForm settings={settings} onChange={setSettings} />\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"runs on every render\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Lazy State Initialization\",\n    \"type\": \"good\",\n    \"code\": \"function FilteredList({ items }: { items: Item[] }) {\\n  // buildSearchIndex() runs ONLY on initial render\\n  const [searchIndex, setSearchIndex] = useState(() => buildSearchIndex(items))\\n  const [query, setQuery] = useState('')\\n  \\n  return <SearchResults index={searchIndex} query={query} />\\n}\\n\\nfunction UserProfile() {\\n  // JSON.parse runs only on initial render\\n  const [settings, setSettings] = useState(() => {\\n    const stored = localStorage.getItem('settings')\\n    return stored ? JSON.parse(stored) : {}\\n  })\\n  \\n  return <SettingsForm settings={settings} onChange={setSettings} />\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"runs only once\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Extract Default Non-primitive Parameter Value from Memoized Component to Constant\",\n    \"type\": \"bad\",\n    \"code\": \"const UserAvatar = memo(function UserAvatar({ onClick = () => {} }: { onClick?: () => void }) {\\n  // ...\\n})\\n\\n// Used without optional onClick\\n<UserAvatar />\",\n    \"language\": \"tsx\",\n    \"description\": \"`onClick` has different values on every rerender\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Extract Default Non-primitive Parameter Value from Memoized Component to Constant\",\n    \"type\": \"good\",\n    \"code\": \"const NOOP = () => {};\\n\\nconst UserAvatar = memo(function UserAvatar({ onClick = NOOP }: { onClick?: () => void }) {\\n  // ...\\n})\\n\\n// Used without optional onClick\\n<UserAvatar />\",\n    \"language\": \"tsx\",\n    \"description\": \"stable default value\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Extract to Memoized Components\",\n    \"type\": \"bad\",\n    \"code\": \"function Profile({ user, loading }: Props) {\\n  const avatar = useMemo(() => {\\n    const id = computeAvatarId(user)\\n    return <Avatar id={id} />\\n  }, [user])\\n\\n  if (loading) return <Skeleton />\\n  return <div>{avatar}</div>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"computes avatar even when loading\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Extract to Memoized Components\",\n    \"type\": \"good\",\n    \"code\": \"const UserAvatar = memo(function UserAvatar({ user }: { user: User }) {\\n  const id = useMemo(() => computeAvatarId(user), [user])\\n  return <Avatar id={id} />\\n})\\n\\nfunction Profile({ user, loading }: Props) {\\n  if (loading) return <Skeleton />\\n  return (\\n    <div>\\n      <UserAvatar user={user} />\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"skips computation when loading\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Put Interaction Logic in Event Handlers\",\n    \"type\": \"bad\",\n    \"code\": \"function Form() {\\n  const [submitted, setSubmitted] = useState(false)\\n  const theme = useContext(ThemeContext)\\n\\n  useEffect(() => {\\n    if (submitted) {\\n      post('/api/register')\\n      showToast('Registered', theme)\\n    }\\n  }, [submitted, theme])\\n\\n  return <button onClick={() => setSubmitted(true)}>Submit</button>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"event modeled as state + effect\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Put Interaction Logic in Event Handlers\",\n    \"type\": \"good\",\n    \"code\": \"function Form() {\\n  const theme = useContext(ThemeContext)\\n\\n  function handleSubmit() {\\n    post('/api/register')\\n    showToast('Registered', theme)\\n  }\\n\\n  return <button onClick={handleSubmit}>Submit</button>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"do it in the handler\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Don't Define Components Inside Components\",\n    \"type\": \"bad\",\n    \"code\": \"function UserProfile({ user, theme }) {\\n  // Defined inside to access `theme` - BAD\\n  const Avatar = () => (\\n    <img\\n      src={user.avatarUrl}\\n      className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}\\n    />\\n  )\\n\\n  // Defined inside to access `user` - BAD\\n  const Stats = () => (\\n    <div>\\n      <span>{user.followers} followers</span>\\n      <span>{user.posts} posts</span>\\n    </div>\\n  )\\n\\n  return (\\n    <div>\\n      <Avatar />\\n      <Stats />\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"remounts on every render\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Don't Define Components Inside Components\",\n    \"type\": \"good\",\n    \"code\": \"function Avatar({ src, theme }: { src: string; theme: string }) {\\n  return (\\n    <img\\n      src={src}\\n      className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}\\n    />\\n  )\\n}\\n\\nfunction Stats({ followers, posts }: { followers: number; posts: number }) {\\n  return (\\n    <div>\\n      <span>{followers} followers</span>\\n      <span>{posts} posts</span>\\n    </div>\\n  )\\n}\\n\\nfunction UserProfile({ user, theme }) {\\n  return (\\n    <div>\\n      <Avatar src={user.avatarUrl} theme={theme} />\\n      <Stats followers={user.followers} posts={user.posts} />\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"pass props instead\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Do not wrap a simple expression with a primitive result type in useMemo\",\n    \"type\": \"bad\",\n    \"code\": \"function Header({ user, notifications }: Props) {\\n  const isLoading = useMemo(() => {\\n    return user.isLoading || notifications.isLoading\\n  }, [user.isLoading, notifications.isLoading])\\n\\n  if (isLoading) return <Skeleton />\\n  // return some markup\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"Incorrect example for Do not wrap a simple expression with a primitive result type in useMemo\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Do not wrap a simple expression with a primitive result type in useMemo\",\n    \"type\": \"good\",\n    \"code\": \"function Header({ user, notifications }: Props) {\\n  const isLoading = user.isLoading || notifications.isLoading\\n\\n  if (isLoading) return <Skeleton />\\n  // return some markup\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"Correct example for Do not wrap a simple expression with a primitive result type in useMemo\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Split Combined Hook Computations\",\n    \"type\": \"bad\",\n    \"code\": \"const sortedProducts = useMemo(() => {\\n  const filtered = products.filter((p) => p.category === category)\\n  const sorted = filtered.toSorted((a, b) =>\\n    sortOrder === \\\"asc\\\" ? a.price - b.price : b.price - a.price\\n  )\\n  return sorted\\n}, [products, category, sortOrder])\",\n    \"language\": \"tsx\",\n    \"description\": \"changing `sortOrder` recomputes filtering\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Split Combined Hook Computations\",\n    \"type\": \"good\",\n    \"code\": \"const filteredProducts = useMemo(\\n  () => products.filter((p) => p.category === category),\\n  [products, category]\\n)\\n\\nconst sortedProducts = useMemo(\\n  () =>\\n    filteredProducts.toSorted((a, b) =>\\n      sortOrder === \\\"asc\\\" ? a.price - b.price : b.price - a.price\\n    ),\\n  [filteredProducts, sortOrder]\\n)\",\n    \"language\": \"tsx\",\n    \"description\": \"filtering only recomputes when products or category change\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Split Combined Hook Computations\",\n    \"type\": \"bad\",\n    \"code\": \"useEffect(() => {\\n  analytics.trackPageView(pathname)\\n  document.title = `${pageTitle} | My App`\\n}, [pathname, pageTitle])\",\n    \"language\": \"tsx\",\n    \"description\": \"both effects run when either dependency changes\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Split Combined Hook Computations\",\n    \"type\": \"good\",\n    \"code\": \"useEffect(() => {\\n  analytics.trackPageView(pathname)\\n}, [pathname])\\n\\nuseEffect(() => {\\n  document.title = `${pageTitle} | My App`\\n}, [pageTitle])\",\n    \"language\": \"tsx\",\n    \"description\": \"effects run independently\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Transitions for Non-Urgent Updates\",\n    \"type\": \"bad\",\n    \"code\": \"function ScrollTracker() {\\n  const [scrollY, setScrollY] = useState(0)\\n  useEffect(() => {\\n    const handler = () => setScrollY(window.scrollY)\\n    window.addEventListener('scroll', handler, { passive: true })\\n    return () => window.removeEventListener('scroll', handler)\\n  }, [])\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"blocks UI on every scroll\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use Transitions for Non-Urgent Updates\",\n    \"type\": \"good\",\n    \"code\": \"import { startTransition } from 'react'\\n\\nfunction ScrollTracker() {\\n  const [scrollY, setScrollY] = useState(0)\\n  useEffect(() => {\\n    const handler = () => {\\n      startTransition(() => setScrollY(window.scrollY))\\n    }\\n    window.addEventListener('scroll', handler, { passive: true })\\n    return () => window.removeEventListener('scroll', handler)\\n  }, [])\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"non-blocking updates\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use useDeferredValue for Expensive Derived Renders\",\n    \"type\": \"bad\",\n    \"code\": \"function Search({ items }: { items: Item[] }) {\\n  const [query, setQuery] = useState('')\\n  const filtered = items.filter(item => fuzzyMatch(item, query))\\n\\n  return (\\n    <>\\n      <input value={query} onChange={e => setQuery(e.target.value)} />\\n      <ResultsList results={filtered} />\\n    </>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"input feels laggy while filtering\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use useDeferredValue for Expensive Derived Renders\",\n    \"type\": \"good\",\n    \"code\": \"function Search({ items }: { items: Item[] }) {\\n  const [query, setQuery] = useState('')\\n  const deferredQuery = useDeferredValue(query)\\n  const filtered = useMemo(\\n    () => items.filter(item => fuzzyMatch(item, deferredQuery)),\\n    [items, deferredQuery]\\n  )\\n  const isStale = query !== deferredQuery\\n\\n  return (\\n    <>\\n      <input value={query} onChange={e => setQuery(e.target.value)} />\\n      <div style={{ opacity: isStale ? 0.7 : 1 }}>\\n        <ResultsList results={filtered} />\\n      </div>\\n    </>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"input stays snappy, results render when ready\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use useRef for Transient Values\",\n    \"type\": \"bad\",\n    \"code\": \"function Tracker() {\\n  const [lastX, setLastX] = useState(0)\\n\\n  useEffect(() => {\\n    const onMove = (e: MouseEvent) => setLastX(e.clientX)\\n    window.addEventListener('mousemove', onMove)\\n    return () => window.removeEventListener('mousemove', onMove)\\n  }, [])\\n\\n  return (\\n    <div\\n      style={{\\n        position: 'fixed',\\n        top: 0,\\n        left: lastX,\\n        width: 8,\\n        height: 8,\\n        background: 'black',\\n      }}\\n    />\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"renders every update\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use useRef for Transient Values\",\n    \"type\": \"good\",\n    \"code\": \"function Tracker() {\\n  const lastXRef = useRef(0)\\n  const dotRef = useRef<HTMLDivElement>(null)\\n\\n  useEffect(() => {\\n    const onMove = (e: MouseEvent) => {\\n      lastXRef.current = e.clientX\\n      const node = dotRef.current\\n      if (node) {\\n        node.style.transform = `translateX(${e.clientX}px)`\\n      }\\n    }\\n    window.addEventListener('mousemove', onMove)\\n    return () => window.removeEventListener('mousemove', onMove)\\n  }, [])\\n\\n  return (\\n    <div\\n      ref={dotRef}\\n      style={{\\n        position: 'fixed',\\n        top: 0,\\n        left: 0,\\n        width: 8,\\n        height: 8,\\n        background: 'black',\\n        transform: 'translateX(0px)',\\n      }}\\n    />\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"no re-render for tracking\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use after() for Non-Blocking Operations\",\n    \"type\": \"bad\",\n    \"code\": \"import { logUserAction } from '@/app/utils'\\n\\nexport async function POST(request: Request) {\\n  // Perform mutation\\n  await updateDatabase(request)\\n  \\n  // Logging blocks the response\\n  const userAgent = request.headers.get('user-agent') || 'unknown'\\n  await logUserAction({ userAgent })\\n  \\n  return new Response(JSON.stringify({ status: 'success' }), {\\n    status: 200,\\n    headers: { 'Content-Type': 'application/json' }\\n  })\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"blocks response\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Use after() for Non-Blocking Operations\",\n    \"type\": \"good\",\n    \"code\": \"import { after } from 'next/server'\\nimport { headers, cookies } from 'next/headers'\\nimport { logUserAction } from '@/app/utils'\\n\\nexport async function POST(request: Request) {\\n  // Perform mutation\\n  await updateDatabase(request)\\n  \\n  // Log after response is sent\\n  after(async () => {\\n    const userAgent = (await headers()).get('user-agent') || 'unknown'\\n    const sessionCookie = (await cookies()).get('session-id')?.value || 'anonymous'\\n    \\n    logUserAction({ sessionCookie, userAgent })\\n  })\\n  \\n  return new Response(JSON.stringify({ status: 'success' }), {\\n    status: 200,\\n    headers: { 'Content-Type': 'application/json' }\\n  })\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"non-blocking\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Authenticate Server Actions Like API Routes\",\n    \"type\": \"bad\",\n    \"code\": \"'use server'\\n\\nexport async function deleteUser(userId: string) {\\n  // Anyone can call this! No auth check\\n  await db.user.delete({ where: { id: userId } })\\n  return { success: true }\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"no authentication check\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Authenticate Server Actions Like API Routes\",\n    \"type\": \"good\",\n    \"code\": \"'use server'\\n\\nimport { verifySession } from '@/lib/auth'\\nimport { unauthorized } from '@/lib/errors'\\n\\nexport async function deleteUser(userId: string) {\\n  // Always check auth inside the action\\n  const session = await verifySession()\\n  \\n  if (!session) {\\n    throw unauthorized('Must be logged in')\\n  }\\n  \\n  // Check authorization too\\n  if (session.user.role !== 'admin' && session.user.id !== userId) {\\n    throw unauthorized('Cannot delete other users')\\n  }\\n  \\n  await db.user.delete({ where: { id: userId } })\\n  return { success: true }\\n}\",\n    \"language\": \"typescript\",\n    \"description\": \"authentication inside the action\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Per-Request Deduplication with React.cache()\",\n    \"type\": \"bad\",\n    \"code\": \"const getUser = cache(async (params: { uid: number }) => {\\n  return await db.user.findUnique({ where: { id: params.uid } })\\n})\\n\\n// Each call creates new object, never hits cache\\ngetUser({ uid: 1 })\\ngetUser({ uid: 1 })  // Cache miss, runs query again\",\n    \"language\": \"typescript\",\n    \"description\": \"always cache miss\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Per-Request Deduplication with React.cache()\",\n    \"type\": \"good\",\n    \"code\": \"const params = { uid: 1 }\\ngetUser(params)  // Query runs\\ngetUser(params)  // Cache hit (same reference)\",\n    \"language\": \"typescript\",\n    \"description\": \"cache hit\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Avoid Duplicate Serialization in RSC Props\",\n    \"type\": \"bad\",\n    \"code\": \"// RSC: sends 6 strings (2 arrays × 3 items)\\n<ClientList usernames={usernames} usernamesOrdered={usernames.toSorted()} />\",\n    \"language\": \"tsx\",\n    \"description\": \"duplicates array\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Avoid Duplicate Serialization in RSC Props\",\n    \"type\": \"good\",\n    \"code\": \"// RSC: send once\\n<ClientList usernames={usernames} />\\n\\n// Client: transform there\\n'use client'\\nconst sorted = useMemo(() => [...usernames].sort(), [usernames])\",\n    \"language\": \"tsx\",\n    \"description\": \"sends 3 strings\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Parallel Data Fetching with Component Composition\",\n    \"type\": \"bad\",\n    \"code\": \"export default async function Page() {\\n  const header = await fetchHeader()\\n  return (\\n    <div>\\n      <div>{header}</div>\\n      <Sidebar />\\n    </div>\\n  )\\n}\\n\\nasync function Sidebar() {\\n  const items = await fetchSidebarItems()\\n  return <nav>{items.map(renderItem)}</nav>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"Sidebar waits for Page's fetch to complete\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Parallel Data Fetching with Component Composition\",\n    \"type\": \"good\",\n    \"code\": \"async function Header() {\\n  const data = await fetchHeader()\\n  return <div>{data}</div>\\n}\\n\\nasync function Sidebar() {\\n  const items = await fetchSidebarItems()\\n  return <nav>{items.map(renderItem)}</nav>\\n}\\n\\nexport default function Page() {\\n  return (\\n    <div>\\n      <Header />\\n      <Sidebar />\\n    </div>\\n  )\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"both fetch simultaneously\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Minimize Serialization at RSC Boundaries\",\n    \"type\": \"bad\",\n    \"code\": \"async function Page() {\\n  const user = await fetchUser()  // 50 fields\\n  return <Profile user={user} />\\n}\\n\\n'use client'\\nfunction Profile({ user }: { user: User }) {\\n  return <div>{user.name}</div>  // uses 1 field\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"serializes all 50 fields\"\n  },\n  {\n    \"ruleId\": \"\",\n    \"ruleTitle\": \"Minimize Serialization at RSC Boundaries\",\n    \"type\": \"good\",\n    \"code\": \"async function Page() {\\n  const user = await fetchUser()\\n  return <Profile name={user.name} />\\n}\\n\\n'use client'\\nfunction Profile({ name }: { name: string }) {\\n  return <div>{name}</div>\\n}\",\n    \"language\": \"tsx\",\n    \"description\": \"serializes only 1 field\"\n  }\n]"
  },
  {
    "path": "packages/react-best-practices-build/tsconfig.json",
    "content": "{\n  \"compilerOptions\": {\n    \"target\": \"ES2022\",\n    \"module\": \"ESNext\",\n    \"lib\": [\"ES2022\"],\n    \"moduleResolution\": \"node\",\n    \"esModuleInterop\": true,\n    \"strict\": true,\n    \"skipLibCheck\": true,\n    \"forceConsistentCasingInFileNames\": true,\n    \"resolveJsonModule\": true,\n    \"outDir\": \"./dist\",\n    \"rootDir\": \"./src\"\n  },\n  \"include\": [\"src/**/*\"],\n  \"exclude\": [\"node_modules\", \"dist\"]\n}\n"
  },
  {
    "path": "skills/composition-patterns/AGENTS.md",
    "content": "# React Composition Patterns\n\n**Version 1.0.0**  \nEngineering  \nJanuary 2026\n\n> **Note:**  \n> This document is mainly for agents and LLMs to follow when maintaining,  \n> generating, or refactoring React codebases using composition. Humans  \n> may also find it useful, but guidance here is optimized for automation  \n> and consistency by AI-assisted workflows.\n\n---\n\n## Abstract\n\nComposition patterns for building flexible, maintainable React components. Avoid boolean prop proliferation by using compound components, lifting state, and composing internals. These patterns make codebases easier for both humans and AI agents to work with as they scale.\n\n---\n\n## Table of Contents\n\n1. [Component Architecture](#1-component-architecture) — **HIGH**\n   - 1.1 [Avoid Boolean Prop Proliferation](#11-avoid-boolean-prop-proliferation)\n   - 1.2 [Use Compound Components](#12-use-compound-components)\n2. [State Management](#2-state-management) — **MEDIUM**\n   - 2.1 [Decouple State Management from UI](#21-decouple-state-management-from-ui)\n   - 2.2 [Define Generic Context Interfaces for Dependency Injection](#22-define-generic-context-interfaces-for-dependency-injection)\n   - 2.3 [Lift State into Provider Components](#23-lift-state-into-provider-components)\n3. [Implementation Patterns](#3-implementation-patterns) — **MEDIUM**\n   - 3.1 [Create Explicit Component Variants](#31-create-explicit-component-variants)\n   - 3.2 [Prefer Composing Children Over Render Props](#32-prefer-composing-children-over-render-props)\n4. [React 19 APIs](#4-react-19-apis) — **MEDIUM**\n   - 4.1 [React 19 API Changes](#41-react-19-api-changes)\n\n---\n\n## 1. Component Architecture\n\n**Impact: HIGH**\n\nFundamental patterns for structuring components to avoid prop\nproliferation and enable flexible composition.\n\n### 1.1 Avoid Boolean Prop Proliferation\n\n**Impact: CRITICAL (prevents unmaintainable component variants)**\n\nDon't add boolean props like `isThread`, `isEditing`, `isDMThread` to customize\n\ncomponent behavior. Each boolean doubles possible states and creates\n\nunmaintainable conditional logic. Use composition instead.\n\n**Incorrect: boolean props create exponential complexity**\n\n```tsx\nfunction Composer({\n  onSubmit,\n  isThread,\n  channelId,\n  isDMThread,\n  dmId,\n  isEditing,\n  isForwarding,\n}: Props) {\n  return (\n    <form>\n      <Header />\n      <Input />\n      {isDMThread ? (\n        <AlsoSendToDMField id={dmId} />\n      ) : isThread ? (\n        <AlsoSendToChannelField id={channelId} />\n      ) : null}\n      {isEditing ? (\n        <EditActions />\n      ) : isForwarding ? (\n        <ForwardActions />\n      ) : (\n        <DefaultActions />\n      )}\n      <Footer onSubmit={onSubmit} />\n    </form>\n  )\n}\n```\n\n**Correct: composition eliminates conditionals**\n\n```tsx\n// Channel composer\nfunction ChannelComposer() {\n  return (\n    <Composer.Frame>\n      <Composer.Header />\n      <Composer.Input />\n      <Composer.Footer>\n        <Composer.Attachments />\n        <Composer.Formatting />\n        <Composer.Emojis />\n        <Composer.Submit />\n      </Composer.Footer>\n    </Composer.Frame>\n  )\n}\n\n// Thread composer - adds \"also send to channel\" field\nfunction ThreadComposer({ channelId }: { channelId: string }) {\n  return (\n    <Composer.Frame>\n      <Composer.Header />\n      <Composer.Input />\n      <AlsoSendToChannelField id={channelId} />\n      <Composer.Footer>\n        <Composer.Formatting />\n        <Composer.Emojis />\n        <Composer.Submit />\n      </Composer.Footer>\n    </Composer.Frame>\n  )\n}\n\n// Edit composer - different footer actions\nfunction EditComposer() {\n  return (\n    <Composer.Frame>\n      <Composer.Input />\n      <Composer.Footer>\n        <Composer.Formatting />\n        <Composer.Emojis />\n        <Composer.CancelEdit />\n        <Composer.SaveEdit />\n      </Composer.Footer>\n    </Composer.Frame>\n  )\n}\n```\n\nEach variant is explicit about what it renders. We can share internals without\n\nsharing a single monolithic parent.\n\n### 1.2 Use Compound Components\n\n**Impact: HIGH (enables flexible composition without prop drilling)**\n\nStructure complex components as compound components with a shared context. Each\n\nsubcomponent accesses shared state via context, not props. Consumers compose the\n\npieces they need.\n\n**Incorrect: monolithic component with render props**\n\n```tsx\nfunction Composer({\n  renderHeader,\n  renderFooter,\n  renderActions,\n  showAttachments,\n  showFormatting,\n  showEmojis,\n}: Props) {\n  return (\n    <form>\n      {renderHeader?.()}\n      <Input />\n      {showAttachments && <Attachments />}\n      {renderFooter ? (\n        renderFooter()\n      ) : (\n        <Footer>\n          {showFormatting && <Formatting />}\n          {showEmojis && <Emojis />}\n          {renderActions?.()}\n        </Footer>\n      )}\n    </form>\n  )\n}\n```\n\n**Correct: compound components with shared context**\n\n```tsx\nconst ComposerContext = createContext<ComposerContextValue | null>(null)\n\nfunction ComposerProvider({ children, state, actions, meta }: ProviderProps) {\n  return (\n    <ComposerContext value={{ state, actions, meta }}>\n      {children}\n    </ComposerContext>\n  )\n}\n\nfunction ComposerFrame({ children }: { children: React.ReactNode }) {\n  return <form>{children}</form>\n}\n\nfunction ComposerInput() {\n  const {\n    state,\n    actions: { update },\n    meta: { inputRef },\n  } = use(ComposerContext)\n  return (\n    <TextInput\n      ref={inputRef}\n      value={state.input}\n      onChangeText={(text) => update((s) => ({ ...s, input: text }))}\n    />\n  )\n}\n\nfunction ComposerSubmit() {\n  const {\n    actions: { submit },\n  } = use(ComposerContext)\n  return <Button onPress={submit}>Send</Button>\n}\n\n// Export as compound component\nconst Composer = {\n  Provider: ComposerProvider,\n  Frame: ComposerFrame,\n  Input: ComposerInput,\n  Submit: ComposerSubmit,\n  Header: ComposerHeader,\n  Footer: ComposerFooter,\n  Attachments: ComposerAttachments,\n  Formatting: ComposerFormatting,\n  Emojis: ComposerEmojis,\n}\n```\n\n**Usage:**\n\n```tsx\n<Composer.Provider state={state} actions={actions} meta={meta}>\n  <Composer.Frame>\n    <Composer.Header />\n    <Composer.Input />\n    <Composer.Footer>\n      <Composer.Formatting />\n      <Composer.Submit />\n    </Composer.Footer>\n  </Composer.Frame>\n</Composer.Provider>\n```\n\nConsumers explicitly compose exactly what they need. No hidden conditionals. And the state, actions and meta are dependency-injected by a parent provider, allowing multiple usages of the same component structure.\n\n---\n\n## 2. State Management\n\n**Impact: MEDIUM**\n\nPatterns for lifting state and managing shared context across\ncomposed components.\n\n### 2.1 Decouple State Management from UI\n\n**Impact: MEDIUM (enables swapping state implementations without changing UI)**\n\nThe provider component should be the only place that knows how state is managed.\n\nUI components consume the context interface—they don't know if state comes from\n\nuseState, Zustand, or a server sync.\n\n**Incorrect: UI coupled to state implementation**\n\n```tsx\nfunction ChannelComposer({ channelId }: { channelId: string }) {\n  // UI component knows about global state implementation\n  const state = useGlobalChannelState(channelId)\n  const { submit, updateInput } = useChannelSync(channelId)\n\n  return (\n    <Composer.Frame>\n      <Composer.Input\n        value={state.input}\n        onChange={(text) => sync.updateInput(text)}\n      />\n      <Composer.Submit onPress={() => sync.submit()} />\n    </Composer.Frame>\n  )\n}\n```\n\n**Correct: state management isolated in provider**\n\n```tsx\n// Provider handles all state management details\nfunction ChannelProvider({\n  channelId,\n  children,\n}: {\n  channelId: string\n  children: React.ReactNode\n}) {\n  const { state, update, submit } = useGlobalChannel(channelId)\n  const inputRef = useRef(null)\n\n  return (\n    <Composer.Provider\n      state={state}\n      actions={{ update, submit }}\n      meta={{ inputRef }}\n    >\n      {children}\n    </Composer.Provider>\n  )\n}\n\n// UI component only knows about the context interface\nfunction ChannelComposer() {\n  return (\n    <Composer.Frame>\n      <Composer.Header />\n      <Composer.Input />\n      <Composer.Footer>\n        <Composer.Submit />\n      </Composer.Footer>\n    </Composer.Frame>\n  )\n}\n\n// Usage\nfunction Channel({ channelId }: { channelId: string }) {\n  return (\n    <ChannelProvider channelId={channelId}>\n      <ChannelComposer />\n    </ChannelProvider>\n  )\n}\n```\n\n**Different providers, same UI:**\n\n```tsx\n// Local state for ephemeral forms\nfunction ForwardMessageProvider({ children }) {\n  const [state, setState] = useState(initialState)\n  const forwardMessage = useForwardMessage()\n\n  return (\n    <Composer.Provider\n      state={state}\n      actions={{ update: setState, submit: forwardMessage }}\n    >\n      {children}\n    </Composer.Provider>\n  )\n}\n\n// Global synced state for channels\nfunction ChannelProvider({ channelId, children }) {\n  const { state, update, submit } = useGlobalChannel(channelId)\n\n  return (\n    <Composer.Provider state={state} actions={{ update, submit }}>\n      {children}\n    </Composer.Provider>\n  )\n}\n```\n\nThe same `Composer.Input` component works with both providers because it only\n\ndepends on the context interface, not the implementation.\n\n### 2.2 Define Generic Context Interfaces for Dependency Injection\n\n**Impact: HIGH (enables dependency-injectable state across use-cases)**\n\nDefine a **generic interface** for your component context with three parts:\n\n`state`, `actions`, and `meta`. This interface is a contract that any provider\n\ncan implement—enabling the same UI components to work with completely different\n\nstate implementations.\n\n**Core principle:** Lift state, compose internals, make state\n\ndependency-injectable.\n\n**Incorrect: UI coupled to specific state implementation**\n\n```tsx\nfunction ComposerInput() {\n  // Tightly coupled to a specific hook\n  const { input, setInput } = useChannelComposerState()\n  return <TextInput value={input} onChangeText={setInput} />\n}\n```\n\n**Correct: generic interface enables dependency injection**\n\n```tsx\n// Define a GENERIC interface that any provider can implement\ninterface ComposerState {\n  input: string\n  attachments: Attachment[]\n  isSubmitting: boolean\n}\n\ninterface ComposerActions {\n  update: (updater: (state: ComposerState) => ComposerState) => void\n  submit: () => void\n}\n\ninterface ComposerMeta {\n  inputRef: React.RefObject<TextInput>\n}\n\ninterface ComposerContextValue {\n  state: ComposerState\n  actions: ComposerActions\n  meta: ComposerMeta\n}\n\nconst ComposerContext = createContext<ComposerContextValue | null>(null)\n```\n\n**UI components consume the interface, not the implementation:**\n\n```tsx\nfunction ComposerInput() {\n  const {\n    state,\n    actions: { update },\n    meta,\n  } = use(ComposerContext)\n\n  // This component works with ANY provider that implements the interface\n  return (\n    <TextInput\n      ref={meta.inputRef}\n      value={state.input}\n      onChangeText={(text) => update((s) => ({ ...s, input: text }))}\n    />\n  )\n}\n```\n\n**Different providers implement the same interface:**\n\n```tsx\n// Provider A: Local state for ephemeral forms\nfunction ForwardMessageProvider({ children }: { children: React.ReactNode }) {\n  const [state, setState] = useState(initialState)\n  const inputRef = useRef(null)\n  const submit = useForwardMessage()\n\n  return (\n    <ComposerContext\n      value={{\n        state,\n        actions: { update: setState, submit },\n        meta: { inputRef },\n      }}\n    >\n      {children}\n    </ComposerContext>\n  )\n}\n\n// Provider B: Global synced state for channels\nfunction ChannelProvider({ channelId, children }: Props) {\n  const { state, update, submit } = useGlobalChannel(channelId)\n  const inputRef = useRef(null)\n\n  return (\n    <ComposerContext\n      value={{\n        state,\n        actions: { update, submit },\n        meta: { inputRef },\n      }}\n    >\n      {children}\n    </ComposerContext>\n  )\n}\n```\n\n**The same composed UI works with both:**\n\n```tsx\n// Works with ForwardMessageProvider (local state)\n<ForwardMessageProvider>\n  <Composer.Frame>\n    <Composer.Input />\n    <Composer.Submit />\n  </Composer.Frame>\n</ForwardMessageProvider>\n\n// Works with ChannelProvider (global synced state)\n<ChannelProvider channelId=\"abc\">\n  <Composer.Frame>\n    <Composer.Input />\n    <Composer.Submit />\n  </Composer.Frame>\n</ChannelProvider>\n```\n\n**Custom UI outside the component can access state and actions:**\n\n```tsx\nfunction ForwardMessageDialog() {\n  return (\n    <ForwardMessageProvider>\n      <Dialog>\n        {/* The composer UI */}\n        <Composer.Frame>\n          <Composer.Input placeholder=\"Add a message, if you'd like.\" />\n          <Composer.Footer>\n            <Composer.Formatting />\n            <Composer.Emojis />\n          </Composer.Footer>\n        </Composer.Frame>\n\n        {/* Custom UI OUTSIDE the composer, but INSIDE the provider */}\n        <MessagePreview />\n\n        {/* Actions at the bottom of the dialog */}\n        <DialogActions>\n          <CancelButton />\n          <ForwardButton />\n        </DialogActions>\n      </Dialog>\n    </ForwardMessageProvider>\n  )\n}\n\n// This button lives OUTSIDE Composer.Frame but can still submit based on its context!\nfunction ForwardButton() {\n  const {\n    actions: { submit },\n  } = use(ComposerContext)\n  return <Button onPress={submit}>Forward</Button>\n}\n\n// This preview lives OUTSIDE Composer.Frame but can read composer's state!\nfunction MessagePreview() {\n  const { state } = use(ComposerContext)\n  return <Preview message={state.input} attachments={state.attachments} />\n}\n```\n\nThe provider boundary is what matters—not the visual nesting. Components that\n\nneed shared state don't have to be inside the `Composer.Frame`. They just need\n\nto be within the provider.\n\nThe `ForwardButton` and `MessagePreview` are not visually inside the composer\n\nbox, but they can still access its state and actions. This is the power of\n\nlifting state into providers.\n\nThe UI is reusable bits you compose together. The state is dependency-injected\n\nby the provider. Swap the provider, keep the UI.\n\n### 2.3 Lift State into Provider Components\n\n**Impact: HIGH (enables state sharing outside component boundaries)**\n\nMove state management into dedicated provider components. This allows sibling\n\ncomponents outside the main UI to access and modify state without prop drilling\n\nor awkward refs.\n\n**Incorrect: state trapped inside component**\n\n```tsx\nfunction ForwardMessageComposer() {\n  const [state, setState] = useState(initialState)\n  const forwardMessage = useForwardMessage()\n\n  return (\n    <Composer.Frame>\n      <Composer.Input />\n      <Composer.Footer />\n    </Composer.Frame>\n  )\n}\n\n// Problem: How does this button access composer state?\nfunction ForwardMessageDialog() {\n  return (\n    <Dialog>\n      <ForwardMessageComposer />\n      <MessagePreview /> {/* Needs composer state */}\n      <DialogActions>\n        <CancelButton />\n        <ForwardButton /> {/* Needs to call submit */}\n      </DialogActions>\n    </Dialog>\n  )\n}\n```\n\n**Incorrect: useEffect to sync state up**\n\n```tsx\nfunction ForwardMessageDialog() {\n  const [input, setInput] = useState('')\n  return (\n    <Dialog>\n      <ForwardMessageComposer onInputChange={setInput} />\n      <MessagePreview input={input} />\n    </Dialog>\n  )\n}\n\nfunction ForwardMessageComposer({ onInputChange }) {\n  const [state, setState] = useState(initialState)\n  useEffect(() => {\n    onInputChange(state.input) // Sync on every change 😬\n  }, [state.input])\n}\n```\n\n**Incorrect: reading state from ref on submit**\n\n```tsx\nfunction ForwardMessageDialog() {\n  const stateRef = useRef(null)\n  return (\n    <Dialog>\n      <ForwardMessageComposer stateRef={stateRef} />\n      <ForwardButton onPress={() => submit(stateRef.current)} />\n    </Dialog>\n  )\n}\n```\n\n**Correct: state lifted to provider**\n\n```tsx\nfunction ForwardMessageProvider({ children }: { children: React.ReactNode }) {\n  const [state, setState] = useState(initialState)\n  const forwardMessage = useForwardMessage()\n  const inputRef = useRef(null)\n\n  return (\n    <Composer.Provider\n      state={state}\n      actions={{ update: setState, submit: forwardMessage }}\n      meta={{ inputRef }}\n    >\n      {children}\n    </Composer.Provider>\n  )\n}\n\nfunction ForwardMessageDialog() {\n  return (\n    <ForwardMessageProvider>\n      <Dialog>\n        <ForwardMessageComposer />\n        <MessagePreview /> {/* Custom components can access state and actions */}\n        <DialogActions>\n          <CancelButton />\n          <ForwardButton /> {/* Custom components can access state and actions */}\n        </DialogActions>\n      </Dialog>\n    </ForwardMessageProvider>\n  )\n}\n\nfunction ForwardButton() {\n  const { actions } = use(Composer.Context)\n  return <Button onPress={actions.submit}>Forward</Button>\n}\n```\n\nThe ForwardButton lives outside the Composer.Frame but still has access to the\n\nsubmit action because it's within the provider. Even though it's a one-off\n\ncomponent, it can still access the composer's state and actions from outside the\n\nUI itself.\n\n**Key insight:** Components that need shared state don't have to be visually\n\nnested inside each other—they just need to be within the same provider.\n\n---\n\n## 3. Implementation Patterns\n\n**Impact: MEDIUM**\n\nSpecific techniques for implementing compound components and\ncontext providers.\n\n### 3.1 Create Explicit Component Variants\n\n**Impact: MEDIUM (self-documenting code, no hidden conditionals)**\n\nInstead of one component with many boolean props, create explicit variant\n\ncomponents. Each variant composes the pieces it needs. The code documents\n\nitself.\n\n**Incorrect: one component, many modes**\n\n```tsx\n// What does this component actually render?\n<Composer\n  isThread\n  isEditing={false}\n  channelId='abc'\n  showAttachments\n  showFormatting={false}\n/>\n```\n\n**Correct: explicit variants**\n\n```tsx\n// Immediately clear what this renders\n<ThreadComposer channelId=\"abc\" />\n\n// Or\n<EditMessageComposer messageId=\"xyz\" />\n\n// Or\n<ForwardMessageComposer messageId=\"123\" />\n```\n\nEach implementation is unique, explicit and self-contained. Yet they can each\n\nuse shared parts.\n\n**Implementation:**\n\n```tsx\nfunction ThreadComposer({ channelId }: { channelId: string }) {\n  return (\n    <ThreadProvider channelId={channelId}>\n      <Composer.Frame>\n        <Composer.Input />\n        <AlsoSendToChannelField channelId={channelId} />\n        <Composer.Footer>\n          <Composer.Formatting />\n          <Composer.Emojis />\n          <Composer.Submit />\n        </Composer.Footer>\n      </Composer.Frame>\n    </ThreadProvider>\n  )\n}\n\nfunction EditMessageComposer({ messageId }: { messageId: string }) {\n  return (\n    <EditMessageProvider messageId={messageId}>\n      <Composer.Frame>\n        <Composer.Input />\n        <Composer.Footer>\n          <Composer.Formatting />\n          <Composer.Emojis />\n          <Composer.CancelEdit />\n          <Composer.SaveEdit />\n        </Composer.Footer>\n      </Composer.Frame>\n    </EditMessageProvider>\n  )\n}\n\nfunction ForwardMessageComposer({ messageId }: { messageId: string }) {\n  return (\n    <ForwardMessageProvider messageId={messageId}>\n      <Composer.Frame>\n        <Composer.Input placeholder=\"Add a message, if you'd like.\" />\n        <Composer.Footer>\n          <Composer.Formatting />\n          <Composer.Emojis />\n          <Composer.Mentions />\n        </Composer.Footer>\n      </Composer.Frame>\n    </ForwardMessageProvider>\n  )\n}\n```\n\nEach variant is explicit about:\n\n- What provider/state it uses\n\n- What UI elements it includes\n\n- What actions are available\n\nNo boolean prop combinations to reason about. No impossible states.\n\n### 3.2 Prefer Composing Children Over Render Props\n\n**Impact: MEDIUM (cleaner composition, better readability)**\n\nUse `children` for composition instead of `renderX` props. Children are more\n\nreadable, compose naturally, and don't require understanding callback\n\nsignatures.\n\n**Incorrect: render props**\n\n```tsx\nfunction Composer({\n  renderHeader,\n  renderFooter,\n  renderActions,\n}: {\n  renderHeader?: () => React.ReactNode\n  renderFooter?: () => React.ReactNode\n  renderActions?: () => React.ReactNode\n}) {\n  return (\n    <form>\n      {renderHeader?.()}\n      <Input />\n      {renderFooter ? renderFooter() : <DefaultFooter />}\n      {renderActions?.()}\n    </form>\n  )\n}\n\n// Usage is awkward and inflexible\nreturn (\n  <Composer\n    renderHeader={() => <CustomHeader />}\n    renderFooter={() => (\n      <>\n        <Formatting />\n        <Emojis />\n      </>\n    )}\n    renderActions={() => <SubmitButton />}\n  />\n)\n```\n\n**Correct: compound components with children**\n\n```tsx\nfunction ComposerFrame({ children }: { children: React.ReactNode }) {\n  return <form>{children}</form>\n}\n\nfunction ComposerFooter({ children }: { children: React.ReactNode }) {\n  return <footer className='flex'>{children}</footer>\n}\n\n// Usage is flexible\nreturn (\n  <Composer.Frame>\n    <CustomHeader />\n    <Composer.Input />\n    <Composer.Footer>\n      <Composer.Formatting />\n      <Composer.Emojis />\n      <SubmitButton />\n    </Composer.Footer>\n  </Composer.Frame>\n)\n```\n\n**When render props are appropriate:**\n\n```tsx\n// Render props work well when you need to pass data back\n<List\n  data={items}\n  renderItem={({ item, index }) => <Item item={item} index={index} />}\n/>\n```\n\nUse render props when the parent needs to provide data or state to the child.\n\nUse children when composing static structure.\n\n---\n\n## 4. React 19 APIs\n\n**Impact: MEDIUM**\n\nReact 19+ only. Don't use `forwardRef`; use `use()` instead of `useContext()`.\n\n### 4.1 React 19 API Changes\n\n**Impact: MEDIUM (cleaner component definitions and context usage)**\n\n> **⚠️ React 19+ only.** Skip this if you're on React 18 or earlier.\n\nIn React 19, `ref` is now a regular prop (no `forwardRef` wrapper needed), and `use()` replaces `useContext()`.\n\n**Incorrect: forwardRef in React 19**\n\n```tsx\nconst ComposerInput = forwardRef<TextInput, Props>((props, ref) => {\n  return <TextInput ref={ref} {...props} />\n})\n```\n\n**Correct: ref as a regular prop**\n\n```tsx\nfunction ComposerInput({ ref, ...props }: Props & { ref?: React.Ref<TextInput> }) {\n  return <TextInput ref={ref} {...props} />\n}\n```\n\n**Incorrect: useContext in React 19**\n\n```tsx\nconst value = useContext(MyContext)\n```\n\n**Correct: use instead of useContext**\n\n```tsx\nconst value = use(MyContext)\n```\n\n`use()` can also be called conditionally, unlike `useContext()`.\n\n---\n\n## References\n\n1. [https://react.dev](https://react.dev)\n2. [https://react.dev/learn/passing-data-deeply-with-context](https://react.dev/learn/passing-data-deeply-with-context)\n3. [https://react.dev/reference/react/use](https://react.dev/reference/react/use)\n"
  },
  {
    "path": "skills/composition-patterns/README.md",
    "content": "# React Composition Patterns\n\nA structured repository for React composition patterns that scale. These\npatterns help avoid boolean prop proliferation by using compound components,\nlifting state, and composing internals.\n\n## Structure\n\n- `rules/` - Individual rule files (one per rule)\n  - `_sections.md` - Section metadata (titles, impacts, descriptions)\n  - `_template.md` - Template for creating new rules\n  - `area-description.md` - Individual rule files\n- `metadata.json` - Document metadata (version, organization, abstract)\n- **`AGENTS.md`** - Compiled output (generated)\n\n## Rules\n\n### Component Architecture (CRITICAL)\n\n- `architecture-avoid-boolean-props.md` - Don't add boolean props to customize\n  behavior\n- `architecture-compound-components.md` - Structure as compound components with\n  shared context\n\n### State Management (HIGH)\n\n- `state-lift-state.md` - Lift state into provider components\n- `state-context-interface.md` - Define clear context interfaces\n  (state/actions/meta)\n- `state-decouple-implementation.md` - Decouple state management from UI\n\n### Implementation Patterns (MEDIUM)\n\n- `patterns-children-over-render-props.md` - Prefer children over renderX props\n- `patterns-explicit-variants.md` - Create explicit component variants\n\n## Core Principles\n\n1. **Composition over configuration** — Instead of adding props, let consumers\n   compose\n2. **Lift your state** — State in providers, not trapped in components\n3. **Compose your internals** — Subcomponents access context, not props\n4. **Explicit variants** — Create ThreadComposer, EditComposer, not Composer\n   with isThread\n\n## Creating a New Rule\n\n1. Copy `rules/_template.md` to `rules/area-description.md`\n2. Choose the appropriate area prefix:\n   - `architecture-` for Component Architecture\n   - `state-` for State Management\n   - `patterns-` for Implementation Patterns\n3. Fill in the frontmatter and content\n4. Ensure you have clear examples with explanations\n\n## Impact Levels\n\n- `CRITICAL` - Foundational patterns, prevents unmaintainable code\n- `HIGH` - Significant maintainability improvements\n- `MEDIUM` - Good practices for cleaner code\n"
  },
  {
    "path": "skills/composition-patterns/SKILL.md",
    "content": "---\nname: vercel-composition-patterns\ndescription:\n  React composition patterns that scale. Use when refactoring components with\n  boolean prop proliferation, building flexible component libraries, or\n  designing reusable APIs. Triggers on tasks involving compound components,\n  render props, context providers, or component architecture. Includes React 19\n  API changes.\nlicense: MIT\nmetadata:\n  author: vercel\n  version: '1.0.0'\n---\n\n# React Composition Patterns\n\nComposition patterns for building flexible, maintainable React components. Avoid\nboolean prop proliferation by using compound components, lifting state, and\ncomposing internals. These patterns make codebases easier for both humans and AI\nagents to work with as they scale.\n\n## When to Apply\n\nReference these guidelines when:\n\n- Refactoring components with many boolean props\n- Building reusable component libraries\n- Designing flexible component APIs\n- Reviewing component architecture\n- Working with compound components or context providers\n\n## Rule Categories by Priority\n\n| Priority | Category                | Impact | Prefix          |\n| -------- | ----------------------- | ------ | --------------- |\n| 1        | Component Architecture  | HIGH   | `architecture-` |\n| 2        | State Management        | MEDIUM | `state-`        |\n| 3        | Implementation Patterns | MEDIUM | `patterns-`     |\n| 4        | React 19 APIs           | MEDIUM | `react19-`      |\n\n## Quick Reference\n\n### 1. Component Architecture (HIGH)\n\n- `architecture-avoid-boolean-props` - Don't add boolean props to customize\n  behavior; use composition\n- `architecture-compound-components` - Structure complex components with shared\n  context\n\n### 2. State Management (MEDIUM)\n\n- `state-decouple-implementation` - Provider is the only place that knows how\n  state is managed\n- `state-context-interface` - Define generic interface with state, actions, meta\n  for dependency injection\n- `state-lift-state` - Move state into provider components for sibling access\n\n### 3. Implementation Patterns (MEDIUM)\n\n- `patterns-explicit-variants` - Create explicit variant components instead of\n  boolean modes\n- `patterns-children-over-render-props` - Use children for composition instead\n  of renderX props\n\n### 4. React 19 APIs (MEDIUM)\n\n> **⚠️ React 19+ only.** Skip this section if using React 18 or earlier.\n\n- `react19-no-forwardref` - Don't use `forwardRef`; use `use()` instead of `useContext()`\n\n## How to Use\n\nRead individual rule files for detailed explanations and code examples:\n\n```\nrules/architecture-avoid-boolean-props.md\nrules/state-context-interface.md\n```\n\nEach rule file contains:\n\n- Brief explanation of why it matters\n- Incorrect code example with explanation\n- Correct code example with explanation\n- Additional context and references\n\n## Full Compiled Document\n\nFor the complete guide with all rules expanded: `AGENTS.md`\n"
  },
  {
    "path": "skills/composition-patterns/metadata.json",
    "content": "{\n  \"version\": \"1.0.0\",\n  \"organization\": \"Engineering\",\n  \"date\": \"January 2026\",\n  \"abstract\": \"Composition patterns for building flexible, maintainable React components. Avoid boolean prop proliferation by using compound components, lifting state, and composing internals. These patterns make codebases easier for both humans and AI agents to work with as they scale.\",\n  \"references\": [\n    \"https://react.dev\",\n    \"https://react.dev/learn/passing-data-deeply-with-context\",\n    \"https://react.dev/reference/react/use\"\n  ]\n}\n"
  },
  {
    "path": "skills/composition-patterns/rules/_sections.md",
    "content": "# Sections\n\nThis file defines all sections, their ordering, impact levels, and descriptions.\nThe section ID (in parentheses) is the filename prefix used to group rules.\n\n---\n\n## 1. Component Architecture (architecture)\n\n**Impact:** HIGH  \n**Description:** Fundamental patterns for structuring components to avoid prop\nproliferation and enable flexible composition.\n\n## 2. State Management (state)\n\n**Impact:** MEDIUM  \n**Description:** Patterns for lifting state and managing shared context across\ncomposed components.\n\n## 3. Implementation Patterns (patterns)\n\n**Impact:** MEDIUM  \n**Description:** Specific techniques for implementing compound components and\ncontext providers.\n\n## 4. React 19 APIs (react19)\n\n**Impact:** MEDIUM  \n**Description:** React 19+ only. Don't use `forwardRef`; use `use()` instead of `useContext()`.\n"
  },
  {
    "path": "skills/composition-patterns/rules/_template.md",
    "content": "---\ntitle: Rule Title Here\nimpact: MEDIUM\nimpactDescription: brief description of impact\ntags: composition, components\n---\n\n## Rule Title Here\n\nBrief explanation of the rule and why it matters.\n\n**Incorrect:**\n\n```tsx\n// Bad code example\n```\n\n**Correct:**\n\n```tsx\n// Good code example\n```\n\nReference: [Link](https://example.com)\n"
  },
  {
    "path": "skills/composition-patterns/rules/architecture-avoid-boolean-props.md",
    "content": "---\ntitle: Avoid Boolean Prop Proliferation\nimpact: CRITICAL\nimpactDescription: prevents unmaintainable component variants\ntags: composition, props, architecture\n---\n\n## Avoid Boolean Prop Proliferation\n\nDon't add boolean props like `isThread`, `isEditing`, `isDMThread` to customize\ncomponent behavior. Each boolean doubles possible states and creates\nunmaintainable conditional logic. Use composition instead.\n\n**Incorrect (boolean props create exponential complexity):**\n\n```tsx\nfunction Composer({\n  onSubmit,\n  isThread,\n  channelId,\n  isDMThread,\n  dmId,\n  isEditing,\n  isForwarding,\n}: Props) {\n  return (\n    <form>\n      <Header />\n      <Input />\n      {isDMThread ? (\n        <AlsoSendToDMField id={dmId} />\n      ) : isThread ? (\n        <AlsoSendToChannelField id={channelId} />\n      ) : null}\n      {isEditing ? (\n        <EditActions />\n      ) : isForwarding ? (\n        <ForwardActions />\n      ) : (\n        <DefaultActions />\n      )}\n      <Footer onSubmit={onSubmit} />\n    </form>\n  )\n}\n```\n\n**Correct (composition eliminates conditionals):**\n\n```tsx\n// Channel composer\nfunction ChannelComposer() {\n  return (\n    <Composer.Frame>\n      <Composer.Header />\n      <Composer.Input />\n      <Composer.Footer>\n        <Composer.Attachments />\n        <Composer.Formatting />\n        <Composer.Emojis />\n        <Composer.Submit />\n      </Composer.Footer>\n    </Composer.Frame>\n  )\n}\n\n// Thread composer - adds \"also send to channel\" field\nfunction ThreadComposer({ channelId }: { channelId: string }) {\n  return (\n    <Composer.Frame>\n      <Composer.Header />\n      <Composer.Input />\n      <AlsoSendToChannelField id={channelId} />\n      <Composer.Footer>\n        <Composer.Formatting />\n        <Composer.Emojis />\n        <Composer.Submit />\n      </Composer.Footer>\n    </Composer.Frame>\n  )\n}\n\n// Edit composer - different footer actions\nfunction EditComposer() {\n  return (\n    <Composer.Frame>\n      <Composer.Input />\n      <Composer.Footer>\n        <Composer.Formatting />\n        <Composer.Emojis />\n        <Composer.CancelEdit />\n        <Composer.SaveEdit />\n      </Composer.Footer>\n    </Composer.Frame>\n  )\n}\n```\n\nEach variant is explicit about what it renders. We can share internals without\nsharing a single monolithic parent.\n"
  },
  {
    "path": "skills/composition-patterns/rules/architecture-compound-components.md",
    "content": "---\ntitle: Use Compound Components\nimpact: HIGH\nimpactDescription: enables flexible composition without prop drilling\ntags: composition, compound-components, architecture\n---\n\n## Use Compound Components\n\nStructure complex components as compound components with a shared context. Each\nsubcomponent accesses shared state via context, not props. Consumers compose the\npieces they need.\n\n**Incorrect (monolithic component with render props):**\n\n```tsx\nfunction Composer({\n  renderHeader,\n  renderFooter,\n  renderActions,\n  showAttachments,\n  showFormatting,\n  showEmojis,\n}: Props) {\n  return (\n    <form>\n      {renderHeader?.()}\n      <Input />\n      {showAttachments && <Attachments />}\n      {renderFooter ? (\n        renderFooter()\n      ) : (\n        <Footer>\n          {showFormatting && <Formatting />}\n          {showEmojis && <Emojis />}\n          {renderActions?.()}\n        </Footer>\n      )}\n    </form>\n  )\n}\n```\n\n**Correct (compound components with shared context):**\n\n```tsx\nconst ComposerContext = createContext<ComposerContextValue | null>(null)\n\nfunction ComposerProvider({ children, state, actions, meta }: ProviderProps) {\n  return (\n    <ComposerContext value={{ state, actions, meta }}>\n      {children}\n    </ComposerContext>\n  )\n}\n\nfunction ComposerFrame({ children }: { children: React.ReactNode }) {\n  return <form>{children}</form>\n}\n\nfunction ComposerInput() {\n  const {\n    state,\n    actions: { update },\n    meta: { inputRef },\n  } = use(ComposerContext)\n  return (\n    <TextInput\n      ref={inputRef}\n      value={state.input}\n      onChangeText={(text) => update((s) => ({ ...s, input: text }))}\n    />\n  )\n}\n\nfunction ComposerSubmit() {\n  const {\n    actions: { submit },\n  } = use(ComposerContext)\n  return <Button onPress={submit}>Send</Button>\n}\n\n// Export as compound component\nconst Composer = {\n  Provider: ComposerProvider,\n  Frame: ComposerFrame,\n  Input: ComposerInput,\n  Submit: ComposerSubmit,\n  Header: ComposerHeader,\n  Footer: ComposerFooter,\n  Attachments: ComposerAttachments,\n  Formatting: ComposerFormatting,\n  Emojis: ComposerEmojis,\n}\n```\n\n**Usage:**\n\n```tsx\n<Composer.Provider state={state} actions={actions} meta={meta}>\n  <Composer.Frame>\n    <Composer.Header />\n    <Composer.Input />\n    <Composer.Footer>\n      <Composer.Formatting />\n      <Composer.Submit />\n    </Composer.Footer>\n  </Composer.Frame>\n</Composer.Provider>\n```\n\nConsumers explicitly compose exactly what they need. No hidden conditionals. And the state, actions and meta are dependency-injected by a parent provider, allowing multiple usages of the same component structure.\n"
  },
  {
    "path": "skills/composition-patterns/rules/patterns-children-over-render-props.md",
    "content": "---\ntitle: Prefer Composing Children Over Render Props\nimpact: MEDIUM\nimpactDescription: cleaner composition, better readability\ntags: composition, children, render-props\n---\n\n## Prefer Children Over Render Props\n\nUse `children` for composition instead of `renderX` props. Children are more\nreadable, compose naturally, and don't require understanding callback\nsignatures.\n\n**Incorrect (render props):**\n\n```tsx\nfunction Composer({\n  renderHeader,\n  renderFooter,\n  renderActions,\n}: {\n  renderHeader?: () => React.ReactNode\n  renderFooter?: () => React.ReactNode\n  renderActions?: () => React.ReactNode\n}) {\n  return (\n    <form>\n      {renderHeader?.()}\n      <Input />\n      {renderFooter ? renderFooter() : <DefaultFooter />}\n      {renderActions?.()}\n    </form>\n  )\n}\n\n// Usage is awkward and inflexible\nreturn (\n  <Composer\n    renderHeader={() => <CustomHeader />}\n    renderFooter={() => (\n      <>\n        <Formatting />\n        <Emojis />\n      </>\n    )}\n    renderActions={() => <SubmitButton />}\n  />\n)\n```\n\n**Correct (compound components with children):**\n\n```tsx\nfunction ComposerFrame({ children }: { children: React.ReactNode }) {\n  return <form>{children}</form>\n}\n\nfunction ComposerFooter({ children }: { children: React.ReactNode }) {\n  return <footer className='flex'>{children}</footer>\n}\n\n// Usage is flexible\nreturn (\n  <Composer.Frame>\n    <CustomHeader />\n    <Composer.Input />\n    <Composer.Footer>\n      <Composer.Formatting />\n      <Composer.Emojis />\n      <SubmitButton />\n    </Composer.Footer>\n  </Composer.Frame>\n)\n```\n\n**When render props are appropriate:**\n\n```tsx\n// Render props work well when you need to pass data back\n<List\n  data={items}\n  renderItem={({ item, index }) => <Item item={item} index={index} />}\n/>\n```\n\nUse render props when the parent needs to provide data or state to the child.\nUse children when composing static structure.\n"
  },
  {
    "path": "skills/composition-patterns/rules/patterns-explicit-variants.md",
    "content": "---\ntitle: Create Explicit Component Variants\nimpact: MEDIUM\nimpactDescription: self-documenting code, no hidden conditionals\ntags: composition, variants, architecture\n---\n\n## Create Explicit Component Variants\n\nInstead of one component with many boolean props, create explicit variant\ncomponents. Each variant composes the pieces it needs. The code documents\nitself.\n\n**Incorrect (one component, many modes):**\n\n```tsx\n// What does this component actually render?\n<Composer\n  isThread\n  isEditing={false}\n  channelId='abc'\n  showAttachments\n  showFormatting={false}\n/>\n```\n\n**Correct (explicit variants):**\n\n```tsx\n// Immediately clear what this renders\n<ThreadComposer channelId=\"abc\" />\n\n// Or\n<EditMessageComposer messageId=\"xyz\" />\n\n// Or\n<ForwardMessageComposer messageId=\"123\" />\n```\n\nEach implementation is unique, explicit and self-contained. Yet they can each\nuse shared parts.\n\n**Implementation:**\n\n```tsx\nfunction ThreadComposer({ channelId }: { channelId: string }) {\n  return (\n    <ThreadProvider channelId={channelId}>\n      <Composer.Frame>\n        <Composer.Input />\n        <AlsoSendToChannelField channelId={channelId} />\n        <Composer.Footer>\n          <Composer.Formatting />\n          <Composer.Emojis />\n          <Composer.Submit />\n        </Composer.Footer>\n      </Composer.Frame>\n    </ThreadProvider>\n  )\n}\n\nfunction EditMessageComposer({ messageId }: { messageId: string }) {\n  return (\n    <EditMessageProvider messageId={messageId}>\n      <Composer.Frame>\n        <Composer.Input />\n        <Composer.Footer>\n          <Composer.Formatting />\n          <Composer.Emojis />\n          <Composer.CancelEdit />\n          <Composer.SaveEdit />\n        </Composer.Footer>\n      </Composer.Frame>\n    </EditMessageProvider>\n  )\n}\n\nfunction ForwardMessageComposer({ messageId }: { messageId: string }) {\n  return (\n    <ForwardMessageProvider messageId={messageId}>\n      <Composer.Frame>\n        <Composer.Input placeholder=\"Add a message, if you'd like.\" />\n        <Composer.Footer>\n          <Composer.Formatting />\n          <Composer.Emojis />\n          <Composer.Mentions />\n        </Composer.Footer>\n      </Composer.Frame>\n    </ForwardMessageProvider>\n  )\n}\n```\n\nEach variant is explicit about:\n\n- What provider/state it uses\n- What UI elements it includes\n- What actions are available\n\nNo boolean prop combinations to reason about. No impossible states.\n"
  },
  {
    "path": "skills/composition-patterns/rules/react19-no-forwardref.md",
    "content": "---\ntitle: React 19 API Changes\nimpact: MEDIUM\nimpactDescription: cleaner component definitions and context usage\ntags: react19, refs, context, hooks\n---\n\n## React 19 API Changes\n\n> **⚠️ React 19+ only.** Skip this if you're on React 18 or earlier.\n\nIn React 19, `ref` is now a regular prop (no `forwardRef` wrapper needed), and `use()` replaces `useContext()`.\n\n**Incorrect (forwardRef in React 19):**\n\n```tsx\nconst ComposerInput = forwardRef<TextInput, Props>((props, ref) => {\n  return <TextInput ref={ref} {...props} />\n})\n```\n\n**Correct (ref as a regular prop):**\n\n```tsx\nfunction ComposerInput({ ref, ...props }: Props & { ref?: React.Ref<TextInput> }) {\n  return <TextInput ref={ref} {...props} />\n}\n```\n\n**Incorrect (useContext in React 19):**\n\n```tsx\nconst value = useContext(MyContext)\n```\n\n**Correct (use instead of useContext):**\n\n```tsx\nconst value = use(MyContext)\n```\n\n`use()` can also be called conditionally, unlike `useContext()`.\n"
  },
  {
    "path": "skills/composition-patterns/rules/state-context-interface.md",
    "content": "---\ntitle: Define Generic Context Interfaces for Dependency Injection\nimpact: HIGH\nimpactDescription: enables dependency-injectable state across use-cases\ntags: composition, context, state, typescript, dependency-injection\n---\n\n## Define Generic Context Interfaces for Dependency Injection\n\nDefine a **generic interface** for your component context with three parts:\n`state`, `actions`, and `meta`. This interface is a contract that any provider\ncan implement—enabling the same UI components to work with completely different\nstate implementations.\n\n**Core principle:** Lift state, compose internals, make state\ndependency-injectable.\n\n**Incorrect (UI coupled to specific state implementation):**\n\n```tsx\nfunction ComposerInput() {\n  // Tightly coupled to a specific hook\n  const { input, setInput } = useChannelComposerState()\n  return <TextInput value={input} onChangeText={setInput} />\n}\n```\n\n**Correct (generic interface enables dependency injection):**\n\n```tsx\n// Define a GENERIC interface that any provider can implement\ninterface ComposerState {\n  input: string\n  attachments: Attachment[]\n  isSubmitting: boolean\n}\n\ninterface ComposerActions {\n  update: (updater: (state: ComposerState) => ComposerState) => void\n  submit: () => void\n}\n\ninterface ComposerMeta {\n  inputRef: React.RefObject<TextInput>\n}\n\ninterface ComposerContextValue {\n  state: ComposerState\n  actions: ComposerActions\n  meta: ComposerMeta\n}\n\nconst ComposerContext = createContext<ComposerContextValue | null>(null)\n```\n\n**UI components consume the interface, not the implementation:**\n\n```tsx\nfunction ComposerInput() {\n  const {\n    state,\n    actions: { update },\n    meta,\n  } = use(ComposerContext)\n\n  // This component works with ANY provider that implements the interface\n  return (\n    <TextInput\n      ref={meta.inputRef}\n      value={state.input}\n      onChangeText={(text) => update((s) => ({ ...s, input: text }))}\n    />\n  )\n}\n```\n\n**Different providers implement the same interface:**\n\n```tsx\n// Provider A: Local state for ephemeral forms\nfunction ForwardMessageProvider({ children }: { children: React.ReactNode }) {\n  const [state, setState] = useState(initialState)\n  const inputRef = useRef(null)\n  const submit = useForwardMessage()\n\n  return (\n    <ComposerContext\n      value={{\n        state,\n        actions: { update: setState, submit },\n        meta: { inputRef },\n      }}\n    >\n      {children}\n    </ComposerContext>\n  )\n}\n\n// Provider B: Global synced state for channels\nfunction ChannelProvider({ channelId, children }: Props) {\n  const { state, update, submit } = useGlobalChannel(channelId)\n  const inputRef = useRef(null)\n\n  return (\n    <ComposerContext\n      value={{\n        state,\n        actions: { update, submit },\n        meta: { inputRef },\n      }}\n    >\n      {children}\n    </ComposerContext>\n  )\n}\n```\n\n**The same composed UI works with both:**\n\n```tsx\n// Works with ForwardMessageProvider (local state)\n<ForwardMessageProvider>\n  <Composer.Frame>\n    <Composer.Input />\n    <Composer.Submit />\n  </Composer.Frame>\n</ForwardMessageProvider>\n\n// Works with ChannelProvider (global synced state)\n<ChannelProvider channelId=\"abc\">\n  <Composer.Frame>\n    <Composer.Input />\n    <Composer.Submit />\n  </Composer.Frame>\n</ChannelProvider>\n```\n\n**Custom UI outside the component can access state and actions:**\n\nThe provider boundary is what matters—not the visual nesting. Components that\nneed shared state don't have to be inside the `Composer.Frame`. They just need\nto be within the provider.\n\n```tsx\nfunction ForwardMessageDialog() {\n  return (\n    <ForwardMessageProvider>\n      <Dialog>\n        {/* The composer UI */}\n        <Composer.Frame>\n          <Composer.Input placeholder=\"Add a message, if you'd like.\" />\n          <Composer.Footer>\n            <Composer.Formatting />\n            <Composer.Emojis />\n          </Composer.Footer>\n        </Composer.Frame>\n\n        {/* Custom UI OUTSIDE the composer, but INSIDE the provider */}\n        <MessagePreview />\n\n        {/* Actions at the bottom of the dialog */}\n        <DialogActions>\n          <CancelButton />\n          <ForwardButton />\n        </DialogActions>\n      </Dialog>\n    </ForwardMessageProvider>\n  )\n}\n\n// This button lives OUTSIDE Composer.Frame but can still submit based on its context!\nfunction ForwardButton() {\n  const {\n    actions: { submit },\n  } = use(ComposerContext)\n  return <Button onPress={submit}>Forward</Button>\n}\n\n// This preview lives OUTSIDE Composer.Frame but can read composer's state!\nfunction MessagePreview() {\n  const { state } = use(ComposerContext)\n  return <Preview message={state.input} attachments={state.attachments} />\n}\n```\n\nThe `ForwardButton` and `MessagePreview` are not visually inside the composer\nbox, but they can still access its state and actions. This is the power of\nlifting state into providers.\n\nThe UI is reusable bits you compose together. The state is dependency-injected\nby the provider. Swap the provider, keep the UI.\n"
  },
  {
    "path": "skills/composition-patterns/rules/state-decouple-implementation.md",
    "content": "---\ntitle: Decouple State Management from UI\nimpact: MEDIUM\nimpactDescription: enables swapping state implementations without changing UI\ntags: composition, state, architecture\n---\n\n## Decouple State Management from UI\n\nThe provider component should be the only place that knows how state is managed.\nUI components consume the context interface—they don't know if state comes from\nuseState, Zustand, or a server sync.\n\n**Incorrect (UI coupled to state implementation):**\n\n```tsx\nfunction ChannelComposer({ channelId }: { channelId: string }) {\n  // UI component knows about global state implementation\n  const state = useGlobalChannelState(channelId)\n  const { submit, updateInput } = useChannelSync(channelId)\n\n  return (\n    <Composer.Frame>\n      <Composer.Input\n        value={state.input}\n        onChange={(text) => sync.updateInput(text)}\n      />\n      <Composer.Submit onPress={() => sync.submit()} />\n    </Composer.Frame>\n  )\n}\n```\n\n**Correct (state management isolated in provider):**\n\n```tsx\n// Provider handles all state management details\nfunction ChannelProvider({\n  channelId,\n  children,\n}: {\n  channelId: string\n  children: React.ReactNode\n}) {\n  const { state, update, submit } = useGlobalChannel(channelId)\n  const inputRef = useRef(null)\n\n  return (\n    <Composer.Provider\n      state={state}\n      actions={{ update, submit }}\n      meta={{ inputRef }}\n    >\n      {children}\n    </Composer.Provider>\n  )\n}\n\n// UI component only knows about the context interface\nfunction ChannelComposer() {\n  return (\n    <Composer.Frame>\n      <Composer.Header />\n      <Composer.Input />\n      <Composer.Footer>\n        <Composer.Submit />\n      </Composer.Footer>\n    </Composer.Frame>\n  )\n}\n\n// Usage\nfunction Channel({ channelId }: { channelId: string }) {\n  return (\n    <ChannelProvider channelId={channelId}>\n      <ChannelComposer />\n    </ChannelProvider>\n  )\n}\n```\n\n**Different providers, same UI:**\n\n```tsx\n// Local state for ephemeral forms\nfunction ForwardMessageProvider({ children }) {\n  const [state, setState] = useState(initialState)\n  const forwardMessage = useForwardMessage()\n\n  return (\n    <Composer.Provider\n      state={state}\n      actions={{ update: setState, submit: forwardMessage }}\n    >\n      {children}\n    </Composer.Provider>\n  )\n}\n\n// Global synced state for channels\nfunction ChannelProvider({ channelId, children }) {\n  const { state, update, submit } = useGlobalChannel(channelId)\n\n  return (\n    <Composer.Provider state={state} actions={{ update, submit }}>\n      {children}\n    </Composer.Provider>\n  )\n}\n```\n\nThe same `Composer.Input` component works with both providers because it only\ndepends on the context interface, not the implementation.\n"
  },
  {
    "path": "skills/composition-patterns/rules/state-lift-state.md",
    "content": "---\ntitle: Lift State into Provider Components\nimpact: HIGH\nimpactDescription: enables state sharing outside component boundaries\ntags: composition, state, context, providers\n---\n\n## Lift State into Provider Components\n\nMove state management into dedicated provider components. This allows sibling\ncomponents outside the main UI to access and modify state without prop drilling\nor awkward refs.\n\n**Incorrect (state trapped inside component):**\n\n```tsx\nfunction ForwardMessageComposer() {\n  const [state, setState] = useState(initialState)\n  const forwardMessage = useForwardMessage()\n\n  return (\n    <Composer.Frame>\n      <Composer.Input />\n      <Composer.Footer />\n    </Composer.Frame>\n  )\n}\n\n// Problem: How does this button access composer state?\nfunction ForwardMessageDialog() {\n  return (\n    <Dialog>\n      <ForwardMessageComposer />\n      <MessagePreview /> {/* Needs composer state */}\n      <DialogActions>\n        <CancelButton />\n        <ForwardButton /> {/* Needs to call submit */}\n      </DialogActions>\n    </Dialog>\n  )\n}\n```\n\n**Incorrect (useEffect to sync state up):**\n\n```tsx\nfunction ForwardMessageDialog() {\n  const [input, setInput] = useState('')\n  return (\n    <Dialog>\n      <ForwardMessageComposer onInputChange={setInput} />\n      <MessagePreview input={input} />\n    </Dialog>\n  )\n}\n\nfunction ForwardMessageComposer({ onInputChange }) {\n  const [state, setState] = useState(initialState)\n  useEffect(() => {\n    onInputChange(state.input) // Sync on every change 😬\n  }, [state.input])\n}\n```\n\n**Incorrect (reading state from ref on submit):**\n\n```tsx\nfunction ForwardMessageDialog() {\n  const stateRef = useRef(null)\n  return (\n    <Dialog>\n      <ForwardMessageComposer stateRef={stateRef} />\n      <ForwardButton onPress={() => submit(stateRef.current)} />\n    </Dialog>\n  )\n}\n```\n\n**Correct (state lifted to provider):**\n\n```tsx\nfunction ForwardMessageProvider({ children }: { children: React.ReactNode }) {\n  const [state, setState] = useState(initialState)\n  const forwardMessage = useForwardMessage()\n  const inputRef = useRef(null)\n\n  return (\n    <Composer.Provider\n      state={state}\n      actions={{ update: setState, submit: forwardMessage }}\n      meta={{ inputRef }}\n    >\n      {children}\n    </Composer.Provider>\n  )\n}\n\nfunction ForwardMessageDialog() {\n  return (\n    <ForwardMessageProvider>\n      <Dialog>\n        <ForwardMessageComposer />\n        <MessagePreview /> {/* Custom components can access state and actions */}\n        <DialogActions>\n          <CancelButton />\n          <ForwardButton /> {/* Custom components can access state and actions */}\n        </DialogActions>\n      </Dialog>\n    </ForwardMessageProvider>\n  )\n}\n\nfunction ForwardButton() {\n  const { actions } = use(Composer.Context)\n  return <Button onPress={actions.submit}>Forward</Button>\n}\n```\n\nThe ForwardButton lives outside the Composer.Frame but still has access to the\nsubmit action because it's within the provider. Even though it's a one-off\ncomponent, it can still access the composer's state and actions from outside the\nUI itself.\n\n**Key insight:** Components that need shared state don't have to be visually\nnested inside each other—they just need to be within the same provider.\n"
  },
  {
    "path": "skills/deploy-to-vercel/SKILL.md",
    "content": "---\nname: deploy-to-vercel\ndescription: Deploy applications and websites to Vercel. Use when the user requests deployment actions like \"deploy my app\", \"deploy and give me the link\", \"push this live\", or \"create a preview deployment\".\nmetadata:\n  author: vercel\n  version: \"3.0.0\"\n---\n\n# Deploy to Vercel\n\nDeploy any project to Vercel. **Always deploy as preview** (not production) unless the user explicitly asks for production.\n\nThe goal is to get the user into the best long-term setup: their project linked to Vercel with git-push deploys. Every method below tries to move the user closer to that state.\n\n## Step 1: Gather Project State\n\nRun all four checks before deciding which method to use:\n\n```bash\n# 1. Check for a git remote\ngit remote get-url origin 2>/dev/null\n\n# 2. Check if locally linked to a Vercel project (either file means linked)\ncat .vercel/project.json 2>/dev/null || cat .vercel/repo.json 2>/dev/null\n\n# 3. Check if the Vercel CLI is installed and authenticated\nvercel whoami 2>/dev/null\n\n# 4. List available teams (if authenticated)\nvercel teams list --format json 2>/dev/null\n```\n\n### Team selection\n\nIf the user belongs to multiple teams, present all available team slugs as a bulleted list and ask which one to deploy to. Once the user picks a team, proceed immediately to the next step — do not ask for additional confirmation.\n\nPass the team slug via `--scope` on all subsequent CLI commands (`vercel deploy`, `vercel link`, `vercel inspect`, etc.):\n\n```bash\nvercel deploy [path] -y --no-wait --scope <team-slug>\n```\n\nIf the project is already linked (`.vercel/project.json` or `.vercel/repo.json` exists), the `orgId` in those files determines the team — no need to ask again. If there is only one team (or just a personal account), skip the prompt and use it directly.\n\n**About the `.vercel/` directory:** A linked project has either:\n- `.vercel/project.json` — created by `vercel link` (single project linking). Contains `projectId` and `orgId`.\n- `.vercel/repo.json` — created by `vercel link --repo` (repo-based linking). Contains `orgId`, `remoteName`, and a `projects` array mapping directories to Vercel project IDs.\n\nEither file means the project is linked. Check for both.\n\n**Do NOT** use `vercel project inspect`, `vercel ls`, or `vercel link` to detect state in an unlinked directory — without a `.vercel/` config, they will interactively prompt (or with `--yes`, silently link as a side-effect). Only `vercel whoami` is safe to run anywhere.\n\n## Step 2: Choose a Deploy Method\n\n### Linked (`.vercel/` exists) + has git remote → Git Push\n\nThis is the ideal state. The project is linked and has git integration.\n\n1. **Ask the user before pushing.** Never push without explicit approval:\n   ```\n   This project is connected to Vercel via git. I can commit and push to\n   trigger a deployment. Want me to proceed?\n   ```\n\n2. **Commit and push:**\n   ```bash\n   git add .\n   git commit -m \"deploy: <description of changes>\"\n   git push\n   ```\n   Vercel automatically builds from the push. Non-production branches get preview deployments; the production branch (usually `main`) gets a production deployment.\n\n3. **Retrieve the preview URL.** If the CLI is authenticated:\n   ```bash\n   sleep 5\n   vercel ls --format json\n   ```\n   The JSON output has a `deployments` array. Find the latest entry — its `url` field is the preview URL.\n\n   If the CLI is not authenticated, tell the user to check the Vercel dashboard or the commit status checks on their git provider for the preview URL.\n\n---\n\n### Linked (`.vercel/` exists) + no git remote → `vercel deploy`\n\nThe project is linked but there's no git repo. Deploy directly with the CLI.\n\n```bash\nvercel deploy [path] -y --no-wait\n```\n\nUse `--no-wait` so the CLI returns immediately with the deployment URL instead of blocking until the build finishes (builds can take a while). Then check on the deployment status with:\n\n```bash\nvercel inspect <deployment-url>\n```\n\nFor production deploys (only if user explicitly asks):\n```bash\nvercel deploy [path] --prod -y --no-wait\n```\n\n---\n\n### Not linked + CLI is authenticated → Link first, then deploy\n\nThe CLI is working but the project isn't linked yet. This is the opportunity to get the user into the best state.\n\n1. **Ask the user which team to deploy to.** Present the team slugs from Step 1 as a bulleted list. If there's only one team (or just a personal account), skip this step.\n\n2. **Once a team is selected, proceed directly to linking.** Tell the user what will happen but do not ask for separate confirmation:\n   ```\n   Linking this project to <team name> on Vercel. This will create a Vercel\n   project to deploy to and enable automatic deployments on future git pushes.\n   ```\n\n3. **If a git remote exists**, use repo-based linking with the selected team scope:\n   ```bash\n   vercel link --repo --scope <team-slug>\n   ```\n   This reads the git remote URL and matches it to existing Vercel projects that deploy from that repo. It creates `.vercel/repo.json`. This is much more reliable than `vercel link` (without `--repo`), which tries to match by directory name and often fails when the local folder and Vercel project are named differently.\n\n   **If there is no git remote**, fall back to standard linking:\n   ```bash\n   vercel link --scope <team-slug>\n   ```\n   This prompts the user to select or create a project. It creates `.vercel/project.json`.\n\n4. **Then deploy using the best available method:**\n   - If a git remote exists → commit and push (see git push method above)\n   - If no git remote → `vercel deploy [path] -y --no-wait --scope <team-slug>`, then `vercel inspect <url>` to check status\n\n---\n\n### Not linked + CLI not authenticated → Install, auth, link, deploy\n\nThe Vercel CLI isn't set up at all.\n\n1. **Install the CLI (if not already installed):**\n   ```bash\n   npm install -g vercel\n   ```\n\n2. **Authenticate:**\n   ```bash\n   vercel login\n   ```\n   The user completes auth in their browser. If running in a non-interactive environment where login is not possible, skip to the **no-auth fallback** below.\n\n3. **Ask which team to deploy to** — present team slugs from `vercel teams list --format json` as a bulleted list. If only one team / personal account, skip. Once selected, proceed immediately.\n\n4. **Link the project** with the selected team scope (use `--repo` if a git remote exists, plain `vercel link` otherwise):\n   ```bash\n   vercel link --repo --scope <team-slug>   # if git remote exists\n   vercel link --scope <team-slug>          # if no git remote\n   ```\n\n5. **Deploy** using the best available method (git push if remote exists, otherwise `vercel deploy -y --no-wait --scope <team-slug>`, then `vercel inspect <url>` to check status).\n\n---\n\n### No-Auth Fallback — claude.ai sandbox\n\n**When to use:** Last resort when the CLI can't be installed or authenticated in the claude.ai sandbox. This requires no authentication — it returns a **Preview URL** (live site) and a **Claim URL** (transfer to your Vercel account).\n\n```bash\nbash /mnt/skills/user/deploy-to-vercel/resources/deploy.sh [path]\n```\n\n**Arguments:**\n- `path` - Directory to deploy, or a `.tgz` file (defaults to current directory)\n\n**Examples:**\n```bash\n# Deploy current directory\nbash /mnt/skills/user/deploy-to-vercel/resources/deploy.sh\n\n# Deploy specific project\nbash /mnt/skills/user/deploy-to-vercel/resources/deploy.sh /path/to/project\n\n# Deploy existing tarball\nbash /mnt/skills/user/deploy-to-vercel/resources/deploy.sh /path/to/project.tgz\n```\n\nThe script auto-detects the framework from `package.json`, packages the project (excluding `node_modules`, `.git`, `.env`), uploads it, and waits for the build to complete.\n\n**Tell the user:** \"Your deployment is ready at [previewUrl]. Claim it at [claimUrl] to manage your deployment.\"\n\n---\n\n### No-Auth Fallback — Codex sandbox\n\n**When to use:** In the Codex sandbox where the CLI may not be authenticated. Codex runs in a sandboxed environment by default — try the CLI first, and fall back to the deploy script if auth fails.\n\n1. **Check whether the Vercel CLI is installed** (no escalation needed for this check):\n   ```bash\n   command -v vercel\n   ```\n\n2. **If `vercel` is installed**, try deploying with the CLI:\n   ```bash\n   vercel deploy [path] -y --no-wait\n   ```\n\n3. **If `vercel` is not installed, or the CLI fails with \"No existing credentials found\"**, use the fallback script:\n   ```bash\n   skill_dir=\"<path-to-skill>\"\n\n   # Deploy current directory\n   bash \"$skill_dir/resources/deploy-codex.sh\"\n\n   # Deploy specific project\n   bash \"$skill_dir/resources/deploy-codex.sh\" /path/to/project\n\n   # Deploy existing tarball\n   bash \"$skill_dir/resources/deploy-codex.sh\" /path/to/project.tgz\n   ```\n\nThe script handles framework detection, packaging, and deployment. It waits for the build to complete and returns JSON with `previewUrl` and `claimUrl`.\n\n**Tell the user:** \"Your deployment is ready at [previewUrl]. Claim it at [claimUrl] to manage your deployment.\"\n\n**Escalated network access:** Only escalate the actual deploy command if sandboxing blocks the network call (`sandbox_permissions=require_escalated`). Do **not** escalate the `command -v vercel` check.\n\n---\n\n## Agent-Specific Notes\n\n### Claude Code / terminal-based agents\n\nYou have full shell access. Do NOT use the `/mnt/skills/` path. Follow the decision flow above using the CLI directly.\n\nFor the no-auth fallback, run the deploy script from the skill's installed location:\n```bash\nbash ~/.claude/skills/deploy-to-vercel/resources/deploy.sh [path]\n```\nThe path may vary depending on where the user installed the skill.\n\n### Sandboxed environments (claude.ai)\n\nYou likely cannot run `vercel login` or `git push`. Go directly to the **no-auth fallback — claude.ai sandbox**.\n\n### Codex\n\nCodex runs in a sandbox. Check if the CLI is available first, then fall back to the deploy script. Go to the **no-auth fallback — Codex sandbox**.\n\n---\n\n## Output\n\nAlways show the user the deployment URL.\n\n- **Git push:** Use `vercel ls --format json` to find the preview URL. If the CLI isn't authenticated, tell the user to check the Vercel dashboard or commit status checks.\n- **CLI deploy:** Show the URL returned by `vercel deploy --no-wait`. Use `vercel inspect <url>` to check build status and report it to the user.\n- **No-auth fallback:** Show both the preview URL and the claim URL:\n  ```\n  Deployment successful!\n\n  Preview URL: https://my-app-abc123.vercel.app\n  Claim URL:   https://vercel.com/claim-deployment?code=...\n\n  View your site at the Preview URL.\n  To transfer this deployment to your Vercel account, visit the Claim URL.\n  ```\n\n**Do not** curl or fetch the deployed URL to verify it works. Just return the link.\n\n---\n\n## Troubleshooting\n\n### Network Egress Error (claude.ai)\n\nIf deployment fails due to network restrictions on claude.ai, tell the user:\n\n```\nDeployment failed due to network restrictions. To fix this:\n\n1. Go to https://claude.ai/settings/capabilities\n2. Add *.vercel.com to the allowed domains\n3. Try deploying again\n```\n\n### Escalated Network Access (Codex / sandboxed environments)\n\nIf deployment fails due to network issues (timeouts, DNS errors, connection resets) in a sandboxed environment, rerun with escalated permissions (`sandbox_permissions=require_escalated`). Do not escalate the initial CLI availability check — only escalate the actual deploy command.\n\nExample guidance to the user:\n```\nThe deploy needs escalated network access to deploy to Vercel. I can rerun\nthe command with escalated permissions — want me to proceed?\n```\n\n### CLI Auth Failure\n\nIf `vercel login` or `vercel deploy` fails with authentication errors, fall back to the no-auth deploy script (claude.ai or Codex variant, depending on the environment).\n"
  },
  {
    "path": "skills/deploy-to-vercel/resources/deploy-codex.sh",
    "content": "#!/bin/bash\n\n# Vercel Deployment Script for Codex (via claimable deploy endpoint)\n# Usage: ./deploy-codex.sh [project-path]\n# Returns: JSON with previewUrl, claimUrl, deploymentId, projectId\n\nset -euo pipefail\n\nDEPLOY_ENDPOINT=\"https://codex-deploy-skills.vercel.sh/api/deploy\"\n\n# Detect framework from package.json\ndetect_framework() {\n    local pkg_json=\"$1\"\n\n    if [ ! -f \"$pkg_json\" ]; then\n        echo \"null\"\n        return\n    fi\n\n    local content=$(cat \"$pkg_json\")\n\n    # Helper to check if a package exists in dependencies or devDependencies.\n    # Use exact matching by default, with a separate prefix matcher for scoped\n    # package families like \"@remix-run/\".\n    has_dep_exact() {\n        echo \"$content\" | grep -q \"\\\"$1\\\"\"\n    }\n\n    has_dep_prefix() {\n        echo \"$content\" | grep -q \"\\\"$1\"\n    }\n\n    # Order matters - check more specific frameworks first\n\n    # Blitz\n    if has_dep_exact \"blitz\"; then echo \"blitzjs\"; return; fi\n\n    # Next.js\n    if has_dep_exact \"next\"; then echo \"nextjs\"; return; fi\n\n    # Gatsby\n    if has_dep_exact \"gatsby\"; then echo \"gatsby\"; return; fi\n\n    # Remix\n    if has_dep_prefix \"@remix-run/\"; then echo \"remix\"; return; fi\n\n    # React Router (v7 framework mode)\n    if has_dep_prefix \"@react-router/\"; then echo \"react-router\"; return; fi\n\n    # TanStack Start\n    if has_dep_exact \"@tanstack/start\"; then echo \"tanstack-start\"; return; fi\n\n    # Astro\n    if has_dep_exact \"astro\"; then echo \"astro\"; return; fi\n\n    # Hydrogen (Shopify)\n    if has_dep_exact \"@shopify/hydrogen\"; then echo \"hydrogen\"; return; fi\n\n    # SvelteKit\n    if has_dep_exact \"@sveltejs/kit\"; then echo \"sveltekit-1\"; return; fi\n\n    # Svelte (standalone)\n    if has_dep_exact \"svelte\"; then echo \"svelte\"; return; fi\n\n    # Nuxt\n    if has_dep_exact \"nuxt\"; then echo \"nuxtjs\"; return; fi\n\n    # Vue with Vitepress\n    if has_dep_exact \"vitepress\"; then echo \"vitepress\"; return; fi\n\n    # Vue with Vuepress\n    if has_dep_exact \"vuepress\"; then echo \"vuepress\"; return; fi\n\n    # Gridsome\n    if has_dep_exact \"gridsome\"; then echo \"gridsome\"; return; fi\n\n    # SolidStart\n    if has_dep_exact \"@solidjs/start\"; then echo \"solidstart-1\"; return; fi\n\n    # Docusaurus\n    if has_dep_exact \"@docusaurus/core\"; then echo \"docusaurus-2\"; return; fi\n\n    # RedwoodJS\n    if has_dep_prefix \"@redwoodjs/\"; then echo \"redwoodjs\"; return; fi\n\n    # Hexo\n    if has_dep_exact \"hexo\"; then echo \"hexo\"; return; fi\n\n    # Eleventy\n    if has_dep_exact \"@11ty/eleventy\"; then echo \"eleventy\"; return; fi\n\n    # Angular / Ionic Angular\n    if has_dep_exact \"@ionic/angular\"; then echo \"ionic-angular\"; return; fi\n    if has_dep_exact \"@angular/core\"; then echo \"angular\"; return; fi\n\n    # Ionic React\n    if has_dep_exact \"@ionic/react\"; then echo \"ionic-react\"; return; fi\n\n    # Create React App\n    if has_dep_exact \"react-scripts\"; then echo \"create-react-app\"; return; fi\n\n    # Ember\n    if has_dep_exact \"ember-cli\" || has_dep_exact \"ember-source\"; then echo \"ember\"; return; fi\n\n    # Dojo\n    if has_dep_exact \"@dojo/framework\"; then echo \"dojo\"; return; fi\n\n    # Polymer\n    if has_dep_prefix \"@polymer/\"; then echo \"polymer\"; return; fi\n\n    # Preact\n    if has_dep_exact \"preact\"; then echo \"preact\"; return; fi\n\n    # Stencil\n    if has_dep_exact \"@stencil/core\"; then echo \"stencil\"; return; fi\n\n    # UmiJS\n    if has_dep_exact \"umi\"; then echo \"umijs\"; return; fi\n\n    # Sapper (legacy Svelte)\n    if has_dep_exact \"sapper\"; then echo \"sapper\"; return; fi\n\n    # Saber\n    if has_dep_exact \"saber\"; then echo \"saber\"; return; fi\n\n    # Sanity\n    if has_dep_exact \"sanity\"; then echo \"sanity-v3\"; return; fi\n    if has_dep_prefix \"@sanity/\"; then echo \"sanity\"; return; fi\n\n    # Storybook\n    if has_dep_prefix \"@storybook/\"; then echo \"storybook\"; return; fi\n\n    # NestJS\n    if has_dep_exact \"@nestjs/core\"; then echo \"nestjs\"; return; fi\n\n    # Elysia\n    if has_dep_exact \"elysia\"; then echo \"elysia\"; return; fi\n\n    # Hono\n    if has_dep_exact \"hono\"; then echo \"hono\"; return; fi\n\n    # Fastify\n    if has_dep_exact \"fastify\"; then echo \"fastify\"; return; fi\n\n    # h3\n    if has_dep_exact \"h3\"; then echo \"h3\"; return; fi\n\n    # Nitro\n    if has_dep_exact \"nitropack\"; then echo \"nitro\"; return; fi\n\n    # Express\n    if has_dep_exact \"express\"; then echo \"express\"; return; fi\n\n    # Vite (generic - check last among JS frameworks)\n    if has_dep_exact \"vite\"; then echo \"vite\"; return; fi\n\n    # Parcel\n    if has_dep_exact \"parcel\"; then echo \"parcel\"; return; fi\n\n    # No framework detected\n    echo \"null\"\n}\n\n# Parse arguments\nINPUT_PATH=\"${1:-.}\"\n\n# Create temp directory for packaging\nTEMP_DIR=$(mktemp -d)\nTARBALL=\"$TEMP_DIR/project.tgz\"\nSTAGING_DIR=\"$TEMP_DIR/staging\"\nCLEANUP_TEMP=true\n\ncleanup() {\n    if [ \"$CLEANUP_TEMP\" = true ]; then\n        rm -rf \"$TEMP_DIR\"\n    fi\n}\ntrap cleanup EXIT\n\necho \"Preparing deployment...\" >&2\n\n# Check if input is a .tgz file or a directory\nFRAMEWORK=\"null\"\n\nif [ -f \"$INPUT_PATH\" ] && [[ \"$INPUT_PATH\" == *.tgz ]]; then\n    # Input is already a tarball, use it directly\n    echo \"Using provided tarball...\" >&2\n    TARBALL=\"$INPUT_PATH\"\n    CLEANUP_TEMP=false\n    # Can't detect framework from tarball, leave as null\nelif [ -d \"$INPUT_PATH\" ]; then\n    # Input is a directory, need to tar it\n    PROJECT_PATH=$(cd \"$INPUT_PATH\" && pwd)\n\n    # Detect framework from package.json\n    FRAMEWORK=$(detect_framework \"$PROJECT_PATH/package.json\")\n\n    # Stage files into a temporary directory to avoid mutating the source tree.\n    mkdir -p \"$STAGING_DIR\"\n    echo \"Staging project files...\" >&2\n    tar -C \"$PROJECT_PATH\" \\\n        --exclude='node_modules' \\\n        --exclude='.git' \\\n        --exclude='.env' \\\n        --exclude='.env.*' \\\n        -cf - . | tar -C \"$STAGING_DIR\" -xf -\n\n    # Check if this is a static HTML project (no package.json)\n    if [ ! -f \"$PROJECT_PATH/package.json\" ]; then\n        # Find HTML files in root\n        HTML_FILES=$(find \"$STAGING_DIR\" -maxdepth 1 -name \"*.html\" -type f)\n        HTML_COUNT=$(printf '%s\\n' \"$HTML_FILES\" | sed '/^$/d' | wc -l | tr -d '[:space:]')\n\n        # If there's exactly one HTML file and it's not index.html, rename it\n        if [ \"$HTML_COUNT\" -eq 1 ]; then\n            HTML_FILE=$(echo \"$HTML_FILES\" | head -1)\n            BASENAME=$(basename \"$HTML_FILE\")\n            if [ \"$BASENAME\" != \"index.html\" ]; then\n                echo \"Renaming $BASENAME to index.html...\" >&2\n                mv \"$HTML_FILE\" \"$STAGING_DIR/index.html\"\n            fi\n        fi\n    fi\n\n    # Create tarball from the staging directory\n    echo \"Creating deployment package...\" >&2\n    tar -czf \"$TARBALL\" -C \"$STAGING_DIR\" .\nelse\n    echo \"Error: Input must be a directory or a .tgz file\" >&2\n    exit 1\nfi\n\nif [ \"$FRAMEWORK\" != \"null\" ]; then\n    echo \"Detected framework: $FRAMEWORK\" >&2\nfi\n\n# Deploy\necho \"Deploying...\" >&2\nRESPONSE=$(curl -s -X POST \"$DEPLOY_ENDPOINT\" -F \"file=@$TARBALL\" -F \"framework=$FRAMEWORK\")\n\n# Check for error in response\nif echo \"$RESPONSE\" | grep -q '\"error\"'; then\n    ERROR_MSG=$(echo \"$RESPONSE\" | grep -o '\"error\":\"[^\"]*\"' | cut -d'\"' -f4)\n    echo \"Error: $ERROR_MSG\" >&2\n    exit 1\nfi\n\n# Extract URLs from response\nPREVIEW_URL=$(echo \"$RESPONSE\" | grep -o '\"previewUrl\":\"[^\"]*\"' | cut -d'\"' -f4)\nCLAIM_URL=$(echo \"$RESPONSE\" | grep -o '\"claimUrl\":\"[^\"]*\"' | cut -d'\"' -f4)\n\nif [ -z \"$PREVIEW_URL\" ]; then\n    echo \"Error: Could not extract preview URL from response\" >&2\n    echo \"$RESPONSE\" >&2\n    exit 1\nfi\n\necho \"Deployment started. Waiting for build to complete...\" >&2\necho \"Preview URL: $PREVIEW_URL\" >&2\n\n# Poll the preview URL until it returns a non-5xx response (5xx = still building)\nMAX_ATTEMPTS=60  # 5 minutes max (60 * 5 seconds)\nATTEMPT=0\n\nwhile [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do\n    HTTP_STATUS=$(curl -s -o /dev/null -w \"%{http_code}\" \"$PREVIEW_URL\")\n\n    if [ \"$HTTP_STATUS\" -eq 200 ]; then\n        echo \"\" >&2\n        echo \"Deployment ready!\" >&2\n        break\n    elif [ \"$HTTP_STATUS\" -ge 500 ]; then\n        # 5xx means still building/deploying\n        echo \"Building... (attempt $((ATTEMPT + 1))/$MAX_ATTEMPTS)\" >&2\n        sleep 5\n        ATTEMPT=$((ATTEMPT + 1))\n    elif [ \"$HTTP_STATUS\" -ge 400 ] && [ \"$HTTP_STATUS\" -lt 500 ]; then\n        # 4xx might be an error or the app itself returns 4xx - it's responding\n        echo \"\" >&2\n        echo \"Deployment ready (returned $HTTP_STATUS)!\" >&2\n        break\n    else\n        # Any other status, assume it's ready\n        echo \"\" >&2\n        echo \"Deployment ready!\" >&2\n        break\n    fi\ndone\n\nif [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then\n    echo \"\" >&2\n    echo \"Warning: Timed out waiting for deployment, but it may still be building.\" >&2\nfi\n\necho \"\" >&2\necho \"Preview URL: $PREVIEW_URL\" >&2\necho \"Claim URL:   $CLAIM_URL\" >&2\necho \"\" >&2\n\n# Output JSON for programmatic use\necho \"$RESPONSE\"\n"
  },
  {
    "path": "skills/deploy-to-vercel/resources/deploy.sh",
    "content": "#!/bin/bash\n\n# Vercel Deployment Script (via claimable deploy endpoint)\n# Usage: ./deploy.sh [project-path]\n# Returns: JSON with previewUrl, claimUrl, deploymentId, projectId\n\nset -euo pipefail\n\nDEPLOY_ENDPOINT=\"https://claude-skills-deploy.vercel.com/api/deploy\"\n\n# Detect framework from package.json\ndetect_framework() {\n    local pkg_json=\"$1\"\n\n    if [ ! -f \"$pkg_json\" ]; then\n        echo \"null\"\n        return\n    fi\n\n    local content=$(cat \"$pkg_json\")\n\n    # Helper to check if a package exists in dependencies or devDependencies.\n    # Use exact matching by default, with a separate prefix matcher for scoped\n    # package families like \"@remix-run/\".\n    has_dep_exact() {\n        echo \"$content\" | grep -q \"\\\"$1\\\"\"\n    }\n\n    has_dep_prefix() {\n        echo \"$content\" | grep -q \"\\\"$1\"\n    }\n\n    # Order matters - check more specific frameworks first\n\n    # Blitz\n    if has_dep_exact \"blitz\"; then echo \"blitzjs\"; return; fi\n\n    # Next.js\n    if has_dep_exact \"next\"; then echo \"nextjs\"; return; fi\n\n    # Gatsby\n    if has_dep_exact \"gatsby\"; then echo \"gatsby\"; return; fi\n\n    # Remix\n    if has_dep_prefix \"@remix-run/\"; then echo \"remix\"; return; fi\n\n    # React Router (v7 framework mode)\n    if has_dep_prefix \"@react-router/\"; then echo \"react-router\"; return; fi\n\n    # TanStack Start\n    if has_dep_exact \"@tanstack/start\"; then echo \"tanstack-start\"; return; fi\n\n    # Astro\n    if has_dep_exact \"astro\"; then echo \"astro\"; return; fi\n\n    # Hydrogen (Shopify)\n    if has_dep_exact \"@shopify/hydrogen\"; then echo \"hydrogen\"; return; fi\n\n    # SvelteKit\n    if has_dep_exact \"@sveltejs/kit\"; then echo \"sveltekit-1\"; return; fi\n\n    # Svelte (standalone)\n    if has_dep_exact \"svelte\"; then echo \"svelte\"; return; fi\n\n    # Nuxt\n    if has_dep_exact \"nuxt\"; then echo \"nuxtjs\"; return; fi\n\n    # Vue with Vitepress\n    if has_dep_exact \"vitepress\"; then echo \"vitepress\"; return; fi\n\n    # Vue with Vuepress\n    if has_dep_exact \"vuepress\"; then echo \"vuepress\"; return; fi\n\n    # Gridsome\n    if has_dep_exact \"gridsome\"; then echo \"gridsome\"; return; fi\n\n    # SolidStart\n    if has_dep_exact \"@solidjs/start\"; then echo \"solidstart-1\"; return; fi\n\n    # Docusaurus\n    if has_dep_exact \"@docusaurus/core\"; then echo \"docusaurus-2\"; return; fi\n\n    # RedwoodJS\n    if has_dep_prefix \"@redwoodjs/\"; then echo \"redwoodjs\"; return; fi\n\n    # Hexo\n    if has_dep_exact \"hexo\"; then echo \"hexo\"; return; fi\n\n    # Eleventy\n    if has_dep_exact \"@11ty/eleventy\"; then echo \"eleventy\"; return; fi\n\n    # Angular / Ionic Angular\n    if has_dep_exact \"@ionic/angular\"; then echo \"ionic-angular\"; return; fi\n    if has_dep_exact \"@angular/core\"; then echo \"angular\"; return; fi\n\n    # Ionic React\n    if has_dep_exact \"@ionic/react\"; then echo \"ionic-react\"; return; fi\n\n    # Create React App\n    if has_dep_exact \"react-scripts\"; then echo \"create-react-app\"; return; fi\n\n    # Ember\n    if has_dep_exact \"ember-cli\" || has_dep_exact \"ember-source\"; then echo \"ember\"; return; fi\n\n    # Dojo\n    if has_dep_exact \"@dojo/framework\"; then echo \"dojo\"; return; fi\n\n    # Polymer\n    if has_dep_prefix \"@polymer/\"; then echo \"polymer\"; return; fi\n\n    # Preact\n    if has_dep_exact \"preact\"; then echo \"preact\"; return; fi\n\n    # Stencil\n    if has_dep_exact \"@stencil/core\"; then echo \"stencil\"; return; fi\n\n    # UmiJS\n    if has_dep_exact \"umi\"; then echo \"umijs\"; return; fi\n\n    # Sapper (legacy Svelte)\n    if has_dep_exact \"sapper\"; then echo \"sapper\"; return; fi\n\n    # Saber\n    if has_dep_exact \"saber\"; then echo \"saber\"; return; fi\n\n    # Sanity\n    if has_dep_exact \"sanity\"; then echo \"sanity-v3\"; return; fi\n    if has_dep_prefix \"@sanity/\"; then echo \"sanity\"; return; fi\n\n    # Storybook\n    if has_dep_prefix \"@storybook/\"; then echo \"storybook\"; return; fi\n\n    # NestJS\n    if has_dep_exact \"@nestjs/core\"; then echo \"nestjs\"; return; fi\n\n    # Elysia\n    if has_dep_exact \"elysia\"; then echo \"elysia\"; return; fi\n\n    # Hono\n    if has_dep_exact \"hono\"; then echo \"hono\"; return; fi\n\n    # Fastify\n    if has_dep_exact \"fastify\"; then echo \"fastify\"; return; fi\n\n    # h3\n    if has_dep_exact \"h3\"; then echo \"h3\"; return; fi\n\n    # Nitro\n    if has_dep_exact \"nitropack\"; then echo \"nitro\"; return; fi\n\n    # Express\n    if has_dep_exact \"express\"; then echo \"express\"; return; fi\n\n    # Vite (generic - check last among JS frameworks)\n    if has_dep_exact \"vite\"; then echo \"vite\"; return; fi\n\n    # Parcel\n    if has_dep_exact \"parcel\"; then echo \"parcel\"; return; fi\n\n    # No framework detected\n    echo \"null\"\n}\n\n# Parse arguments\nINPUT_PATH=\"${1:-.}\"\n\n# Create temp directory for packaging\nTEMP_DIR=$(mktemp -d)\nTARBALL=\"$TEMP_DIR/project.tgz\"\nSTAGING_DIR=\"$TEMP_DIR/staging\"\nCLEANUP_TEMP=true\n\ncleanup() {\n    if [ \"$CLEANUP_TEMP\" = true ]; then\n        rm -rf \"$TEMP_DIR\"\n    fi\n}\ntrap cleanup EXIT\n\necho \"Preparing deployment...\" >&2\n\n# Check if input is a .tgz file or a directory\nFRAMEWORK=\"null\"\n\nif [ -f \"$INPUT_PATH\" ] && [[ \"$INPUT_PATH\" == *.tgz ]]; then\n    # Input is already a tarball, use it directly\n    echo \"Using provided tarball...\" >&2\n    TARBALL=\"$INPUT_PATH\"\n    CLEANUP_TEMP=false\n    # Can't detect framework from tarball, leave as null\nelif [ -d \"$INPUT_PATH\" ]; then\n    # Input is a directory, need to tar it\n    PROJECT_PATH=$(cd \"$INPUT_PATH\" && pwd)\n\n    # Detect framework from package.json\n    FRAMEWORK=$(detect_framework \"$PROJECT_PATH/package.json\")\n\n    # Stage files into a temporary directory to avoid mutating the source tree.\n    mkdir -p \"$STAGING_DIR\"\n    echo \"Staging project files...\" >&2\n    tar -C \"$PROJECT_PATH\" \\\n        --exclude='node_modules' \\\n        --exclude='.git' \\\n        --exclude='.env' \\\n        --exclude='.env.*' \\\n        -cf - . | tar -C \"$STAGING_DIR\" -xf -\n\n    # Check if this is a static HTML project (no package.json)\n    if [ ! -f \"$PROJECT_PATH/package.json\" ]; then\n        # Find HTML files in root\n        HTML_FILES=$(find \"$STAGING_DIR\" -maxdepth 1 -name \"*.html\" -type f)\n        HTML_COUNT=$(printf '%s\\n' \"$HTML_FILES\" | sed '/^$/d' | wc -l | tr -d '[:space:]')\n\n        # If there's exactly one HTML file and it's not index.html, rename it\n        if [ \"$HTML_COUNT\" -eq 1 ]; then\n            HTML_FILE=$(echo \"$HTML_FILES\" | head -1)\n            BASENAME=$(basename \"$HTML_FILE\")\n            if [ \"$BASENAME\" != \"index.html\" ]; then\n                echo \"Renaming $BASENAME to index.html...\" >&2\n                mv \"$HTML_FILE\" \"$STAGING_DIR/index.html\"\n            fi\n        fi\n    fi\n\n    # Create tarball from the staging directory\n    echo \"Creating deployment package...\" >&2\n    tar -czf \"$TARBALL\" -C \"$STAGING_DIR\" .\nelse\n    echo \"Error: Input must be a directory or a .tgz file\" >&2\n    exit 1\nfi\n\nif [ \"$FRAMEWORK\" != \"null\" ]; then\n    echo \"Detected framework: $FRAMEWORK\" >&2\nfi\n\n# Deploy\necho \"Deploying...\" >&2\nRESPONSE=$(curl -s -X POST \"$DEPLOY_ENDPOINT\" -F \"file=@$TARBALL\" -F \"framework=$FRAMEWORK\")\n\n# Check for error in response\nif echo \"$RESPONSE\" | grep -q '\"error\"'; then\n    ERROR_MSG=$(echo \"$RESPONSE\" | grep -o '\"error\":\"[^\"]*\"' | cut -d'\"' -f4)\n    echo \"Error: $ERROR_MSG\" >&2\n    exit 1\nfi\n\n# Extract URLs from response\nPREVIEW_URL=$(echo \"$RESPONSE\" | grep -o '\"previewUrl\":\"[^\"]*\"' | cut -d'\"' -f4)\nCLAIM_URL=$(echo \"$RESPONSE\" | grep -o '\"claimUrl\":\"[^\"]*\"' | cut -d'\"' -f4)\n\nif [ -z \"$PREVIEW_URL\" ]; then\n    echo \"Error: Could not extract preview URL from response\" >&2\n    echo \"$RESPONSE\" >&2\n    exit 1\nfi\n\necho \"Deployment started. Waiting for build to complete...\" >&2\necho \"Preview URL: $PREVIEW_URL\" >&2\n\n# Poll the preview URL until it returns a non-5xx response (5xx = still building)\nMAX_ATTEMPTS=60  # 5 minutes max (60 * 5 seconds)\nATTEMPT=0\n\nwhile [ $ATTEMPT -lt $MAX_ATTEMPTS ]; do\n    HTTP_STATUS=$(curl -s -o /dev/null -w \"%{http_code}\" \"$PREVIEW_URL\")\n\n    if [ \"$HTTP_STATUS\" -eq 200 ]; then\n        echo \"\" >&2\n        echo \"Deployment ready!\" >&2\n        break\n    elif [ \"$HTTP_STATUS\" -ge 500 ]; then\n        # 5xx means still building/deploying\n        echo \"Building... (attempt $((ATTEMPT + 1))/$MAX_ATTEMPTS)\" >&2\n        sleep 5\n        ATTEMPT=$((ATTEMPT + 1))\n    elif [ \"$HTTP_STATUS\" -ge 400 ] && [ \"$HTTP_STATUS\" -lt 500 ]; then\n        # 4xx might be an error or the app itself returns 4xx - it's responding\n        echo \"\" >&2\n        echo \"Deployment ready (returned $HTTP_STATUS)!\" >&2\n        break\n    else\n        # Any other status, assume it's ready\n        echo \"\" >&2\n        echo \"Deployment ready!\" >&2\n        break\n    fi\ndone\n\nif [ $ATTEMPT -eq $MAX_ATTEMPTS ]; then\n    echo \"\" >&2\n    echo \"Warning: Timed out waiting for deployment, but it may still be building.\" >&2\nfi\n\necho \"\" >&2\necho \"Preview URL: $PREVIEW_URL\" >&2\necho \"Claim URL:   $CLAIM_URL\" >&2\necho \"\" >&2\n\n# Output JSON for programmatic use\necho \"$RESPONSE\"\n"
  },
  {
    "path": "skills/react-best-practices/AGENTS.md",
    "content": "# React Best Practices\n\n**Version 1.0.0**  \nVercel Engineering  \nJanuary 2026\n\n> **Note:**  \n> This document is mainly for agents and LLMs to follow when maintaining,  \n> generating, or refactoring React and Next.js codebases. Humans  \n> may also find it useful, but guidance here is optimized for automation  \n> and consistency by AI-assisted workflows.\n\n---\n\n## Abstract\n\nComprehensive performance optimization guide for React and Next.js applications, designed for AI agents and LLMs. Contains 40+ rules across 8 categories, prioritized by impact from critical (eliminating waterfalls, reducing bundle size) to incremental (advanced patterns). Each rule includes detailed explanations, real-world examples comparing incorrect vs. correct implementations, and specific impact metrics to guide automated refactoring and code generation.\n\n---\n\n## Table of Contents\n\n1. [Eliminating Waterfalls](#1-eliminating-waterfalls) — **CRITICAL**\n   - 1.1 [Defer Await Until Needed](#11-defer-await-until-needed)\n   - 1.2 [Dependency-Based Parallelization](#12-dependency-based-parallelization)\n   - 1.3 [Prevent Waterfall Chains in API Routes](#13-prevent-waterfall-chains-in-api-routes)\n   - 1.4 [Promise.all() for Independent Operations](#14-promiseall-for-independent-operations)\n   - 1.5 [Strategic Suspense Boundaries](#15-strategic-suspense-boundaries)\n2. [Bundle Size Optimization](#2-bundle-size-optimization) — **CRITICAL**\n   - 2.1 [Avoid Barrel File Imports](#21-avoid-barrel-file-imports)\n   - 2.2 [Conditional Module Loading](#22-conditional-module-loading)\n   - 2.3 [Defer Non-Critical Third-Party Libraries](#23-defer-non-critical-third-party-libraries)\n   - 2.4 [Dynamic Imports for Heavy Components](#24-dynamic-imports-for-heavy-components)\n   - 2.5 [Preload Based on User Intent](#25-preload-based-on-user-intent)\n3. [Server-Side Performance](#3-server-side-performance) — **HIGH**\n   - 3.1 [Authenticate Server Actions Like API Routes](#31-authenticate-server-actions-like-api-routes)\n   - 3.2 [Avoid Duplicate Serialization in RSC Props](#32-avoid-duplicate-serialization-in-rsc-props)\n   - 3.3 [Cross-Request LRU Caching](#33-cross-request-lru-caching)\n   - 3.4 [Hoist Static I/O to Module Level](#34-hoist-static-io-to-module-level)\n   - 3.5 [Minimize Serialization at RSC Boundaries](#35-minimize-serialization-at-rsc-boundaries)\n   - 3.6 [Parallel Data Fetching with Component Composition](#36-parallel-data-fetching-with-component-composition)\n   - 3.7 [Per-Request Deduplication with React.cache()](#37-per-request-deduplication-with-reactcache)\n   - 3.8 [Use after() for Non-Blocking Operations](#38-use-after-for-non-blocking-operations)\n4. [Client-Side Data Fetching](#4-client-side-data-fetching) — **MEDIUM-HIGH**\n   - 4.1 [Deduplicate Global Event Listeners](#41-deduplicate-global-event-listeners)\n   - 4.2 [Use Passive Event Listeners for Scrolling Performance](#42-use-passive-event-listeners-for-scrolling-performance)\n   - 4.3 [Use SWR for Automatic Deduplication](#43-use-swr-for-automatic-deduplication)\n   - 4.4 [Version and Minimize localStorage Data](#44-version-and-minimize-localstorage-data)\n5. [Re-render Optimization](#5-re-render-optimization) — **MEDIUM**\n   - 5.1 [Calculate Derived State During Rendering](#51-calculate-derived-state-during-rendering)\n   - 5.2 [Defer State Reads to Usage Point](#52-defer-state-reads-to-usage-point)\n   - 5.3 [Do not wrap a simple expression with a primitive result type in useMemo](#53-do-not-wrap-a-simple-expression-with-a-primitive-result-type-in-usememo)\n   - 5.4 [Don't Define Components Inside Components](#54-dont-define-components-inside-components)\n   - 5.5 [Extract Default Non-primitive Parameter Value from Memoized Component to Constant](#55-extract-default-non-primitive-parameter-value-from-memoized-component-to-constant)\n   - 5.6 [Extract to Memoized Components](#56-extract-to-memoized-components)\n   - 5.7 [Narrow Effect Dependencies](#57-narrow-effect-dependencies)\n   - 5.8 [Put Interaction Logic in Event Handlers](#58-put-interaction-logic-in-event-handlers)\n   - 5.9 [Split Combined Hook Computations](#59-split-combined-hook-computations)\n   - 5.10 [Subscribe to Derived State](#510-subscribe-to-derived-state)\n   - 5.11 [Use Functional setState Updates](#511-use-functional-setstate-updates)\n   - 5.12 [Use Lazy State Initialization](#512-use-lazy-state-initialization)\n   - 5.13 [Use Transitions for Non-Urgent Updates](#513-use-transitions-for-non-urgent-updates)\n   - 5.14 [Use useDeferredValue for Expensive Derived Renders](#514-use-usedeferredvalue-for-expensive-derived-renders)\n   - 5.15 [Use useRef for Transient Values](#515-use-useref-for-transient-values)\n6. [Rendering Performance](#6-rendering-performance) — **MEDIUM**\n   - 6.1 [Animate SVG Wrapper Instead of SVG Element](#61-animate-svg-wrapper-instead-of-svg-element)\n   - 6.2 [CSS content-visibility for Long Lists](#62-css-content-visibility-for-long-lists)\n   - 6.3 [Hoist Static JSX Elements](#63-hoist-static-jsx-elements)\n   - 6.4 [Optimize SVG Precision](#64-optimize-svg-precision)\n   - 6.5 [Prevent Hydration Mismatch Without Flickering](#65-prevent-hydration-mismatch-without-flickering)\n   - 6.6 [Suppress Expected Hydration Mismatches](#66-suppress-expected-hydration-mismatches)\n   - 6.7 [Use Activity Component for Show/Hide](#67-use-activity-component-for-showhide)\n   - 6.8 [Use defer or async on Script Tags](#68-use-defer-or-async-on-script-tags)\n   - 6.9 [Use Explicit Conditional Rendering](#69-use-explicit-conditional-rendering)\n   - 6.10 [Use React DOM Resource Hints](#610-use-react-dom-resource-hints)\n   - 6.11 [Use useTransition Over Manual Loading States](#611-use-usetransition-over-manual-loading-states)\n7. [JavaScript Performance](#7-javascript-performance) — **LOW-MEDIUM**\n   - 7.1 [Avoid Layout Thrashing](#71-avoid-layout-thrashing)\n   - 7.2 [Build Index Maps for Repeated Lookups](#72-build-index-maps-for-repeated-lookups)\n   - 7.3 [Cache Property Access in Loops](#73-cache-property-access-in-loops)\n   - 7.4 [Cache Repeated Function Calls](#74-cache-repeated-function-calls)\n   - 7.5 [Cache Storage API Calls](#75-cache-storage-api-calls)\n   - 7.6 [Combine Multiple Array Iterations](#76-combine-multiple-array-iterations)\n   - 7.7 [Early Length Check for Array Comparisons](#77-early-length-check-for-array-comparisons)\n   - 7.8 [Early Return from Functions](#78-early-return-from-functions)\n   - 7.9 [Hoist RegExp Creation](#79-hoist-regexp-creation)\n   - 7.10 [Use flatMap to Map and Filter in One Pass](#710-use-flatmap-to-map-and-filter-in-one-pass)\n   - 7.11 [Use Loop for Min/Max Instead of Sort](#711-use-loop-for-minmax-instead-of-sort)\n   - 7.12 [Use Set/Map for O(1) Lookups](#712-use-setmap-for-o1-lookups)\n   - 7.13 [Use toSorted() Instead of sort() for Immutability](#713-use-tosorted-instead-of-sort-for-immutability)\n8. [Advanced Patterns](#8-advanced-patterns) — **LOW**\n   - 8.1 [Initialize App Once, Not Per Mount](#81-initialize-app-once-not-per-mount)\n   - 8.2 [Store Event Handlers in Refs](#82-store-event-handlers-in-refs)\n   - 8.3 [useEffectEvent for Stable Callback Refs](#83-useeffectevent-for-stable-callback-refs)\n\n---\n\n## 1. Eliminating Waterfalls\n\n**Impact: CRITICAL**\n\nWaterfalls are the #1 performance killer. Each sequential await adds full network latency. Eliminating them yields the largest gains.\n\n### 1.1 Defer Await Until Needed\n\n**Impact: HIGH (avoids blocking unused code paths)**\n\nMove `await` operations into the branches where they're actually used to avoid blocking code paths that don't need them.\n\n**Incorrect: blocks both branches**\n\n```typescript\nasync function handleRequest(userId: string, skipProcessing: boolean) {\n  const userData = await fetchUserData(userId)\n  \n  if (skipProcessing) {\n    // Returns immediately but still waited for userData\n    return { skipped: true }\n  }\n  \n  // Only this branch uses userData\n  return processUserData(userData)\n}\n```\n\n**Correct: only blocks when needed**\n\n```typescript\nasync function handleRequest(userId: string, skipProcessing: boolean) {\n  if (skipProcessing) {\n    // Returns immediately without waiting\n    return { skipped: true }\n  }\n  \n  // Fetch only when needed\n  const userData = await fetchUserData(userId)\n  return processUserData(userData)\n}\n```\n\n**Another example: early return optimization**\n\n```typescript\n// Incorrect: always fetches permissions\nasync function updateResource(resourceId: string, userId: string) {\n  const permissions = await fetchPermissions(userId)\n  const resource = await getResource(resourceId)\n  \n  if (!resource) {\n    return { error: 'Not found' }\n  }\n  \n  if (!permissions.canEdit) {\n    return { error: 'Forbidden' }\n  }\n  \n  return await updateResourceData(resource, permissions)\n}\n\n// Correct: fetches only when needed\nasync function updateResource(resourceId: string, userId: string) {\n  const resource = await getResource(resourceId)\n  \n  if (!resource) {\n    return { error: 'Not found' }\n  }\n  \n  const permissions = await fetchPermissions(userId)\n  \n  if (!permissions.canEdit) {\n    return { error: 'Forbidden' }\n  }\n  \n  return await updateResourceData(resource, permissions)\n}\n```\n\nThis optimization is especially valuable when the skipped branch is frequently taken, or when the deferred operation is expensive.\n\n### 1.2 Dependency-Based Parallelization\n\n**Impact: CRITICAL (2-10× improvement)**\n\nFor operations with partial dependencies, use `better-all` to maximize parallelism. It automatically starts each task at the earliest possible moment.\n\n**Incorrect: profile waits for config unnecessarily**\n\n```typescript\nconst [user, config] = await Promise.all([\n  fetchUser(),\n  fetchConfig()\n])\nconst profile = await fetchProfile(user.id)\n```\n\n**Correct: config and profile run in parallel**\n\n```typescript\nimport { all } from 'better-all'\n\nconst { user, config, profile } = await all({\n  async user() { return fetchUser() },\n  async config() { return fetchConfig() },\n  async profile() {\n    return fetchProfile((await this.$.user).id)\n  }\n})\n```\n\n**Alternative without extra dependencies:**\n\n```typescript\nconst userPromise = fetchUser()\nconst profilePromise = userPromise.then(user => fetchProfile(user.id))\n\nconst [user, config, profile] = await Promise.all([\n  userPromise,\n  fetchConfig(),\n  profilePromise\n])\n```\n\nWe can also create all the promises first, and do `Promise.all()` at the end.\n\nReference: [https://github.com/shuding/better-all](https://github.com/shuding/better-all)\n\n### 1.3 Prevent Waterfall Chains in API Routes\n\n**Impact: CRITICAL (2-10× improvement)**\n\nIn API routes and Server Actions, start independent operations immediately, even if you don't await them yet.\n\n**Incorrect: config waits for auth, data waits for both**\n\n```typescript\nexport async function GET(request: Request) {\n  const session = await auth()\n  const config = await fetchConfig()\n  const data = await fetchData(session.user.id)\n  return Response.json({ data, config })\n}\n```\n\n**Correct: auth and config start immediately**\n\n```typescript\nexport async function GET(request: Request) {\n  const sessionPromise = auth()\n  const configPromise = fetchConfig()\n  const session = await sessionPromise\n  const [config, data] = await Promise.all([\n    configPromise,\n    fetchData(session.user.id)\n  ])\n  return Response.json({ data, config })\n}\n```\n\nFor operations with more complex dependency chains, use `better-all` to automatically maximize parallelism (see Dependency-Based Parallelization).\n\n### 1.4 Promise.all() for Independent Operations\n\n**Impact: CRITICAL (2-10× improvement)**\n\nWhen async operations have no interdependencies, execute them concurrently using `Promise.all()`.\n\n**Incorrect: sequential execution, 3 round trips**\n\n```typescript\nconst user = await fetchUser()\nconst posts = await fetchPosts()\nconst comments = await fetchComments()\n```\n\n**Correct: parallel execution, 1 round trip**\n\n```typescript\nconst [user, posts, comments] = await Promise.all([\n  fetchUser(),\n  fetchPosts(),\n  fetchComments()\n])\n```\n\n### 1.5 Strategic Suspense Boundaries\n\n**Impact: HIGH (faster initial paint)**\n\nInstead of awaiting data in async components before returning JSX, use Suspense boundaries to show the wrapper UI faster while data loads.\n\n**Incorrect: wrapper blocked by data fetching**\n\n```tsx\nasync function Page() {\n  const data = await fetchData() // Blocks entire page\n  \n  return (\n    <div>\n      <div>Sidebar</div>\n      <div>Header</div>\n      <div>\n        <DataDisplay data={data} />\n      </div>\n      <div>Footer</div>\n    </div>\n  )\n}\n```\n\nThe entire layout waits for data even though only the middle section needs it.\n\n**Correct: wrapper shows immediately, data streams in**\n\n```tsx\nfunction Page() {\n  return (\n    <div>\n      <div>Sidebar</div>\n      <div>Header</div>\n      <div>\n        <Suspense fallback={<Skeleton />}>\n          <DataDisplay />\n        </Suspense>\n      </div>\n      <div>Footer</div>\n    </div>\n  )\n}\n\nasync function DataDisplay() {\n  const data = await fetchData() // Only blocks this component\n  return <div>{data.content}</div>\n}\n```\n\nSidebar, Header, and Footer render immediately. Only DataDisplay waits for data.\n\n**Alternative: share promise across components**\n\n```tsx\nfunction Page() {\n  // Start fetch immediately, but don't await\n  const dataPromise = fetchData()\n  \n  return (\n    <div>\n      <div>Sidebar</div>\n      <div>Header</div>\n      <Suspense fallback={<Skeleton />}>\n        <DataDisplay dataPromise={dataPromise} />\n        <DataSummary dataPromise={dataPromise} />\n      </Suspense>\n      <div>Footer</div>\n    </div>\n  )\n}\n\nfunction DataDisplay({ dataPromise }: { dataPromise: Promise<Data> }) {\n  const data = use(dataPromise) // Unwraps the promise\n  return <div>{data.content}</div>\n}\n\nfunction DataSummary({ dataPromise }: { dataPromise: Promise<Data> }) {\n  const data = use(dataPromise) // Reuses the same promise\n  return <div>{data.summary}</div>\n}\n```\n\nBoth components share the same promise, so only one fetch occurs. Layout renders immediately while both components wait together.\n\n**When NOT to use this pattern:**\n\n- Critical data needed for layout decisions (affects positioning)\n\n- SEO-critical content above the fold\n\n- Small, fast queries where suspense overhead isn't worth it\n\n- When you want to avoid layout shift (loading → content jump)\n\n**Trade-off:** Faster initial paint vs potential layout shift. Choose based on your UX priorities.\n\n---\n\n## 2. Bundle Size Optimization\n\n**Impact: CRITICAL**\n\nReducing initial bundle size improves Time to Interactive and Largest Contentful Paint.\n\n### 2.1 Avoid Barrel File Imports\n\n**Impact: CRITICAL (200-800ms import cost, slow builds)**\n\nImport directly from source files instead of barrel files to avoid loading thousands of unused modules. **Barrel files** are entry points that re-export multiple modules (e.g., `index.js` that does `export * from './module'`).\n\nPopular icon and component libraries can have **up to 10,000 re-exports** in their entry file. For many React packages, **it takes 200-800ms just to import them**, affecting both development speed and production cold starts.\n\n**Why tree-shaking doesn't help:** When a library is marked as external (not bundled), the bundler can't optimize it. If you bundle it to enable tree-shaking, builds become substantially slower analyzing the entire module graph.\n\n**Incorrect: imports entire library**\n\n```tsx\nimport { Check, X, Menu } from 'lucide-react'\n// Loads 1,583 modules, takes ~2.8s extra in dev\n// Runtime cost: 200-800ms on every cold start\n\nimport { Button, TextField } from '@mui/material'\n// Loads 2,225 modules, takes ~4.2s extra in dev\n```\n\n**Correct: imports only what you need**\n\n```tsx\nimport Check from 'lucide-react/dist/esm/icons/check'\nimport X from 'lucide-react/dist/esm/icons/x'\nimport Menu from 'lucide-react/dist/esm/icons/menu'\n// Loads only 3 modules (~2KB vs ~1MB)\n\nimport Button from '@mui/material/Button'\nimport TextField from '@mui/material/TextField'\n// Loads only what you use\n```\n\n**Alternative: Next.js 13.5+**\n\n```js\n// next.config.js - use optimizePackageImports\nmodule.exports = {\n  experimental: {\n    optimizePackageImports: ['lucide-react', '@mui/material']\n  }\n}\n\n// Then you can keep the ergonomic barrel imports:\nimport { Check, X, Menu } from 'lucide-react'\n// Automatically transformed to direct imports at build time\n```\n\nDirect imports provide 15-70% faster dev boot, 28% faster builds, 40% faster cold starts, and significantly faster HMR.\n\nLibraries commonly affected: `lucide-react`, `@mui/material`, `@mui/icons-material`, `@tabler/icons-react`, `react-icons`, `@headlessui/react`, `@radix-ui/react-*`, `lodash`, `ramda`, `date-fns`, `rxjs`, `react-use`.\n\nReference: [https://vercel.com/blog/how-we-optimized-package-imports-in-next-js](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js)\n\n### 2.2 Conditional Module Loading\n\n**Impact: HIGH (loads large data only when needed)**\n\nLoad large data or modules only when a feature is activated.\n\n**Example: lazy-load animation frames**\n\n```tsx\nfunction AnimationPlayer({ enabled, setEnabled }: { enabled: boolean; setEnabled: React.Dispatch<React.SetStateAction<boolean>> }) {\n  const [frames, setFrames] = useState<Frame[] | null>(null)\n\n  useEffect(() => {\n    if (enabled && !frames && typeof window !== 'undefined') {\n      import('./animation-frames.js')\n        .then(mod => setFrames(mod.frames))\n        .catch(() => setEnabled(false))\n    }\n  }, [enabled, frames, setEnabled])\n\n  if (!frames) return <Skeleton />\n  return <Canvas frames={frames} />\n}\n```\n\nThe `typeof window !== 'undefined'` check prevents bundling this module for SSR, optimizing server bundle size and build speed.\n\n### 2.3 Defer Non-Critical Third-Party Libraries\n\n**Impact: MEDIUM (loads after hydration)**\n\nAnalytics, logging, and error tracking don't block user interaction. Load them after hydration.\n\n**Incorrect: blocks initial bundle**\n\n```tsx\nimport { Analytics } from '@vercel/analytics/react'\n\nexport default function RootLayout({ children }) {\n  return (\n    <html>\n      <body>\n        {children}\n        <Analytics />\n      </body>\n    </html>\n  )\n}\n```\n\n**Correct: loads after hydration**\n\n```tsx\nimport dynamic from 'next/dynamic'\n\nconst Analytics = dynamic(\n  () => import('@vercel/analytics/react').then(m => m.Analytics),\n  { ssr: false }\n)\n\nexport default function RootLayout({ children }) {\n  return (\n    <html>\n      <body>\n        {children}\n        <Analytics />\n      </body>\n    </html>\n  )\n}\n```\n\n### 2.4 Dynamic Imports for Heavy Components\n\n**Impact: CRITICAL (directly affects TTI and LCP)**\n\nUse `next/dynamic` to lazy-load large components not needed on initial render.\n\n**Incorrect: Monaco bundles with main chunk ~300KB**\n\n```tsx\nimport { MonacoEditor } from './monaco-editor'\n\nfunction CodePanel({ code }: { code: string }) {\n  return <MonacoEditor value={code} />\n}\n```\n\n**Correct: Monaco loads on demand**\n\n```tsx\nimport dynamic from 'next/dynamic'\n\nconst MonacoEditor = dynamic(\n  () => import('./monaco-editor').then(m => m.MonacoEditor),\n  { ssr: false }\n)\n\nfunction CodePanel({ code }: { code: string }) {\n  return <MonacoEditor value={code} />\n}\n```\n\n### 2.5 Preload Based on User Intent\n\n**Impact: MEDIUM (reduces perceived latency)**\n\nPreload heavy bundles before they're needed to reduce perceived latency.\n\n**Example: preload on hover/focus**\n\n```tsx\nfunction EditorButton({ onClick }: { onClick: () => void }) {\n  const preload = () => {\n    if (typeof window !== 'undefined') {\n      void import('./monaco-editor')\n    }\n  }\n\n  return (\n    <button\n      onMouseEnter={preload}\n      onFocus={preload}\n      onClick={onClick}\n    >\n      Open Editor\n    </button>\n  )\n}\n```\n\n**Example: preload when feature flag is enabled**\n\n```tsx\nfunction FlagsProvider({ children, flags }: Props) {\n  useEffect(() => {\n    if (flags.editorEnabled && typeof window !== 'undefined') {\n      void import('./monaco-editor').then(mod => mod.init())\n    }\n  }, [flags.editorEnabled])\n\n  return <FlagsContext.Provider value={flags}>\n    {children}\n  </FlagsContext.Provider>\n}\n```\n\nThe `typeof window !== 'undefined'` check prevents bundling preloaded modules for SSR, optimizing server bundle size and build speed.\n\n---\n\n## 3. Server-Side Performance\n\n**Impact: HIGH**\n\nOptimizing server-side rendering and data fetching eliminates server-side waterfalls and reduces response times.\n\n### 3.1 Authenticate Server Actions Like API Routes\n\n**Impact: CRITICAL (prevents unauthorized access to server mutations)**\n\nServer Actions (functions with `\"use server\"`) are exposed as public endpoints, just like API routes. Always verify authentication and authorization **inside** each Server Action—do not rely solely on middleware, layout guards, or page-level checks, as Server Actions can be invoked directly.\n\nNext.js documentation explicitly states: \"Treat Server Actions with the same security considerations as public-facing API endpoints, and verify if the user is allowed to perform a mutation.\"\n\n**Incorrect: no authentication check**\n\n```typescript\n'use server'\n\nexport async function deleteUser(userId: string) {\n  // Anyone can call this! No auth check\n  await db.user.delete({ where: { id: userId } })\n  return { success: true }\n}\n```\n\n**Correct: authentication inside the action**\n\n```typescript\n'use server'\n\nimport { verifySession } from '@/lib/auth'\nimport { unauthorized } from '@/lib/errors'\n\nexport async function deleteUser(userId: string) {\n  // Always check auth inside the action\n  const session = await verifySession()\n  \n  if (!session) {\n    throw unauthorized('Must be logged in')\n  }\n  \n  // Check authorization too\n  if (session.user.role !== 'admin' && session.user.id !== userId) {\n    throw unauthorized('Cannot delete other users')\n  }\n  \n  await db.user.delete({ where: { id: userId } })\n  return { success: true }\n}\n```\n\n**With input validation:**\n\n```typescript\n'use server'\n\nimport { verifySession } from '@/lib/auth'\nimport { z } from 'zod'\n\nconst updateProfileSchema = z.object({\n  userId: z.string().uuid(),\n  name: z.string().min(1).max(100),\n  email: z.string().email()\n})\n\nexport async function updateProfile(data: unknown) {\n  // Validate input first\n  const validated = updateProfileSchema.parse(data)\n  \n  // Then authenticate\n  const session = await verifySession()\n  if (!session) {\n    throw new Error('Unauthorized')\n  }\n  \n  // Then authorize\n  if (session.user.id !== validated.userId) {\n    throw new Error('Can only update own profile')\n  }\n  \n  // Finally perform the mutation\n  await db.user.update({\n    where: { id: validated.userId },\n    data: {\n      name: validated.name,\n      email: validated.email\n    }\n  })\n  \n  return { success: true }\n}\n```\n\nReference: [https://nextjs.org/docs/app/guides/authentication](https://nextjs.org/docs/app/guides/authentication)\n\n### 3.2 Avoid Duplicate Serialization in RSC Props\n\n**Impact: LOW (reduces network payload by avoiding duplicate serialization)**\n\nRSC→client serialization deduplicates by object reference, not value. Same reference = serialized once; new reference = serialized again. Do transformations (`.toSorted()`, `.filter()`, `.map()`) in client, not server.\n\n**Incorrect: duplicates array**\n\n```tsx\n// RSC: sends 6 strings (2 arrays × 3 items)\n<ClientList usernames={usernames} usernamesOrdered={usernames.toSorted()} />\n```\n\n**Correct: sends 3 strings**\n\n```tsx\n// RSC: send once\n<ClientList usernames={usernames} />\n\n// Client: transform there\n'use client'\nconst sorted = useMemo(() => [...usernames].sort(), [usernames])\n```\n\n**Nested deduplication behavior:**\n\n```tsx\n// string[] - duplicates everything\nusernames={['a','b']} sorted={usernames.toSorted()} // sends 4 strings\n\n// object[] - duplicates array structure only\nusers={[{id:1},{id:2}]} sorted={users.toSorted()} // sends 2 arrays + 2 unique objects (not 4)\n```\n\nDeduplication works recursively. Impact varies by data type:\n\n- `string[]`, `number[]`, `boolean[]`: **HIGH impact** - array + all primitives fully duplicated\n\n- `object[]`: **LOW impact** - array duplicated, but nested objects deduplicated by reference\n\n**Operations breaking deduplication: create new references**\n\n- Arrays: `.toSorted()`, `.filter()`, `.map()`, `.slice()`, `[...arr]`\n\n- Objects: `{...obj}`, `Object.assign()`, `structuredClone()`, `JSON.parse(JSON.stringify())`\n\n**More examples:**\n\n```tsx\n// ❌ Bad\n<C users={users} active={users.filter(u => u.active)} />\n<C product={product} productName={product.name} />\n\n// ✅ Good\n<C users={users} />\n<C product={product} />\n// Do filtering/destructuring in client\n```\n\n**Exception:** Pass derived data when transformation is expensive or client doesn't need original.\n\n### 3.3 Cross-Request LRU Caching\n\n**Impact: HIGH (caches across requests)**\n\n`React.cache()` only works within one request. For data shared across sequential requests (user clicks button A then button B), use an LRU cache.\n\n**Implementation:**\n\n```typescript\nimport { LRUCache } from 'lru-cache'\n\nconst cache = new LRUCache<string, any>({\n  max: 1000,\n  ttl: 5 * 60 * 1000  // 5 minutes\n})\n\nexport async function getUser(id: string) {\n  const cached = cache.get(id)\n  if (cached) return cached\n\n  const user = await db.user.findUnique({ where: { id } })\n  cache.set(id, user)\n  return user\n}\n\n// Request 1: DB query, result cached\n// Request 2: cache hit, no DB query\n```\n\nUse when sequential user actions hit multiple endpoints needing the same data within seconds.\n\n**With Vercel's [Fluid Compute](https://vercel.com/docs/fluid-compute):** LRU caching is especially effective because multiple concurrent requests can share the same function instance and cache. This means the cache persists across requests without needing external storage like Redis.\n\n**In traditional serverless:** Each invocation runs in isolation, so consider Redis for cross-process caching.\n\nReference: [https://github.com/isaacs/node-lru-cache](https://github.com/isaacs/node-lru-cache)\n\n### 3.4 Hoist Static I/O to Module Level\n\n**Impact: HIGH (avoids repeated file/network I/O per request)**\n\nWhen loading static assets (fonts, logos, images, config files) in route handlers or server functions, hoist the I/O operation to module level. Module-level code runs once when the module is first imported, not on every request. This eliminates redundant file system reads or network fetches that would otherwise run on every invocation.\n\n**Incorrect: reads font file on every request**\n\n**Correct: loads once at module initialization**\n\n**Alternative: synchronous file reads with Node.js fs**\n\n**General Node.js example: loading config or templates**\n\n**When to use this pattern:**\n\n- Loading fonts for OG image generation\n\n- Loading static logos, icons, or watermarks\n\n- Reading configuration files that don't change at runtime\n\n- Loading email templates or other static templates\n\n- Any static asset that's the same across all requests\n\n**When NOT to use this pattern:**\n\n- Assets that vary per request or user\n\n- Files that may change during runtime (use caching with TTL instead)\n\n- Large files that would consume too much memory if kept loaded\n\n- Sensitive data that shouldn't persist in memory\n\n**With Vercel's [Fluid Compute](https://vercel.com/docs/fluid-compute):** Module-level caching is especially effective because multiple concurrent requests share the same function instance. The static assets stay loaded in memory across requests without cold start penalties.\n\n**In traditional serverless:** Each cold start re-executes module-level code, but subsequent warm invocations reuse the loaded assets until the instance is recycled.\n\n### 3.5 Minimize Serialization at RSC Boundaries\n\n**Impact: HIGH (reduces data transfer size)**\n\nThe React Server/Client boundary serializes all object properties into strings and embeds them in the HTML response and subsequent RSC requests. This serialized data directly impacts page weight and load time, so **size matters a lot**. Only pass fields that the client actually uses.\n\n**Incorrect: serializes all 50 fields**\n\n```tsx\nasync function Page() {\n  const user = await fetchUser()  // 50 fields\n  return <Profile user={user} />\n}\n\n'use client'\nfunction Profile({ user }: { user: User }) {\n  return <div>{user.name}</div>  // uses 1 field\n}\n```\n\n**Correct: serializes only 1 field**\n\n```tsx\nasync function Page() {\n  const user = await fetchUser()\n  return <Profile name={user.name} />\n}\n\n'use client'\nfunction Profile({ name }: { name: string }) {\n  return <div>{name}</div>\n}\n```\n\n### 3.6 Parallel Data Fetching with Component Composition\n\n**Impact: CRITICAL (eliminates server-side waterfalls)**\n\nReact Server Components execute sequentially within a tree. Restructure with composition to parallelize data fetching.\n\n**Incorrect: Sidebar waits for Page's fetch to complete**\n\n```tsx\nexport default async function Page() {\n  const header = await fetchHeader()\n  return (\n    <div>\n      <div>{header}</div>\n      <Sidebar />\n    </div>\n  )\n}\n\nasync function Sidebar() {\n  const items = await fetchSidebarItems()\n  return <nav>{items.map(renderItem)}</nav>\n}\n```\n\n**Correct: both fetch simultaneously**\n\n```tsx\nasync function Header() {\n  const data = await fetchHeader()\n  return <div>{data}</div>\n}\n\nasync function Sidebar() {\n  const items = await fetchSidebarItems()\n  return <nav>{items.map(renderItem)}</nav>\n}\n\nexport default function Page() {\n  return (\n    <div>\n      <Header />\n      <Sidebar />\n    </div>\n  )\n}\n```\n\n**Alternative with children prop:**\n\n```tsx\nasync function Header() {\n  const data = await fetchHeader()\n  return <div>{data}</div>\n}\n\nasync function Sidebar() {\n  const items = await fetchSidebarItems()\n  return <nav>{items.map(renderItem)}</nav>\n}\n\nfunction Layout({ children }: { children: ReactNode }) {\n  return (\n    <div>\n      <Header />\n      {children}\n    </div>\n  )\n}\n\nexport default function Page() {\n  return (\n    <Layout>\n      <Sidebar />\n    </Layout>\n  )\n}\n```\n\n### 3.7 Per-Request Deduplication with React.cache()\n\n**Impact: MEDIUM (deduplicates within request)**\n\nUse `React.cache()` for server-side request deduplication. Authentication and database queries benefit most.\n\n**Usage:**\n\n```typescript\nimport { cache } from 'react'\n\nexport const getCurrentUser = cache(async () => {\n  const session = await auth()\n  if (!session?.user?.id) return null\n  return await db.user.findUnique({\n    where: { id: session.user.id }\n  })\n})\n```\n\nWithin a single request, multiple calls to `getCurrentUser()` execute the query only once.\n\n**Avoid inline objects as arguments:**\n\n`React.cache()` uses shallow equality (`Object.is`) to determine cache hits. Inline objects create new references each call, preventing cache hits.\n\n**Incorrect: always cache miss**\n\n```typescript\nconst getUser = cache(async (params: { uid: number }) => {\n  return await db.user.findUnique({ where: { id: params.uid } })\n})\n\n// Each call creates new object, never hits cache\ngetUser({ uid: 1 })\ngetUser({ uid: 1 })  // Cache miss, runs query again\n```\n\n**Correct: cache hit**\n\n```typescript\nconst params = { uid: 1 }\ngetUser(params)  // Query runs\ngetUser(params)  // Cache hit (same reference)\n```\n\nIf you must pass objects, pass the same reference:\n\n**Next.js-Specific Note:**\n\nIn Next.js, the `fetch` API is automatically extended with request memoization. Requests with the same URL and options are automatically deduplicated within a single request, so you don't need `React.cache()` for `fetch` calls. However, `React.cache()` is still essential for other async tasks:\n\n- Database queries (Prisma, Drizzle, etc.)\n\n- Heavy computations\n\n- Authentication checks\n\n- File system operations\n\n- Any non-fetch async work\n\nUse `React.cache()` to deduplicate these operations across your component tree.\n\nReference: [https://react.dev/reference/react/cache](https://react.dev/reference/react/cache)\n\n### 3.8 Use after() for Non-Blocking Operations\n\n**Impact: MEDIUM (faster response times)**\n\nUse Next.js's `after()` to schedule work that should execute after a response is sent. This prevents logging, analytics, and other side effects from blocking the response.\n\n**Incorrect: blocks response**\n\n```tsx\nimport { logUserAction } from '@/app/utils'\n\nexport async function POST(request: Request) {\n  // Perform mutation\n  await updateDatabase(request)\n  \n  // Logging blocks the response\n  const userAgent = request.headers.get('user-agent') || 'unknown'\n  await logUserAction({ userAgent })\n  \n  return new Response(JSON.stringify({ status: 'success' }), {\n    status: 200,\n    headers: { 'Content-Type': 'application/json' }\n  })\n}\n```\n\n**Correct: non-blocking**\n\n```tsx\nimport { after } from 'next/server'\nimport { headers, cookies } from 'next/headers'\nimport { logUserAction } from '@/app/utils'\n\nexport async function POST(request: Request) {\n  // Perform mutation\n  await updateDatabase(request)\n  \n  // Log after response is sent\n  after(async () => {\n    const userAgent = (await headers()).get('user-agent') || 'unknown'\n    const sessionCookie = (await cookies()).get('session-id')?.value || 'anonymous'\n    \n    logUserAction({ sessionCookie, userAgent })\n  })\n  \n  return new Response(JSON.stringify({ status: 'success' }), {\n    status: 200,\n    headers: { 'Content-Type': 'application/json' }\n  })\n}\n```\n\nThe response is sent immediately while logging happens in the background.\n\n**Common use cases:**\n\n- Analytics tracking\n\n- Audit logging\n\n- Sending notifications\n\n- Cache invalidation\n\n- Cleanup tasks\n\n**Important notes:**\n\n- `after()` runs even if the response fails or redirects\n\n- Works in Server Actions, Route Handlers, and Server Components\n\nReference: [https://nextjs.org/docs/app/api-reference/functions/after](https://nextjs.org/docs/app/api-reference/functions/after)\n\n---\n\n## 4. Client-Side Data Fetching\n\n**Impact: MEDIUM-HIGH**\n\nAutomatic deduplication and efficient data fetching patterns reduce redundant network requests.\n\n### 4.1 Deduplicate Global Event Listeners\n\n**Impact: LOW (single listener for N components)**\n\nUse `useSWRSubscription()` to share global event listeners across component instances.\n\n**Incorrect: N instances = N listeners**\n\n```tsx\nfunction useKeyboardShortcut(key: string, callback: () => void) {\n  useEffect(() => {\n    const handler = (e: KeyboardEvent) => {\n      if (e.metaKey && e.key === key) {\n        callback()\n      }\n    }\n    window.addEventListener('keydown', handler)\n    return () => window.removeEventListener('keydown', handler)\n  }, [key, callback])\n}\n```\n\nWhen using the `useKeyboardShortcut` hook multiple times, each instance will register a new listener.\n\n**Correct: N instances = 1 listener**\n\n```tsx\nimport useSWRSubscription from 'swr/subscription'\n\n// Module-level Map to track callbacks per key\nconst keyCallbacks = new Map<string, Set<() => void>>()\n\nfunction useKeyboardShortcut(key: string, callback: () => void) {\n  // Register this callback in the Map\n  useEffect(() => {\n    if (!keyCallbacks.has(key)) {\n      keyCallbacks.set(key, new Set())\n    }\n    keyCallbacks.get(key)!.add(callback)\n\n    return () => {\n      const set = keyCallbacks.get(key)\n      if (set) {\n        set.delete(callback)\n        if (set.size === 0) {\n          keyCallbacks.delete(key)\n        }\n      }\n    }\n  }, [key, callback])\n\n  useSWRSubscription('global-keydown', () => {\n    const handler = (e: KeyboardEvent) => {\n      if (e.metaKey && keyCallbacks.has(e.key)) {\n        keyCallbacks.get(e.key)!.forEach(cb => cb())\n      }\n    }\n    window.addEventListener('keydown', handler)\n    return () => window.removeEventListener('keydown', handler)\n  })\n}\n\nfunction Profile() {\n  // Multiple shortcuts will share the same listener\n  useKeyboardShortcut('p', () => { /* ... */ }) \n  useKeyboardShortcut('k', () => { /* ... */ })\n  // ...\n}\n```\n\n### 4.2 Use Passive Event Listeners for Scrolling Performance\n\n**Impact: MEDIUM (eliminates scroll delay caused by event listeners)**\n\nAdd `{ passive: true }` to touch and wheel event listeners to enable immediate scrolling. Browsers normally wait for listeners to finish to check if `preventDefault()` is called, causing scroll delay.\n\n**Incorrect:**\n\n```typescript\nuseEffect(() => {\n  const handleTouch = (e: TouchEvent) => console.log(e.touches[0].clientX)\n  const handleWheel = (e: WheelEvent) => console.log(e.deltaY)\n  \n  document.addEventListener('touchstart', handleTouch)\n  document.addEventListener('wheel', handleWheel)\n  \n  return () => {\n    document.removeEventListener('touchstart', handleTouch)\n    document.removeEventListener('wheel', handleWheel)\n  }\n}, [])\n```\n\n**Correct:**\n\n```typescript\nuseEffect(() => {\n  const handleTouch = (e: TouchEvent) => console.log(e.touches[0].clientX)\n  const handleWheel = (e: WheelEvent) => console.log(e.deltaY)\n  \n  document.addEventListener('touchstart', handleTouch, { passive: true })\n  document.addEventListener('wheel', handleWheel, { passive: true })\n  \n  return () => {\n    document.removeEventListener('touchstart', handleTouch)\n    document.removeEventListener('wheel', handleWheel)\n  }\n}, [])\n```\n\n**Use passive when:** tracking/analytics, logging, any listener that doesn't call `preventDefault()`.\n\n**Don't use passive when:** implementing custom swipe gestures, custom zoom controls, or any listener that needs `preventDefault()`.\n\n### 4.3 Use SWR for Automatic Deduplication\n\n**Impact: MEDIUM-HIGH (automatic deduplication)**\n\nSWR enables request deduplication, caching, and revalidation across component instances.\n\n**Incorrect: no deduplication, each instance fetches**\n\n```tsx\nfunction UserList() {\n  const [users, setUsers] = useState([])\n  useEffect(() => {\n    fetch('/api/users')\n      .then(r => r.json())\n      .then(setUsers)\n  }, [])\n}\n```\n\n**Correct: multiple instances share one request**\n\n```tsx\nimport useSWR from 'swr'\n\nfunction UserList() {\n  const { data: users } = useSWR('/api/users', fetcher)\n}\n```\n\n**For immutable data:**\n\n```tsx\nimport { useImmutableSWR } from '@/lib/swr'\n\nfunction StaticContent() {\n  const { data } = useImmutableSWR('/api/config', fetcher)\n}\n```\n\n**For mutations:**\n\n```tsx\nimport { useSWRMutation } from 'swr/mutation'\n\nfunction UpdateButton() {\n  const { trigger } = useSWRMutation('/api/user', updateUser)\n  return <button onClick={() => trigger()}>Update</button>\n}\n```\n\nReference: [https://swr.vercel.app](https://swr.vercel.app)\n\n### 4.4 Version and Minimize localStorage Data\n\n**Impact: MEDIUM (prevents schema conflicts, reduces storage size)**\n\nAdd version prefix to keys and store only needed fields. Prevents schema conflicts and accidental storage of sensitive data.\n\n**Incorrect:**\n\n```typescript\n// No version, stores everything, no error handling\nlocalStorage.setItem('userConfig', JSON.stringify(fullUserObject))\nconst data = localStorage.getItem('userConfig')\n```\n\n**Correct:**\n\n```typescript\nconst VERSION = 'v2'\n\nfunction saveConfig(config: { theme: string; language: string }) {\n  try {\n    localStorage.setItem(`userConfig:${VERSION}`, JSON.stringify(config))\n  } catch {\n    // Throws in incognito/private browsing, quota exceeded, or disabled\n  }\n}\n\nfunction loadConfig() {\n  try {\n    const data = localStorage.getItem(`userConfig:${VERSION}`)\n    return data ? JSON.parse(data) : null\n  } catch {\n    return null\n  }\n}\n\n// Migration from v1 to v2\nfunction migrate() {\n  try {\n    const v1 = localStorage.getItem('userConfig:v1')\n    if (v1) {\n      const old = JSON.parse(v1)\n      saveConfig({ theme: old.darkMode ? 'dark' : 'light', language: old.lang })\n      localStorage.removeItem('userConfig:v1')\n    }\n  } catch {}\n}\n```\n\n**Store minimal fields from server responses:**\n\n```typescript\n// User object has 20+ fields, only store what UI needs\nfunction cachePrefs(user: FullUser) {\n  try {\n    localStorage.setItem('prefs:v1', JSON.stringify({\n      theme: user.preferences.theme,\n      notifications: user.preferences.notifications\n    }))\n  } catch {}\n}\n```\n\n**Always wrap in try-catch:** `getItem()` and `setItem()` throw in incognito/private browsing (Safari, Firefox), when quota exceeded, or when disabled.\n\n**Benefits:** Schema evolution via versioning, reduced storage size, prevents storing tokens/PII/internal flags.\n\n---\n\n## 5. Re-render Optimization\n\n**Impact: MEDIUM**\n\nReducing unnecessary re-renders minimizes wasted computation and improves UI responsiveness.\n\n### 5.1 Calculate Derived State During Rendering\n\n**Impact: MEDIUM (avoids redundant renders and state drift)**\n\nIf a value can be computed from current props/state, do not store it in state or update it in an effect. Derive it during render to avoid extra renders and state drift. Do not set state in effects solely in response to prop changes; prefer derived values or keyed resets instead.\n\n**Incorrect: redundant state and effect**\n\n```tsx\nfunction Form() {\n  const [firstName, setFirstName] = useState('First')\n  const [lastName, setLastName] = useState('Last')\n  const [fullName, setFullName] = useState('')\n\n  useEffect(() => {\n    setFullName(firstName + ' ' + lastName)\n  }, [firstName, lastName])\n\n  return <p>{fullName}</p>\n}\n```\n\n**Correct: derive during render**\n\n```tsx\nfunction Form() {\n  const [firstName, setFirstName] = useState('First')\n  const [lastName, setLastName] = useState('Last')\n  const fullName = firstName + ' ' + lastName\n\n  return <p>{fullName}</p>\n}\n```\n\nReference: [https://react.dev/learn/you-might-not-need-an-effect](https://react.dev/learn/you-might-not-need-an-effect)\n\n### 5.2 Defer State Reads to Usage Point\n\n**Impact: MEDIUM (avoids unnecessary subscriptions)**\n\nDon't subscribe to dynamic state (searchParams, localStorage) if you only read it inside callbacks.\n\n**Incorrect: subscribes to all searchParams changes**\n\n```tsx\nfunction ShareButton({ chatId }: { chatId: string }) {\n  const searchParams = useSearchParams()\n\n  const handleShare = () => {\n    const ref = searchParams.get('ref')\n    shareChat(chatId, { ref })\n  }\n\n  return <button onClick={handleShare}>Share</button>\n}\n```\n\n**Correct: reads on demand, no subscription**\n\n```tsx\nfunction ShareButton({ chatId }: { chatId: string }) {\n  const handleShare = () => {\n    const params = new URLSearchParams(window.location.search)\n    const ref = params.get('ref')\n    shareChat(chatId, { ref })\n  }\n\n  return <button onClick={handleShare}>Share</button>\n}\n```\n\n### 5.3 Do not wrap a simple expression with a primitive result type in useMemo\n\n**Impact: LOW-MEDIUM (wasted computation on every render)**\n\nWhen an expression is simple (few logical or arithmetical operators) and has a primitive result type (boolean, number, string), do not wrap it in `useMemo`.\n\nCalling `useMemo` and comparing hook dependencies may consume more resources than the expression itself.\n\n**Incorrect:**\n\n```tsx\nfunction Header({ user, notifications }: Props) {\n  const isLoading = useMemo(() => {\n    return user.isLoading || notifications.isLoading\n  }, [user.isLoading, notifications.isLoading])\n\n  if (isLoading) return <Skeleton />\n  // return some markup\n}\n```\n\n**Correct:**\n\n```tsx\nfunction Header({ user, notifications }: Props) {\n  const isLoading = user.isLoading || notifications.isLoading\n\n  if (isLoading) return <Skeleton />\n  // return some markup\n}\n```\n\n### 5.4 Don't Define Components Inside Components\n\n**Impact: HIGH (prevents remount on every render)**\n\nDefining a component inside another component creates a new component type on every render. React sees a different component each time and fully remounts it, destroying all state and DOM.\n\nA common reason developers do this is to access parent variables without passing props. Always pass props instead.\n\n**Incorrect: remounts on every render**\n\n```tsx\nfunction UserProfile({ user, theme }) {\n  // Defined inside to access `theme` - BAD\n  const Avatar = () => (\n    <img\n      src={user.avatarUrl}\n      className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}\n    />\n  )\n\n  // Defined inside to access `user` - BAD\n  const Stats = () => (\n    <div>\n      <span>{user.followers} followers</span>\n      <span>{user.posts} posts</span>\n    </div>\n  )\n\n  return (\n    <div>\n      <Avatar />\n      <Stats />\n    </div>\n  )\n}\n```\n\nEvery time `UserProfile` renders, `Avatar` and `Stats` are new component types. React unmounts the old instances and mounts new ones, losing any internal state, running effects again, and recreating DOM nodes.\n\n**Correct: pass props instead**\n\n```tsx\nfunction Avatar({ src, theme }: { src: string; theme: string }) {\n  return (\n    <img\n      src={src}\n      className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}\n    />\n  )\n}\n\nfunction Stats({ followers, posts }: { followers: number; posts: number }) {\n  return (\n    <div>\n      <span>{followers} followers</span>\n      <span>{posts} posts</span>\n    </div>\n  )\n}\n\nfunction UserProfile({ user, theme }) {\n  return (\n    <div>\n      <Avatar src={user.avatarUrl} theme={theme} />\n      <Stats followers={user.followers} posts={user.posts} />\n    </div>\n  )\n}\n```\n\n**Symptoms of this bug:**\n\n- Input fields lose focus on every keystroke\n\n- Animations restart unexpectedly\n\n- `useEffect` cleanup/setup runs on every parent render\n\n- Scroll position resets inside the component\n\n### 5.5 Extract Default Non-primitive Parameter Value from Memoized Component to Constant\n\n**Impact: MEDIUM (restores memoization by using a constant for default value)**\n\nWhen memoized component has a default value for some non-primitive optional parameter, such as an array, function, or object, calling the component without that parameter results in broken memoization. This is because new value instances are created on every rerender, and they do not pass strict equality comparison in `memo()`.\n\nTo address this issue, extract the default value into a constant.\n\n**Incorrect: `onClick` has different values on every rerender**\n\n```tsx\nconst UserAvatar = memo(function UserAvatar({ onClick = () => {} }: { onClick?: () => void }) {\n  // ...\n})\n\n// Used without optional onClick\n<UserAvatar />\n```\n\n**Correct: stable default value**\n\n```tsx\nconst NOOP = () => {};\n\nconst UserAvatar = memo(function UserAvatar({ onClick = NOOP }: { onClick?: () => void }) {\n  // ...\n})\n\n// Used without optional onClick\n<UserAvatar />\n```\n\n### 5.6 Extract to Memoized Components\n\n**Impact: MEDIUM (enables early returns)**\n\nExtract expensive work into memoized components to enable early returns before computation.\n\n**Incorrect: computes avatar even when loading**\n\n```tsx\nfunction Profile({ user, loading }: Props) {\n  const avatar = useMemo(() => {\n    const id = computeAvatarId(user)\n    return <Avatar id={id} />\n  }, [user])\n\n  if (loading) return <Skeleton />\n  return <div>{avatar}</div>\n}\n```\n\n**Correct: skips computation when loading**\n\n```tsx\nconst UserAvatar = memo(function UserAvatar({ user }: { user: User }) {\n  const id = useMemo(() => computeAvatarId(user), [user])\n  return <Avatar id={id} />\n})\n\nfunction Profile({ user, loading }: Props) {\n  if (loading) return <Skeleton />\n  return (\n    <div>\n      <UserAvatar user={user} />\n    </div>\n  )\n}\n```\n\n**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, manual memoization with `memo()` and `useMemo()` is not necessary. The compiler automatically optimizes re-renders.\n\n### 5.7 Narrow Effect Dependencies\n\n**Impact: LOW (minimizes effect re-runs)**\n\nSpecify primitive dependencies instead of objects to minimize effect re-runs.\n\n**Incorrect: re-runs on any user field change**\n\n```tsx\nuseEffect(() => {\n  console.log(user.id)\n}, [user])\n```\n\n**Correct: re-runs only when id changes**\n\n```tsx\nuseEffect(() => {\n  console.log(user.id)\n}, [user.id])\n```\n\n**For derived state, compute outside effect:**\n\n```tsx\n// Incorrect: runs on width=767, 766, 765...\nuseEffect(() => {\n  if (width < 768) {\n    enableMobileMode()\n  }\n}, [width])\n\n// Correct: runs only on boolean transition\nconst isMobile = width < 768\nuseEffect(() => {\n  if (isMobile) {\n    enableMobileMode()\n  }\n}, [isMobile])\n```\n\n### 5.8 Put Interaction Logic in Event Handlers\n\n**Impact: MEDIUM (avoids effect re-runs and duplicate side effects)**\n\nIf a side effect is triggered by a specific user action (submit, click, drag), run it in that event handler. Do not model the action as state + effect; it makes effects re-run on unrelated changes and can duplicate the action.\n\n**Incorrect: event modeled as state + effect**\n\n```tsx\nfunction Form() {\n  const [submitted, setSubmitted] = useState(false)\n  const theme = useContext(ThemeContext)\n\n  useEffect(() => {\n    if (submitted) {\n      post('/api/register')\n      showToast('Registered', theme)\n    }\n  }, [submitted, theme])\n\n  return <button onClick={() => setSubmitted(true)}>Submit</button>\n}\n```\n\n**Correct: do it in the handler**\n\n```tsx\nfunction Form() {\n  const theme = useContext(ThemeContext)\n\n  function handleSubmit() {\n    post('/api/register')\n    showToast('Registered', theme)\n  }\n\n  return <button onClick={handleSubmit}>Submit</button>\n}\n```\n\nReference: [https://react.dev/learn/removing-effect-dependencies#should-this-code-move-to-an-event-handler](https://react.dev/learn/removing-effect-dependencies#should-this-code-move-to-an-event-handler)\n\n### 5.9 Split Combined Hook Computations\n\n**Impact: MEDIUM (avoids recomputing independent steps)**\n\nWhen a hook contains multiple independent tasks with different dependencies, split them into separate hooks. A combined hook reruns all tasks when any dependency changes, even if some tasks don't use the changed value.\n\n**Incorrect: changing `sortOrder` recomputes filtering**\n\n```tsx\nconst sortedProducts = useMemo(() => {\n  const filtered = products.filter((p) => p.category === category)\n  const sorted = filtered.toSorted((a, b) =>\n    sortOrder === \"asc\" ? a.price - b.price : b.price - a.price\n  )\n  return sorted\n}, [products, category, sortOrder])\n```\n\n**Correct: filtering only recomputes when products or category change**\n\n```tsx\nconst filteredProducts = useMemo(\n  () => products.filter((p) => p.category === category),\n  [products, category]\n)\n\nconst sortedProducts = useMemo(\n  () =>\n    filteredProducts.toSorted((a, b) =>\n      sortOrder === \"asc\" ? a.price - b.price : b.price - a.price\n    ),\n  [filteredProducts, sortOrder]\n)\n```\n\nThis pattern also applies to `useEffect` when combining unrelated side effects:\n\n**Incorrect: both effects run when either dependency changes**\n\n```tsx\nuseEffect(() => {\n  analytics.trackPageView(pathname)\n  document.title = `${pageTitle} | My App`\n}, [pathname, pageTitle])\n```\n\n**Correct: effects run independently**\n\n```tsx\nuseEffect(() => {\n  analytics.trackPageView(pathname)\n}, [pathname])\n\nuseEffect(() => {\n  document.title = `${pageTitle} | My App`\n}, [pageTitle])\n```\n\n**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, it automatically optimizes dependency tracking and may handle some of these cases for you.\n\n### 5.10 Subscribe to Derived State\n\n**Impact: MEDIUM (reduces re-render frequency)**\n\nSubscribe to derived boolean state instead of continuous values to reduce re-render frequency.\n\n**Incorrect: re-renders on every pixel change**\n\n```tsx\nfunction Sidebar() {\n  const width = useWindowWidth()  // updates continuously\n  const isMobile = width < 768\n  return <nav className={isMobile ? 'mobile' : 'desktop'} />\n}\n```\n\n**Correct: re-renders only when boolean changes**\n\n```tsx\nfunction Sidebar() {\n  const isMobile = useMediaQuery('(max-width: 767px)')\n  return <nav className={isMobile ? 'mobile' : 'desktop'} />\n}\n```\n\n### 5.11 Use Functional setState Updates\n\n**Impact: MEDIUM (prevents stale closures and unnecessary callback recreations)**\n\nWhen updating state based on the current state value, use the functional update form of setState instead of directly referencing the state variable. This prevents stale closures, eliminates unnecessary dependencies, and creates stable callback references.\n\n**Incorrect: requires state as dependency**\n\n```tsx\nfunction TodoList() {\n  const [items, setItems] = useState(initialItems)\n  \n  // Callback must depend on items, recreated on every items change\n  const addItems = useCallback((newItems: Item[]) => {\n    setItems([...items, ...newItems])\n  }, [items])  // ❌ items dependency causes recreations\n  \n  // Risk of stale closure if dependency is forgotten\n  const removeItem = useCallback((id: string) => {\n    setItems(items.filter(item => item.id !== id))\n  }, [])  // ❌ Missing items dependency - will use stale items!\n  \n  return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />\n}\n```\n\nThe first callback is recreated every time `items` changes, which can cause child components to re-render unnecessarily. The second callback has a stale closure bug—it will always reference the initial `items` value.\n\n**Correct: stable callbacks, no stale closures**\n\n```tsx\nfunction TodoList() {\n  const [items, setItems] = useState(initialItems)\n  \n  // Stable callback, never recreated\n  const addItems = useCallback((newItems: Item[]) => {\n    setItems(curr => [...curr, ...newItems])\n  }, [])  // ✅ No dependencies needed\n  \n  // Always uses latest state, no stale closure risk\n  const removeItem = useCallback((id: string) => {\n    setItems(curr => curr.filter(item => item.id !== id))\n  }, [])  // ✅ Safe and stable\n  \n  return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />\n}\n```\n\n**Benefits:**\n\n1. **Stable callback references** - Callbacks don't need to be recreated when state changes\n\n2. **No stale closures** - Always operates on the latest state value\n\n3. **Fewer dependencies** - Simplifies dependency arrays and reduces memory leaks\n\n4. **Prevents bugs** - Eliminates the most common source of React closure bugs\n\n**When to use functional updates:**\n\n- Any setState that depends on the current state value\n\n- Inside useCallback/useMemo when state is needed\n\n- Event handlers that reference state\n\n- Async operations that update state\n\n**When direct updates are fine:**\n\n- Setting state to a static value: `setCount(0)`\n\n- Setting state from props/arguments only: `setName(newName)`\n\n- State doesn't depend on previous value\n\n**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler can automatically optimize some cases, but functional updates are still recommended for correctness and to prevent stale closure bugs.\n\n### 5.12 Use Lazy State Initialization\n\n**Impact: MEDIUM (wasted computation on every render)**\n\nPass a function to `useState` for expensive initial values. Without the function form, the initializer runs on every render even though the value is only used once.\n\n**Incorrect: runs on every render**\n\n```tsx\nfunction FilteredList({ items }: { items: Item[] }) {\n  // buildSearchIndex() runs on EVERY render, even after initialization\n  const [searchIndex, setSearchIndex] = useState(buildSearchIndex(items))\n  const [query, setQuery] = useState('')\n  \n  // When query changes, buildSearchIndex runs again unnecessarily\n  return <SearchResults index={searchIndex} query={query} />\n}\n\nfunction UserProfile() {\n  // JSON.parse runs on every render\n  const [settings, setSettings] = useState(\n    JSON.parse(localStorage.getItem('settings') || '{}')\n  )\n  \n  return <SettingsForm settings={settings} onChange={setSettings} />\n}\n```\n\n**Correct: runs only once**\n\n```tsx\nfunction FilteredList({ items }: { items: Item[] }) {\n  // buildSearchIndex() runs ONLY on initial render\n  const [searchIndex, setSearchIndex] = useState(() => buildSearchIndex(items))\n  const [query, setQuery] = useState('')\n  \n  return <SearchResults index={searchIndex} query={query} />\n}\n\nfunction UserProfile() {\n  // JSON.parse runs only on initial render\n  const [settings, setSettings] = useState(() => {\n    const stored = localStorage.getItem('settings')\n    return stored ? JSON.parse(stored) : {}\n  })\n  \n  return <SettingsForm settings={settings} onChange={setSettings} />\n}\n```\n\nUse lazy initialization when computing initial values from localStorage/sessionStorage, building data structures (indexes, maps), reading from the DOM, or performing heavy transformations.\n\nFor simple primitives (`useState(0)`), direct references (`useState(props.value)`), or cheap literals (`useState({})`), the function form is unnecessary.\n\n### 5.13 Use Transitions for Non-Urgent Updates\n\n**Impact: MEDIUM (maintains UI responsiveness)**\n\nMark frequent, non-urgent state updates as transitions to maintain UI responsiveness.\n\n**Incorrect: blocks UI on every scroll**\n\n```tsx\nfunction ScrollTracker() {\n  const [scrollY, setScrollY] = useState(0)\n  useEffect(() => {\n    const handler = () => setScrollY(window.scrollY)\n    window.addEventListener('scroll', handler, { passive: true })\n    return () => window.removeEventListener('scroll', handler)\n  }, [])\n}\n```\n\n**Correct: non-blocking updates**\n\n```tsx\nimport { startTransition } from 'react'\n\nfunction ScrollTracker() {\n  const [scrollY, setScrollY] = useState(0)\n  useEffect(() => {\n    const handler = () => {\n      startTransition(() => setScrollY(window.scrollY))\n    }\n    window.addEventListener('scroll', handler, { passive: true })\n    return () => window.removeEventListener('scroll', handler)\n  }, [])\n}\n```\n\n### 5.14 Use useDeferredValue for Expensive Derived Renders\n\n**Impact: MEDIUM (keeps input responsive during heavy computation)**\n\nWhen user input triggers expensive computations or renders, use `useDeferredValue` to keep the input responsive. The deferred value lags behind, allowing React to prioritize the input update and render the expensive result when idle.\n\n**Incorrect: input feels laggy while filtering**\n\n```tsx\nfunction Search({ items }: { items: Item[] }) {\n  const [query, setQuery] = useState('')\n  const filtered = items.filter(item => fuzzyMatch(item, query))\n\n  return (\n    <>\n      <input value={query} onChange={e => setQuery(e.target.value)} />\n      <ResultsList results={filtered} />\n    </>\n  )\n}\n```\n\n**Correct: input stays snappy, results render when ready**\n\n```tsx\nfunction Search({ items }: { items: Item[] }) {\n  const [query, setQuery] = useState('')\n  const deferredQuery = useDeferredValue(query)\n  const filtered = useMemo(\n    () => items.filter(item => fuzzyMatch(item, deferredQuery)),\n    [items, deferredQuery]\n  )\n  const isStale = query !== deferredQuery\n\n  return (\n    <>\n      <input value={query} onChange={e => setQuery(e.target.value)} />\n      <div style={{ opacity: isStale ? 0.7 : 1 }}>\n        <ResultsList results={filtered} />\n      </div>\n    </>\n  )\n}\n```\n\n**When to use:**\n\n- Filtering/searching large lists\n\n- Expensive visualizations (charts, graphs) reacting to input\n\n- Any derived state that causes noticeable render delays\n\n**Note:** Wrap the expensive computation in `useMemo` with the deferred value as a dependency, otherwise it still runs on every render.\n\nReference: [https://react.dev/reference/react/useDeferredValue](https://react.dev/reference/react/useDeferredValue)\n\n### 5.15 Use useRef for Transient Values\n\n**Impact: MEDIUM (avoids unnecessary re-renders on frequent updates)**\n\nWhen a value changes frequently and you don't want a re-render on every update (e.g., mouse trackers, intervals, transient flags), store it in `useRef` instead of `useState`. Keep component state for UI; use refs for temporary DOM-adjacent values. Updating a ref does not trigger a re-render.\n\n**Incorrect: renders every update**\n\n```tsx\nfunction Tracker() {\n  const [lastX, setLastX] = useState(0)\n\n  useEffect(() => {\n    const onMove = (e: MouseEvent) => setLastX(e.clientX)\n    window.addEventListener('mousemove', onMove)\n    return () => window.removeEventListener('mousemove', onMove)\n  }, [])\n\n  return (\n    <div\n      style={{\n        position: 'fixed',\n        top: 0,\n        left: lastX,\n        width: 8,\n        height: 8,\n        background: 'black',\n      }}\n    />\n  )\n}\n```\n\n**Correct: no re-render for tracking**\n\n```tsx\nfunction Tracker() {\n  const lastXRef = useRef(0)\n  const dotRef = useRef<HTMLDivElement>(null)\n\n  useEffect(() => {\n    const onMove = (e: MouseEvent) => {\n      lastXRef.current = e.clientX\n      const node = dotRef.current\n      if (node) {\n        node.style.transform = `translateX(${e.clientX}px)`\n      }\n    }\n    window.addEventListener('mousemove', onMove)\n    return () => window.removeEventListener('mousemove', onMove)\n  }, [])\n\n  return (\n    <div\n      ref={dotRef}\n      style={{\n        position: 'fixed',\n        top: 0,\n        left: 0,\n        width: 8,\n        height: 8,\n        background: 'black',\n        transform: 'translateX(0px)',\n      }}\n    />\n  )\n}\n```\n\n---\n\n## 6. Rendering Performance\n\n**Impact: MEDIUM**\n\nOptimizing the rendering process reduces the work the browser needs to do.\n\n### 6.1 Animate SVG Wrapper Instead of SVG Element\n\n**Impact: LOW (enables hardware acceleration)**\n\nMany browsers don't have hardware acceleration for CSS3 animations on SVG elements. Wrap SVG in a `<div>` and animate the wrapper instead.\n\n**Incorrect: animating SVG directly - no hardware acceleration**\n\n```tsx\nfunction LoadingSpinner() {\n  return (\n    <svg \n      className=\"animate-spin\"\n      width=\"24\" \n      height=\"24\" \n      viewBox=\"0 0 24 24\"\n    >\n      <circle cx=\"12\" cy=\"12\" r=\"10\" stroke=\"currentColor\" />\n    </svg>\n  )\n}\n```\n\n**Correct: animating wrapper div - hardware accelerated**\n\n```tsx\nfunction LoadingSpinner() {\n  return (\n    <div className=\"animate-spin\">\n      <svg \n        width=\"24\" \n        height=\"24\" \n        viewBox=\"0 0 24 24\"\n      >\n        <circle cx=\"12\" cy=\"12\" r=\"10\" stroke=\"currentColor\" />\n      </svg>\n    </div>\n  )\n}\n```\n\nThis applies to all CSS transforms and transitions (`transform`, `opacity`, `translate`, `scale`, `rotate`). The wrapper div allows browsers to use GPU acceleration for smoother animations.\n\n### 6.2 CSS content-visibility for Long Lists\n\n**Impact: HIGH (faster initial render)**\n\nApply `content-visibility: auto` to defer off-screen rendering.\n\n**CSS:**\n\n```css\n.message-item {\n  content-visibility: auto;\n  contain-intrinsic-size: 0 80px;\n}\n```\n\n**Example:**\n\n```tsx\nfunction MessageList({ messages }: { messages: Message[] }) {\n  return (\n    <div className=\"overflow-y-auto h-screen\">\n      {messages.map(msg => (\n        <div key={msg.id} className=\"message-item\">\n          <Avatar user={msg.author} />\n          <div>{msg.content}</div>\n        </div>\n      ))}\n    </div>\n  )\n}\n```\n\nFor 1000 messages, browser skips layout/paint for ~990 off-screen items (10× faster initial render).\n\n### 6.3 Hoist Static JSX Elements\n\n**Impact: LOW (avoids re-creation)**\n\nExtract static JSX outside components to avoid re-creation.\n\n**Incorrect: recreates element every render**\n\n```tsx\nfunction LoadingSkeleton() {\n  return <div className=\"animate-pulse h-20 bg-gray-200\" />\n}\n\nfunction Container() {\n  return (\n    <div>\n      {loading && <LoadingSkeleton />}\n    </div>\n  )\n}\n```\n\n**Correct: reuses same element**\n\n```tsx\nconst loadingSkeleton = (\n  <div className=\"animate-pulse h-20 bg-gray-200\" />\n)\n\nfunction Container() {\n  return (\n    <div>\n      {loading && loadingSkeleton}\n    </div>\n  )\n}\n```\n\nThis is especially helpful for large and static SVG nodes, which can be expensive to recreate on every render.\n\n**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler automatically hoists static JSX elements and optimizes component re-renders, making manual hoisting unnecessary.\n\n### 6.4 Optimize SVG Precision\n\n**Impact: LOW (reduces file size)**\n\nReduce SVG coordinate precision to decrease file size. The optimal precision depends on the viewBox size, but in general reducing precision should be considered.\n\n**Incorrect: excessive precision**\n\n```svg\n<path d=\"M 10.293847 20.847362 L 30.938472 40.192837\" />\n```\n\n**Correct: 1 decimal place**\n\n```svg\n<path d=\"M 10.3 20.8 L 30.9 40.2\" />\n```\n\n**Automate with SVGO:**\n\n```bash\nnpx svgo --precision=1 --multipass icon.svg\n```\n\n### 6.5 Prevent Hydration Mismatch Without Flickering\n\n**Impact: MEDIUM (avoids visual flicker and hydration errors)**\n\nWhen rendering content that depends on client-side storage (localStorage, cookies), avoid both SSR breakage and post-hydration flickering by injecting a synchronous script that updates the DOM before React hydrates.\n\n**Incorrect: breaks SSR**\n\n```tsx\nfunction ThemeWrapper({ children }: { children: ReactNode }) {\n  // localStorage is not available on server - throws error\n  const theme = localStorage.getItem('theme') || 'light'\n  \n  return (\n    <div className={theme}>\n      {children}\n    </div>\n  )\n}\n```\n\nServer-side rendering will fail because `localStorage` is undefined.\n\n**Incorrect: visual flickering**\n\n```tsx\nfunction ThemeWrapper({ children }: { children: ReactNode }) {\n  const [theme, setTheme] = useState('light')\n  \n  useEffect(() => {\n    // Runs after hydration - causes visible flash\n    const stored = localStorage.getItem('theme')\n    if (stored) {\n      setTheme(stored)\n    }\n  }, [])\n  \n  return (\n    <div className={theme}>\n      {children}\n    </div>\n  )\n}\n```\n\nComponent first renders with default value (`light`), then updates after hydration, causing a visible flash of incorrect content.\n\n**Correct: no flicker, no hydration mismatch**\n\n```tsx\nfunction ThemeWrapper({ children }: { children: ReactNode }) {\n  return (\n    <>\n      <div id=\"theme-wrapper\">\n        {children}\n      </div>\n      <script\n        dangerouslySetInnerHTML={{\n          __html: `\n            (function() {\n              try {\n                var theme = localStorage.getItem('theme') || 'light';\n                var el = document.getElementById('theme-wrapper');\n                if (el) el.className = theme;\n              } catch (e) {}\n            })();\n          `,\n        }}\n      />\n    </>\n  )\n}\n```\n\nThe inline script executes synchronously before showing the element, ensuring the DOM already has the correct value. No flickering, no hydration mismatch.\n\nThis pattern is especially useful for theme toggles, user preferences, authentication states, and any client-only data that should render immediately without flashing default values.\n\n### 6.6 Suppress Expected Hydration Mismatches\n\n**Impact: LOW-MEDIUM (avoids noisy hydration warnings for known differences)**\n\nIn SSR frameworks (e.g., Next.js), some values are intentionally different on server vs client (random IDs, dates, locale/timezone formatting). For these *expected* mismatches, wrap the dynamic text in an element with `suppressHydrationWarning` to prevent noisy warnings. Do not use this to hide real bugs. Don’t overuse it.\n\n**Incorrect: known mismatch warnings**\n\n```tsx\nfunction Timestamp() {\n  return <span>{new Date().toLocaleString()}</span>\n}\n```\n\n**Correct: suppress expected mismatch only**\n\n```tsx\nfunction Timestamp() {\n  return (\n    <span suppressHydrationWarning>\n      {new Date().toLocaleString()}\n    </span>\n  )\n}\n```\n\n### 6.7 Use Activity Component for Show/Hide\n\n**Impact: MEDIUM (preserves state/DOM)**\n\nUse React's `<Activity>` to preserve state/DOM for expensive components that frequently toggle visibility.\n\n**Usage:**\n\n```tsx\nimport { Activity } from 'react'\n\nfunction Dropdown({ isOpen }: Props) {\n  return (\n    <Activity mode={isOpen ? 'visible' : 'hidden'}>\n      <ExpensiveMenu />\n    </Activity>\n  )\n}\n```\n\nAvoids expensive re-renders and state loss.\n\n### 6.8 Use defer or async on Script Tags\n\n**Impact: HIGH (eliminates render-blocking)**\n\nScript tags without `defer` or `async` block HTML parsing while the script downloads and executes. This delays First Contentful Paint and Time to Interactive.\n\n- **`defer`**: Downloads in parallel, executes after HTML parsing completes, maintains execution order\n\n- **`async`**: Downloads in parallel, executes immediately when ready, no guaranteed order\n\nUse `defer` for scripts that depend on DOM or other scripts. Use `async` for independent scripts like analytics.\n\n**Incorrect: blocks rendering**\n\n```tsx\nexport default function Document() {\n  return (\n    <html>\n      <head>\n        <script src=\"https://example.com/analytics.js\" />\n        <script src=\"/scripts/utils.js\" />\n      </head>\n      <body>{/* content */}</body>\n    </html>\n  )\n}\n```\n\n**Correct: non-blocking**\n\n```tsx\nimport Script from 'next/script'\n\nexport default function Page() {\n  return (\n    <>\n      <Script src=\"https://example.com/analytics.js\" strategy=\"afterInteractive\" />\n      <Script src=\"/scripts/utils.js\" strategy=\"beforeInteractive\" />\n    </>\n  )\n}\n```\n\n**Note:** In Next.js, prefer the `next/script` component with `strategy` prop instead of raw script tags:\n\nReference: [https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#defer](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#defer)\n\n### 6.9 Use Explicit Conditional Rendering\n\n**Impact: LOW (prevents rendering 0 or NaN)**\n\nUse explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be `0`, `NaN`, or other falsy values that render.\n\n**Incorrect: renders \"0\" when count is 0**\n\n```tsx\nfunction Badge({ count }: { count: number }) {\n  return (\n    <div>\n      {count && <span className=\"badge\">{count}</span>}\n    </div>\n  )\n}\n\n// When count = 0, renders: <div>0</div>\n// When count = 5, renders: <div><span class=\"badge\">5</span></div>\n```\n\n**Correct: renders nothing when count is 0**\n\n```tsx\nfunction Badge({ count }: { count: number }) {\n  return (\n    <div>\n      {count > 0 ? <span className=\"badge\">{count}</span> : null}\n    </div>\n  )\n}\n\n// When count = 0, renders: <div></div>\n// When count = 5, renders: <div><span class=\"badge\">5</span></div>\n```\n\n### 6.10 Use React DOM Resource Hints\n\n**Impact: HIGH (reduces load time for critical resources)**\n\nReact DOM provides APIs to hint the browser about resources it will need. These are especially useful in server components to start loading resources before the client even receives the HTML.\n\n- **`prefetchDNS(href)`**: Resolve DNS for a domain you expect to connect to\n\n- **`preconnect(href)`**: Establish connection (DNS + TCP + TLS) to a server\n\n- **`preload(href, options)`**: Fetch a resource (stylesheet, font, script, image) you'll use soon\n\n- **`preloadModule(href)`**: Fetch an ES module you'll use soon\n\n- **`preinit(href, options)`**: Fetch and evaluate a stylesheet or script\n\n- **`preinitModule(href)`**: Fetch and evaluate an ES module\n\n**Example: preconnect to third-party APIs**\n\n```tsx\nimport { preconnect, prefetchDNS } from 'react-dom'\n\nexport default function App() {\n  prefetchDNS('https://analytics.example.com')\n  preconnect('https://api.example.com')\n\n  return <main>{/* content */}</main>\n}\n```\n\n**Example: preload critical fonts and styles**\n\n```tsx\nimport { preload, preinit } from 'react-dom'\n\nexport default function RootLayout({ children }) {\n  // Preload font file\n  preload('/fonts/inter.woff2', { as: 'font', type: 'font/woff2', crossOrigin: 'anonymous' })\n\n  // Fetch and apply critical stylesheet immediately\n  preinit('/styles/critical.css', { as: 'style' })\n\n  return (\n    <html>\n      <body>{children}</body>\n    </html>\n  )\n}\n```\n\n**Example: preload modules for code-split routes**\n\n```tsx\nimport { preloadModule, preinitModule } from 'react-dom'\n\nfunction Navigation() {\n  const preloadDashboard = () => {\n    preloadModule('/dashboard.js', { as: 'script' })\n  }\n\n  return (\n    <nav>\n      <a href=\"/dashboard\" onMouseEnter={preloadDashboard}>\n        Dashboard\n      </a>\n    </nav>\n  )\n}\n```\n\n**When to use each:**\n\n| API | Use case |\n\n|-----|----------|\n\n| `prefetchDNS` | Third-party domains you'll connect to later |\n\n| `preconnect` | APIs or CDNs you'll fetch from immediately |\n\n| `preload` | Critical resources needed for current page |\n\n| `preloadModule` | JS modules for likely next navigation |\n\n| `preinit` | Stylesheets/scripts that must execute early |\n\n| `preinitModule` | ES modules that must execute early |\n\nReference: [https://react.dev/reference/react-dom#resource-preloading-apis](https://react.dev/reference/react-dom#resource-preloading-apis)\n\n### 6.11 Use useTransition Over Manual Loading States\n\n**Impact: LOW (reduces re-renders and improves code clarity)**\n\nUse `useTransition` instead of manual `useState` for loading states. This provides built-in `isPending` state and automatically manages transitions.\n\n**Incorrect: manual loading state**\n\n```tsx\nfunction SearchResults() {\n  const [query, setQuery] = useState('')\n  const [results, setResults] = useState([])\n  const [isLoading, setIsLoading] = useState(false)\n\n  const handleSearch = async (value: string) => {\n    setIsLoading(true)\n    setQuery(value)\n    const data = await fetchResults(value)\n    setResults(data)\n    setIsLoading(false)\n  }\n\n  return (\n    <>\n      <input onChange={(e) => handleSearch(e.target.value)} />\n      {isLoading && <Spinner />}\n      <ResultsList results={results} />\n    </>\n  )\n}\n```\n\n**Correct: useTransition with built-in pending state**\n\n```tsx\nimport { useTransition, useState } from 'react'\n\nfunction SearchResults() {\n  const [query, setQuery] = useState('')\n  const [results, setResults] = useState([])\n  const [isPending, startTransition] = useTransition()\n\n  const handleSearch = (value: string) => {\n    setQuery(value) // Update input immediately\n    \n    startTransition(async () => {\n      // Fetch and update results\n      const data = await fetchResults(value)\n      setResults(data)\n    })\n  }\n\n  return (\n    <>\n      <input onChange={(e) => handleSearch(e.target.value)} />\n      {isPending && <Spinner />}\n      <ResultsList results={results} />\n    </>\n  )\n}\n```\n\n**Benefits:**\n\n- **Automatic pending state**: No need to manually manage `setIsLoading(true/false)`\n\n- **Error resilience**: Pending state correctly resets even if the transition throws\n\n- **Better responsiveness**: Keeps the UI responsive during updates\n\n- **Interrupt handling**: New transitions automatically cancel pending ones\n\nReference: [https://react.dev/reference/react/useTransition](https://react.dev/reference/react/useTransition)\n\n---\n\n## 7. JavaScript Performance\n\n**Impact: LOW-MEDIUM**\n\nMicro-optimizations for hot paths can add up to meaningful improvements.\n\n### 7.1 Avoid Layout Thrashing\n\n**Impact: MEDIUM (prevents forced synchronous layouts and reduces performance bottlenecks)**\n\nAvoid interleaving style writes with layout reads. When you read a layout property (like `offsetWidth`, `getBoundingClientRect()`, or `getComputedStyle()`) between style changes, the browser is forced to trigger a synchronous reflow.\n\n**This is OK: browser batches style changes**\n\n```typescript\nfunction updateElementStyles(element: HTMLElement) {\n  // Each line invalidates style, but browser batches the recalculation\n  element.style.width = '100px'\n  element.style.height = '200px'\n  element.style.backgroundColor = 'blue'\n  element.style.border = '1px solid black'\n}\n```\n\n**Incorrect: interleaved reads and writes force reflows**\n\n```typescript\nfunction layoutThrashing(element: HTMLElement) {\n  element.style.width = '100px'\n  const width = element.offsetWidth  // Forces reflow\n  element.style.height = '200px'\n  const height = element.offsetHeight  // Forces another reflow\n}\n```\n\n**Correct: batch writes, then read once**\n\n```typescript\nfunction updateElementStyles(element: HTMLElement) {\n  // Batch all writes together\n  element.style.width = '100px'\n  element.style.height = '200px'\n  element.style.backgroundColor = 'blue'\n  element.style.border = '1px solid black'\n  \n  // Read after all writes are done (single reflow)\n  const { width, height } = element.getBoundingClientRect()\n}\n```\n\n**Correct: batch reads, then writes**\n\n```typescript\nfunction updateElementStyles(element: HTMLElement) {\n  element.classList.add('highlighted-box')\n  \n  const { width, height } = element.getBoundingClientRect()\n}\n```\n\n**Better: use CSS classes**\n\n**React example:**\n\n```tsx\n// Incorrect: interleaving style changes with layout queries\nfunction Box({ isHighlighted }: { isHighlighted: boolean }) {\n  const ref = useRef<HTMLDivElement>(null)\n  \n  useEffect(() => {\n    if (ref.current && isHighlighted) {\n      ref.current.style.width = '100px'\n      const width = ref.current.offsetWidth // Forces layout\n      ref.current.style.height = '200px'\n    }\n  }, [isHighlighted])\n  \n  return <div ref={ref}>Content</div>\n}\n\n// Correct: toggle class\nfunction Box({ isHighlighted }: { isHighlighted: boolean }) {\n  return (\n    <div className={isHighlighted ? 'highlighted-box' : ''}>\n      Content\n    </div>\n  )\n}\n```\n\nPrefer CSS classes over inline styles when possible. CSS files are cached by the browser, and classes provide better separation of concerns and are easier to maintain.\n\nSee [this gist](https://gist.github.com/paulirish/5d52fb081b3570c81e3a) and [CSS Triggers](https://csstriggers.com/) for more information on layout-forcing operations.\n\n### 7.2 Build Index Maps for Repeated Lookups\n\n**Impact: LOW-MEDIUM (1M ops to 2K ops)**\n\nMultiple `.find()` calls by the same key should use a Map.\n\n**Incorrect (O(n) per lookup):**\n\n```typescript\nfunction processOrders(orders: Order[], users: User[]) {\n  return orders.map(order => ({\n    ...order,\n    user: users.find(u => u.id === order.userId)\n  }))\n}\n```\n\n**Correct (O(1) per lookup):**\n\n```typescript\nfunction processOrders(orders: Order[], users: User[]) {\n  const userById = new Map(users.map(u => [u.id, u]))\n\n  return orders.map(order => ({\n    ...order,\n    user: userById.get(order.userId)\n  }))\n}\n```\n\nBuild map once (O(n)), then all lookups are O(1).\n\nFor 1000 orders × 1000 users: 1M ops → 2K ops.\n\n### 7.3 Cache Property Access in Loops\n\n**Impact: LOW-MEDIUM (reduces lookups)**\n\nCache object property lookups in hot paths.\n\n**Incorrect: 3 lookups × N iterations**\n\n```typescript\nfor (let i = 0; i < arr.length; i++) {\n  process(obj.config.settings.value)\n}\n```\n\n**Correct: 1 lookup total**\n\n```typescript\nconst value = obj.config.settings.value\nconst len = arr.length\nfor (let i = 0; i < len; i++) {\n  process(value)\n}\n```\n\n### 7.4 Cache Repeated Function Calls\n\n**Impact: MEDIUM (avoid redundant computation)**\n\nUse a module-level Map to cache function results when the same function is called repeatedly with the same inputs during render.\n\n**Incorrect: redundant computation**\n\n```typescript\nfunction ProjectList({ projects }: { projects: Project[] }) {\n  return (\n    <div>\n      {projects.map(project => {\n        // slugify() called 100+ times for same project names\n        const slug = slugify(project.name)\n        \n        return <ProjectCard key={project.id} slug={slug} />\n      })}\n    </div>\n  )\n}\n```\n\n**Correct: cached results**\n\n```typescript\n// Module-level cache\nconst slugifyCache = new Map<string, string>()\n\nfunction cachedSlugify(text: string): string {\n  if (slugifyCache.has(text)) {\n    return slugifyCache.get(text)!\n  }\n  const result = slugify(text)\n  slugifyCache.set(text, result)\n  return result\n}\n\nfunction ProjectList({ projects }: { projects: Project[] }) {\n  return (\n    <div>\n      {projects.map(project => {\n        // Computed only once per unique project name\n        const slug = cachedSlugify(project.name)\n        \n        return <ProjectCard key={project.id} slug={slug} />\n      })}\n    </div>\n  )\n}\n```\n\n**Simpler pattern for single-value functions:**\n\n```typescript\nlet isLoggedInCache: boolean | null = null\n\nfunction isLoggedIn(): boolean {\n  if (isLoggedInCache !== null) {\n    return isLoggedInCache\n  }\n  \n  isLoggedInCache = document.cookie.includes('auth=')\n  return isLoggedInCache\n}\n\n// Clear cache when auth changes\nfunction onAuthChange() {\n  isLoggedInCache = null\n}\n```\n\nUse a Map (not a hook) so it works everywhere: utilities, event handlers, not just React components.\n\nReference: [https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast](https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast)\n\n### 7.5 Cache Storage API Calls\n\n**Impact: LOW-MEDIUM (reduces expensive I/O)**\n\n`localStorage`, `sessionStorage`, and `document.cookie` are synchronous and expensive. Cache reads in memory.\n\n**Incorrect: reads storage on every call**\n\n```typescript\nfunction getTheme() {\n  return localStorage.getItem('theme') ?? 'light'\n}\n// Called 10 times = 10 storage reads\n```\n\n**Correct: Map cache**\n\n```typescript\nconst storageCache = new Map<string, string | null>()\n\nfunction getLocalStorage(key: string) {\n  if (!storageCache.has(key)) {\n    storageCache.set(key, localStorage.getItem(key))\n  }\n  return storageCache.get(key)\n}\n\nfunction setLocalStorage(key: string, value: string) {\n  localStorage.setItem(key, value)\n  storageCache.set(key, value)  // keep cache in sync\n}\n```\n\nUse a Map (not a hook) so it works everywhere: utilities, event handlers, not just React components.\n\n**Cookie caching:**\n\n```typescript\nlet cookieCache: Record<string, string> | null = null\n\nfunction getCookie(name: string) {\n  if (!cookieCache) {\n    cookieCache = Object.fromEntries(\n      document.cookie.split('; ').map(c => c.split('='))\n    )\n  }\n  return cookieCache[name]\n}\n```\n\n**Important: invalidate on external changes**\n\n```typescript\nwindow.addEventListener('storage', (e) => {\n  if (e.key) storageCache.delete(e.key)\n})\n\ndocument.addEventListener('visibilitychange', () => {\n  if (document.visibilityState === 'visible') {\n    storageCache.clear()\n  }\n})\n```\n\nIf storage can change externally (another tab, server-set cookies), invalidate cache:\n\n### 7.6 Combine Multiple Array Iterations\n\n**Impact: LOW-MEDIUM (reduces iterations)**\n\nMultiple `.filter()` or `.map()` calls iterate the array multiple times. Combine into one loop.\n\n**Incorrect: 3 iterations**\n\n```typescript\nconst admins = users.filter(u => u.isAdmin)\nconst testers = users.filter(u => u.isTester)\nconst inactive = users.filter(u => !u.isActive)\n```\n\n**Correct: 1 iteration**\n\n```typescript\nconst admins: User[] = []\nconst testers: User[] = []\nconst inactive: User[] = []\n\nfor (const user of users) {\n  if (user.isAdmin) admins.push(user)\n  if (user.isTester) testers.push(user)\n  if (!user.isActive) inactive.push(user)\n}\n```\n\n### 7.7 Early Length Check for Array Comparisons\n\n**Impact: MEDIUM-HIGH (avoids expensive operations when lengths differ)**\n\nWhen comparing arrays with expensive operations (sorting, deep equality, serialization), check lengths first. If lengths differ, the arrays cannot be equal.\n\nIn real-world applications, this optimization is especially valuable when the comparison runs in hot paths (event handlers, render loops).\n\n**Incorrect: always runs expensive comparison**\n\n```typescript\nfunction hasChanges(current: string[], original: string[]) {\n  // Always sorts and joins, even when lengths differ\n  return current.sort().join() !== original.sort().join()\n}\n```\n\nTwo O(n log n) sorts run even when `current.length` is 5 and `original.length` is 100. There is also overhead of joining the arrays and comparing the strings.\n\n**Correct (O(1) length check first):**\n\n```typescript\nfunction hasChanges(current: string[], original: string[]) {\n  // Early return if lengths differ\n  if (current.length !== original.length) {\n    return true\n  }\n  // Only sort when lengths match\n  const currentSorted = current.toSorted()\n  const originalSorted = original.toSorted()\n  for (let i = 0; i < currentSorted.length; i++) {\n    if (currentSorted[i] !== originalSorted[i]) {\n      return true\n    }\n  }\n  return false\n}\n```\n\nThis new approach is more efficient because:\n\n- It avoids the overhead of sorting and joining the arrays when lengths differ\n\n- It avoids consuming memory for the joined strings (especially important for large arrays)\n\n- It avoids mutating the original arrays\n\n- It returns early when a difference is found\n\n### 7.8 Early Return from Functions\n\n**Impact: LOW-MEDIUM (avoids unnecessary computation)**\n\nReturn early when result is determined to skip unnecessary processing.\n\n**Incorrect: processes all items even after finding answer**\n\n```typescript\nfunction validateUsers(users: User[]) {\n  let hasError = false\n  let errorMessage = ''\n  \n  for (const user of users) {\n    if (!user.email) {\n      hasError = true\n      errorMessage = 'Email required'\n    }\n    if (!user.name) {\n      hasError = true\n      errorMessage = 'Name required'\n    }\n    // Continues checking all users even after error found\n  }\n  \n  return hasError ? { valid: false, error: errorMessage } : { valid: true }\n}\n```\n\n**Correct: returns immediately on first error**\n\n```typescript\nfunction validateUsers(users: User[]) {\n  for (const user of users) {\n    if (!user.email) {\n      return { valid: false, error: 'Email required' }\n    }\n    if (!user.name) {\n      return { valid: false, error: 'Name required' }\n    }\n  }\n\n  return { valid: true }\n}\n```\n\n### 7.9 Hoist RegExp Creation\n\n**Impact: LOW-MEDIUM (avoids recreation)**\n\nDon't create RegExp inside render. Hoist to module scope or memoize with `useMemo()`.\n\n**Incorrect: new RegExp every render**\n\n```tsx\nfunction Highlighter({ text, query }: Props) {\n  const regex = new RegExp(`(${query})`, 'gi')\n  const parts = text.split(regex)\n  return <>{parts.map((part, i) => ...)}</>\n}\n```\n\n**Correct: memoize or hoist**\n\n```tsx\nconst EMAIL_REGEX = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/\n\nfunction Highlighter({ text, query }: Props) {\n  const regex = useMemo(\n    () => new RegExp(`(${escapeRegex(query)})`, 'gi'),\n    [query]\n  )\n  const parts = text.split(regex)\n  return <>{parts.map((part, i) => ...)}</>\n}\n```\n\n**Warning: global regex has mutable state**\n\n```typescript\nconst regex = /foo/g\nregex.test('foo')  // true, lastIndex = 3\nregex.test('foo')  // false, lastIndex = 0\n```\n\nGlobal regex (`/g`) has mutable `lastIndex` state:\n\n### 7.10 Use flatMap to Map and Filter in One Pass\n\n**Impact: LOW-MEDIUM (eliminates intermediate array)**\n\nChaining `.map().filter(Boolean)` creates an intermediate array and iterates twice. Use `.flatMap()` to transform and filter in a single pass.\n\n**Incorrect: 2 iterations, intermediate array**\n\n```typescript\nconst userNames = users\n  .map(user => user.isActive ? user.name : null)\n  .filter(Boolean)\n```\n\n**Correct: 1 iteration, no intermediate array**\n\n```typescript\nconst userNames = users.flatMap(user =>\n  user.isActive ? [user.name] : []\n)\n```\n\n**More examples:**\n\n```typescript\n// Extract valid emails from responses\n// Before\nconst emails = responses\n  .map(r => r.success ? r.data.email : null)\n  .filter(Boolean)\n\n// After\nconst emails = responses.flatMap(r =>\n  r.success ? [r.data.email] : []\n)\n\n// Parse and filter valid numbers\n// Before\nconst numbers = strings\n  .map(s => parseInt(s, 10))\n  .filter(n => !isNaN(n))\n\n// After\nconst numbers = strings.flatMap(s => {\n  const n = parseInt(s, 10)\n  return isNaN(n) ? [] : [n]\n})\n```\n\n**When to use:**\n\n- Transforming items while filtering some out\n\n- Conditional mapping where some inputs produce no output\n\n- Parsing/validating where invalid inputs should be skipped\n\n### 7.11 Use Loop for Min/Max Instead of Sort\n\n**Impact: LOW (O(n) instead of O(n log n))**\n\nFinding the smallest or largest element only requires a single pass through the array. Sorting is wasteful and slower.\n\n**Incorrect (O(n log n) - sort to find latest):**\n\n```typescript\ninterface Project {\n  id: string\n  name: string\n  updatedAt: number\n}\n\nfunction getLatestProject(projects: Project[]) {\n  const sorted = [...projects].sort((a, b) => b.updatedAt - a.updatedAt)\n  return sorted[0]\n}\n```\n\nSorts the entire array just to find the maximum value.\n\n**Incorrect (O(n log n) - sort for oldest and newest):**\n\n```typescript\nfunction getOldestAndNewest(projects: Project[]) {\n  const sorted = [...projects].sort((a, b) => a.updatedAt - b.updatedAt)\n  return { oldest: sorted[0], newest: sorted[sorted.length - 1] }\n}\n```\n\nStill sorts unnecessarily when only min/max are needed.\n\n**Correct (O(n) - single loop):**\n\n```typescript\nfunction getLatestProject(projects: Project[]) {\n  if (projects.length === 0) return null\n  \n  let latest = projects[0]\n  \n  for (let i = 1; i < projects.length; i++) {\n    if (projects[i].updatedAt > latest.updatedAt) {\n      latest = projects[i]\n    }\n  }\n  \n  return latest\n}\n\nfunction getOldestAndNewest(projects: Project[]) {\n  if (projects.length === 0) return { oldest: null, newest: null }\n  \n  let oldest = projects[0]\n  let newest = projects[0]\n  \n  for (let i = 1; i < projects.length; i++) {\n    if (projects[i].updatedAt < oldest.updatedAt) oldest = projects[i]\n    if (projects[i].updatedAt > newest.updatedAt) newest = projects[i]\n  }\n  \n  return { oldest, newest }\n}\n```\n\nSingle pass through the array, no copying, no sorting.\n\n**Alternative: Math.min/Math.max for small arrays**\n\n```typescript\nconst numbers = [5, 2, 8, 1, 9]\nconst min = Math.min(...numbers)\nconst max = Math.max(...numbers)\n```\n\nThis works for small arrays, but can be slower or just throw an error for very large arrays due to spread operator limitations. Maximal array length is approximately 124000 in Chrome 143 and 638000 in Safari 18; exact numbers may vary - see [the fiddle](https://jsfiddle.net/qw1jabsx/4/). Use the loop approach for reliability.\n\n### 7.12 Use Set/Map for O(1) Lookups\n\n**Impact: LOW-MEDIUM (O(n) to O(1))**\n\nConvert arrays to Set/Map for repeated membership checks.\n\n**Incorrect (O(n) per check):**\n\n```typescript\nconst allowedIds = ['a', 'b', 'c', ...]\nitems.filter(item => allowedIds.includes(item.id))\n```\n\n**Correct (O(1) per check):**\n\n```typescript\nconst allowedIds = new Set(['a', 'b', 'c', ...])\nitems.filter(item => allowedIds.has(item.id))\n```\n\n### 7.13 Use toSorted() Instead of sort() for Immutability\n\n**Impact: MEDIUM-HIGH (prevents mutation bugs in React state)**\n\n`.sort()` mutates the array in place, which can cause bugs with React state and props. Use `.toSorted()` to create a new sorted array without mutation.\n\n**Incorrect: mutates original array**\n\n```typescript\nfunction UserList({ users }: { users: User[] }) {\n  // Mutates the users prop array!\n  const sorted = useMemo(\n    () => users.sort((a, b) => a.name.localeCompare(b.name)),\n    [users]\n  )\n  return <div>{sorted.map(renderUser)}</div>\n}\n```\n\n**Correct: creates new array**\n\n```typescript\nfunction UserList({ users }: { users: User[] }) {\n  // Creates new sorted array, original unchanged\n  const sorted = useMemo(\n    () => users.toSorted((a, b) => a.name.localeCompare(b.name)),\n    [users]\n  )\n  return <div>{sorted.map(renderUser)}</div>\n}\n```\n\n**Why this matters in React:**\n\n1. Props/state mutations break React's immutability model - React expects props and state to be treated as read-only\n\n2. Causes stale closure bugs - Mutating arrays inside closures (callbacks, effects) can lead to unexpected behavior\n\n**Browser support: fallback for older browsers**\n\n```typescript\n// Fallback for older browsers\nconst sorted = [...items].sort((a, b) => a.value - b.value)\n```\n\n`.toSorted()` is available in all modern browsers (Chrome 110+, Safari 16+, Firefox 115+, Node.js 20+). For older environments, use spread operator:\n\n**Other immutable array methods:**\n\n- `.toSorted()` - immutable sort\n\n- `.toReversed()` - immutable reverse\n\n- `.toSpliced()` - immutable splice\n\n- `.with()` - immutable element replacement\n\n---\n\n## 8. Advanced Patterns\n\n**Impact: LOW**\n\nAdvanced patterns for specific cases that require careful implementation.\n\n### 8.1 Initialize App Once, Not Per Mount\n\n**Impact: LOW-MEDIUM (avoids duplicate init in development)**\n\nDo not put app-wide initialization that must run once per app load inside `useEffect([])` of a component. Components can remount and effects will re-run. Use a module-level guard or top-level init in the entry module instead.\n\n**Incorrect: runs twice in dev, re-runs on remount**\n\n```tsx\nfunction Comp() {\n  useEffect(() => {\n    loadFromStorage()\n    checkAuthToken()\n  }, [])\n\n  // ...\n}\n```\n\n**Correct: once per app load**\n\n```tsx\nlet didInit = false\n\nfunction Comp() {\n  useEffect(() => {\n    if (didInit) return\n    didInit = true\n    loadFromStorage()\n    checkAuthToken()\n  }, [])\n\n  // ...\n}\n```\n\nReference: [https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)\n\n### 8.2 Store Event Handlers in Refs\n\n**Impact: LOW (stable subscriptions)**\n\nStore callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.\n\n**Incorrect: re-subscribes on every render**\n\n```tsx\nfunction useWindowEvent(event: string, handler: (e) => void) {\n  useEffect(() => {\n    window.addEventListener(event, handler)\n    return () => window.removeEventListener(event, handler)\n  }, [event, handler])\n}\n```\n\n**Correct: stable subscription**\n\n```tsx\nimport { useEffectEvent } from 'react'\n\nfunction useWindowEvent(event: string, handler: (e) => void) {\n  const onEvent = useEffectEvent(handler)\n\n  useEffect(() => {\n    window.addEventListener(event, onEvent)\n    return () => window.removeEventListener(event, onEvent)\n  }, [event])\n}\n```\n\n**Alternative: use `useEffectEvent` if you're on latest React:**\n\n`useEffectEvent` provides a cleaner API for the same pattern: it creates a stable function reference that always calls the latest version of the handler.\n\n### 8.3 useEffectEvent for Stable Callback Refs\n\n**Impact: LOW (prevents effect re-runs)**\n\nAccess latest values in callbacks without adding them to dependency arrays. Prevents effect re-runs while avoiding stale closures.\n\n**Incorrect: effect re-runs on every callback change**\n\n```tsx\nfunction SearchInput({ onSearch }: { onSearch: (q: string) => void }) {\n  const [query, setQuery] = useState('')\n\n  useEffect(() => {\n    const timeout = setTimeout(() => onSearch(query), 300)\n    return () => clearTimeout(timeout)\n  }, [query, onSearch])\n}\n```\n\n**Correct: using React's useEffectEvent**\n\n```tsx\nimport { useEffectEvent } from 'react';\n\nfunction SearchInput({ onSearch }: { onSearch: (q: string) => void }) {\n  const [query, setQuery] = useState('')\n  const onSearchEvent = useEffectEvent(onSearch)\n\n  useEffect(() => {\n    const timeout = setTimeout(() => onSearchEvent(query), 300)\n    return () => clearTimeout(timeout)\n  }, [query])\n}\n```\n\n---\n\n## References\n\n1. [https://react.dev](https://react.dev)\n2. [https://nextjs.org](https://nextjs.org)\n3. [https://swr.vercel.app](https://swr.vercel.app)\n4. [https://github.com/shuding/better-all](https://github.com/shuding/better-all)\n5. [https://github.com/isaacs/node-lru-cache](https://github.com/isaacs/node-lru-cache)\n6. [https://vercel.com/blog/how-we-optimized-package-imports-in-next-js](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js)\n7. [https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast](https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast)\n"
  },
  {
    "path": "skills/react-best-practices/README.md",
    "content": "# React Best Practices\n\nA structured repository for creating and maintaining React Best Practices optimized for agents and LLMs.\n\n## Structure\n\n- `rules/` - Individual rule files (one per rule)\n  - `_sections.md` - Section metadata (titles, impacts, descriptions)\n  - `_template.md` - Template for creating new rules\n  - `area-description.md` - Individual rule files\n- `src/` - Build scripts and utilities\n- `metadata.json` - Document metadata (version, organization, abstract)\n- __`AGENTS.md`__ - Compiled output (generated)\n- __`test-cases.json`__ - Test cases for LLM evaluation (generated)\n\n## Getting Started\n\n1. Install dependencies:\n   ```bash\n   pnpm install\n   ```\n\n2. Build AGENTS.md from rules:\n   ```bash\n   pnpm build\n   ```\n\n3. Validate rule files:\n   ```bash\n   pnpm validate\n   ```\n\n4. Extract test cases:\n   ```bash\n   pnpm extract-tests\n   ```\n\n## Creating a New Rule\n\n1. Copy `rules/_template.md` to `rules/area-description.md`\n2. Choose the appropriate area prefix:\n   - `async-` for Eliminating Waterfalls (Section 1)\n   - `bundle-` for Bundle Size Optimization (Section 2)\n   - `server-` for Server-Side Performance (Section 3)\n   - `client-` for Client-Side Data Fetching (Section 4)\n   - `rerender-` for Re-render Optimization (Section 5)\n   - `rendering-` for Rendering Performance (Section 6)\n   - `js-` for JavaScript Performance (Section 7)\n   - `advanced-` for Advanced Patterns (Section 8)\n3. Fill in the frontmatter and content\n4. Ensure you have clear examples with explanations\n5. Run `pnpm build` to regenerate AGENTS.md and test-cases.json\n\n## Rule File Structure\n\nEach rule file should follow this structure:\n\n```markdown\n---\ntitle: Rule Title Here\nimpact: MEDIUM\nimpactDescription: Optional description\ntags: tag1, tag2, tag3\n---\n\n## Rule Title Here\n\nBrief explanation of the rule and why it matters.\n\n**Incorrect (description of what's wrong):**\n\n```typescript\n// Bad code example\n```\n\n**Correct (description of what's right):**\n\n```typescript\n// Good code example\n```\n\nOptional explanatory text after examples.\n\nReference: [Link](https://example.com)\n\n## File Naming Convention\n\n- Files starting with `_` are special (excluded from build)\n- Rule files: `area-description.md` (e.g., `async-parallel.md`)\n- Section is automatically inferred from filename prefix\n- Rules are sorted alphabetically by title within each section\n- IDs (e.g., 1.1, 1.2) are auto-generated during build\n\n## Impact Levels\n\n- `CRITICAL` - Highest priority, major performance gains\n- `HIGH` - Significant performance improvements\n- `MEDIUM-HIGH` - Moderate-high gains\n- `MEDIUM` - Moderate performance improvements\n- `LOW-MEDIUM` - Low-medium gains\n- `LOW` - Incremental improvements\n\n## Scripts\n\n- `pnpm build` - Compile rules into AGENTS.md\n- `pnpm validate` - Validate all rule files\n- `pnpm extract-tests` - Extract test cases for LLM evaluation\n- `pnpm dev` - Build and validate\n\n## Contributing\n\nWhen adding or modifying rules:\n\n1. Use the correct filename prefix for your section\n2. Follow the `_template.md` structure\n3. Include clear bad/good examples with explanations\n4. Add appropriate tags\n5. Run `pnpm build` to regenerate AGENTS.md and test-cases.json\n6. Rules are automatically sorted by title - no need to manage numbers!\n\n## Acknowledgments\n\nOriginally created by [@shuding](https://x.com/shuding) at [Vercel](https://vercel.com).\n"
  },
  {
    "path": "skills/react-best-practices/SKILL.md",
    "content": "---\nname: vercel-react-best-practices\ndescription: React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.\nlicense: MIT\nmetadata:\n  author: vercel\n  version: \"1.0.0\"\n---\n\n# Vercel React Best Practices\n\nComprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 64 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.\n\n## When to Apply\n\nReference these guidelines when:\n- Writing new React components or Next.js pages\n- Implementing data fetching (client or server-side)\n- Reviewing code for performance issues\n- Refactoring existing React/Next.js code\n- Optimizing bundle size or load times\n\n## Rule Categories by Priority\n\n| Priority | Category | Impact | Prefix |\n|----------|----------|--------|--------|\n| 1 | Eliminating Waterfalls | CRITICAL | `async-` |\n| 2 | Bundle Size Optimization | CRITICAL | `bundle-` |\n| 3 | Server-Side Performance | HIGH | `server-` |\n| 4 | Client-Side Data Fetching | MEDIUM-HIGH | `client-` |\n| 5 | Re-render Optimization | MEDIUM | `rerender-` |\n| 6 | Rendering Performance | MEDIUM | `rendering-` |\n| 7 | JavaScript Performance | LOW-MEDIUM | `js-` |\n| 8 | Advanced Patterns | LOW | `advanced-` |\n\n## Quick Reference\n\n### 1. Eliminating Waterfalls (CRITICAL)\n\n- `async-defer-await` - Move await into branches where actually used\n- `async-parallel` - Use Promise.all() for independent operations\n- `async-dependencies` - Use better-all for partial dependencies\n- `async-api-routes` - Start promises early, await late in API routes\n- `async-suspense-boundaries` - Use Suspense to stream content\n\n### 2. Bundle Size Optimization (CRITICAL)\n\n- `bundle-barrel-imports` - Import directly, avoid barrel files\n- `bundle-dynamic-imports` - Use next/dynamic for heavy components\n- `bundle-defer-third-party` - Load analytics/logging after hydration\n- `bundle-conditional` - Load modules only when feature is activated\n- `bundle-preload` - Preload on hover/focus for perceived speed\n\n### 3. Server-Side Performance (HIGH)\n\n- `server-auth-actions` - Authenticate server actions like API routes\n- `server-cache-react` - Use React.cache() for per-request deduplication\n- `server-cache-lru` - Use LRU cache for cross-request caching\n- `server-dedup-props` - Avoid duplicate serialization in RSC props\n- `server-hoist-static-io` - Hoist static I/O (fonts, logos) to module level\n- `server-serialization` - Minimize data passed to client components\n- `server-parallel-fetching` - Restructure components to parallelize fetches\n- `server-after-nonblocking` - Use after() for non-blocking operations\n\n### 4. Client-Side Data Fetching (MEDIUM-HIGH)\n\n- `client-swr-dedup` - Use SWR for automatic request deduplication\n- `client-event-listeners` - Deduplicate global event listeners\n- `client-passive-event-listeners` - Use passive listeners for scroll\n- `client-localstorage-schema` - Version and minimize localStorage data\n\n### 5. Re-render Optimization (MEDIUM)\n\n- `rerender-defer-reads` - Don't subscribe to state only used in callbacks\n- `rerender-memo` - Extract expensive work into memoized components\n- `rerender-memo-with-default-value` - Hoist default non-primitive props\n- `rerender-dependencies` - Use primitive dependencies in effects\n- `rerender-derived-state` - Subscribe to derived booleans, not raw values\n- `rerender-derived-state-no-effect` - Derive state during render, not effects\n- `rerender-functional-setstate` - Use functional setState for stable callbacks\n- `rerender-lazy-state-init` - Pass function to useState for expensive values\n- `rerender-simple-expression-in-memo` - Avoid memo for simple primitives\n- `rerender-split-combined-hooks` - Split hooks with independent dependencies\n- `rerender-move-effect-to-event` - Put interaction logic in event handlers\n- `rerender-transitions` - Use startTransition for non-urgent updates\n- `rerender-use-deferred-value` - Defer expensive renders to keep input responsive\n- `rerender-use-ref-transient-values` - Use refs for transient frequent values\n- `rerender-no-inline-components` - Don't define components inside components\n\n### 6. Rendering Performance (MEDIUM)\n\n- `rendering-animate-svg-wrapper` - Animate div wrapper, not SVG element\n- `rendering-content-visibility` - Use content-visibility for long lists\n- `rendering-hoist-jsx` - Extract static JSX outside components\n- `rendering-svg-precision` - Reduce SVG coordinate precision\n- `rendering-hydration-no-flicker` - Use inline script for client-only data\n- `rendering-hydration-suppress-warning` - Suppress expected mismatches\n- `rendering-activity` - Use Activity component for show/hide\n- `rendering-conditional-render` - Use ternary, not && for conditionals\n- `rendering-usetransition-loading` - Prefer useTransition for loading state\n- `rendering-resource-hints` - Use React DOM resource hints for preloading\n- `rendering-script-defer-async` - Use defer or async on script tags\n\n### 7. JavaScript Performance (LOW-MEDIUM)\n\n- `js-batch-dom-css` - Group CSS changes via classes or cssText\n- `js-index-maps` - Build Map for repeated lookups\n- `js-cache-property-access` - Cache object properties in loops\n- `js-cache-function-results` - Cache function results in module-level Map\n- `js-cache-storage` - Cache localStorage/sessionStorage reads\n- `js-combine-iterations` - Combine multiple filter/map into one loop\n- `js-length-check-first` - Check array length before expensive comparison\n- `js-early-exit` - Return early from functions\n- `js-hoist-regexp` - Hoist RegExp creation outside loops\n- `js-min-max-loop` - Use loop for min/max instead of sort\n- `js-set-map-lookups` - Use Set/Map for O(1) lookups\n- `js-tosorted-immutable` - Use toSorted() for immutability\n- `js-flatmap-filter` - Use flatMap to map and filter in one pass\n\n### 8. Advanced Patterns (LOW)\n\n- `advanced-event-handler-refs` - Store event handlers in refs\n- `advanced-init-once` - Initialize app once per app load\n- `advanced-use-latest` - useLatest for stable callback refs\n\n## How to Use\n\nRead individual rule files for detailed explanations and code examples:\n\n```\nrules/async-parallel.md\nrules/bundle-barrel-imports.md\n```\n\nEach rule file contains:\n- Brief explanation of why it matters\n- Incorrect code example with explanation\n- Correct code example with explanation\n- Additional context and references\n\n## Full Compiled Document\n\nFor the complete guide with all rules expanded: `AGENTS.md`\n"
  },
  {
    "path": "skills/react-best-practices/metadata.json",
    "content": "{\n  \"version\": \"1.0.0\",\n  \"organization\": \"Vercel Engineering\",\n  \"date\": \"January 2026\",\n  \"abstract\": \"Comprehensive performance optimization guide for React and Next.js applications, designed for AI agents and LLMs. Contains 40+ rules across 8 categories, prioritized by impact from critical (eliminating waterfalls, reducing bundle size) to incremental (advanced patterns). Each rule includes detailed explanations, real-world examples comparing incorrect vs. correct implementations, and specific impact metrics to guide automated refactoring and code generation.\",\n  \"references\": [\n    \"https://react.dev\",\n    \"https://nextjs.org\",\n    \"https://swr.vercel.app\",\n    \"https://github.com/shuding/better-all\",\n    \"https://github.com/isaacs/node-lru-cache\",\n    \"https://vercel.com/blog/how-we-optimized-package-imports-in-next-js\",\n    \"https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast\"\n  ]\n}\n"
  },
  {
    "path": "skills/react-best-practices/rules/_sections.md",
    "content": "# Sections\n\nThis file defines all sections, their ordering, impact levels, and descriptions.\nThe section ID (in parentheses) is the filename prefix used to group rules.\n\n---\n\n## 1. Eliminating Waterfalls (async)\n\n**Impact:** CRITICAL  \n**Description:** Waterfalls are the #1 performance killer. Each sequential await adds full network latency. Eliminating them yields the largest gains.\n\n## 2. Bundle Size Optimization (bundle)\n\n**Impact:** CRITICAL  \n**Description:** Reducing initial bundle size improves Time to Interactive and Largest Contentful Paint.\n\n## 3. Server-Side Performance (server)\n\n**Impact:** HIGH  \n**Description:** Optimizing server-side rendering and data fetching eliminates server-side waterfalls and reduces response times.\n\n## 4. Client-Side Data Fetching (client)\n\n**Impact:** MEDIUM-HIGH  \n**Description:** Automatic deduplication and efficient data fetching patterns reduce redundant network requests.\n\n## 5. Re-render Optimization (rerender)\n\n**Impact:** MEDIUM  \n**Description:** Reducing unnecessary re-renders minimizes wasted computation and improves UI responsiveness.\n\n## 6. Rendering Performance (rendering)\n\n**Impact:** MEDIUM  \n**Description:** Optimizing the rendering process reduces the work the browser needs to do.\n\n## 7. JavaScript Performance (js)\n\n**Impact:** LOW-MEDIUM  \n**Description:** Micro-optimizations for hot paths can add up to meaningful improvements.\n\n## 8. Advanced Patterns (advanced)\n\n**Impact:** LOW  \n**Description:** Advanced patterns for specific cases that require careful implementation.\n"
  },
  {
    "path": "skills/react-best-practices/rules/_template.md",
    "content": "---\ntitle: Rule Title Here\nimpact: MEDIUM\nimpactDescription: Optional description of impact (e.g., \"20-50% improvement\")\ntags: tag1, tag2\n---\n\n## Rule Title Here\n\n**Impact: MEDIUM (optional impact description)**\n\nBrief explanation of the rule and why it matters. This should be clear and concise, explaining the performance implications.\n\n**Incorrect (description of what's wrong):**\n\n```typescript\n// Bad code example here\nconst bad = example()\n```\n\n**Correct (description of what's right):**\n\n```typescript\n// Good code example here\nconst good = example()\n```\n\nReference: [Link to documentation or resource](https://example.com)\n"
  },
  {
    "path": "skills/react-best-practices/rules/advanced-event-handler-refs.md",
    "content": "---\ntitle: Store Event Handlers in Refs\nimpact: LOW\nimpactDescription: stable subscriptions\ntags: advanced, hooks, refs, event-handlers, optimization\n---\n\n## Store Event Handlers in Refs\n\nStore callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.\n\n**Incorrect (re-subscribes on every render):**\n\n```tsx\nfunction useWindowEvent(event: string, handler: (e) => void) {\n  useEffect(() => {\n    window.addEventListener(event, handler)\n    return () => window.removeEventListener(event, handler)\n  }, [event, handler])\n}\n```\n\n**Correct (stable subscription):**\n\n```tsx\nfunction useWindowEvent(event: string, handler: (e) => void) {\n  const handlerRef = useRef(handler)\n  useEffect(() => {\n    handlerRef.current = handler\n  }, [handler])\n\n  useEffect(() => {\n    const listener = (e) => handlerRef.current(e)\n    window.addEventListener(event, listener)\n    return () => window.removeEventListener(event, listener)\n  }, [event])\n}\n```\n\n**Alternative: use `useEffectEvent` if you're on latest React:**\n\n```tsx\nimport { useEffectEvent } from 'react'\n\nfunction useWindowEvent(event: string, handler: (e) => void) {\n  const onEvent = useEffectEvent(handler)\n\n  useEffect(() => {\n    window.addEventListener(event, onEvent)\n    return () => window.removeEventListener(event, onEvent)\n  }, [event])\n}\n```\n\n`useEffectEvent` provides a cleaner API for the same pattern: it creates a stable function reference that always calls the latest version of the handler.\n"
  },
  {
    "path": "skills/react-best-practices/rules/advanced-init-once.md",
    "content": "---\ntitle: Initialize App Once, Not Per Mount\nimpact: LOW-MEDIUM\nimpactDescription: avoids duplicate init in development\ntags: initialization, useEffect, app-startup, side-effects\n---\n\n## Initialize App Once, Not Per Mount\n\nDo not put app-wide initialization that must run once per app load inside `useEffect([])` of a component. Components can remount and effects will re-run. Use a module-level guard or top-level init in the entry module instead.\n\n**Incorrect (runs twice in dev, re-runs on remount):**\n\n```tsx\nfunction Comp() {\n  useEffect(() => {\n    loadFromStorage()\n    checkAuthToken()\n  }, [])\n\n  // ...\n}\n```\n\n**Correct (once per app load):**\n\n```tsx\nlet didInit = false\n\nfunction Comp() {\n  useEffect(() => {\n    if (didInit) return\n    didInit = true\n    loadFromStorage()\n    checkAuthToken()\n  }, [])\n\n  // ...\n}\n```\n\nReference: [Initializing the application](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)\n"
  },
  {
    "path": "skills/react-best-practices/rules/advanced-use-latest.md",
    "content": "---\ntitle: useEffectEvent for Stable Callback Refs\nimpact: LOW\nimpactDescription: prevents effect re-runs\ntags: advanced, hooks, useEffectEvent, refs, optimization\n---\n\n## useEffectEvent for Stable Callback Refs\n\nAccess latest values in callbacks without adding them to dependency arrays. Prevents effect re-runs while avoiding stale closures.\n\n**Incorrect (effect re-runs on every callback change):**\n\n```tsx\nfunction SearchInput({ onSearch }: { onSearch: (q: string) => void }) {\n  const [query, setQuery] = useState('')\n\n  useEffect(() => {\n    const timeout = setTimeout(() => onSearch(query), 300)\n    return () => clearTimeout(timeout)\n  }, [query, onSearch])\n}\n```\n\n**Correct (using React's useEffectEvent):**\n\n```tsx\nimport { useEffectEvent } from 'react';\n\nfunction SearchInput({ onSearch }: { onSearch: (q: string) => void }) {\n  const [query, setQuery] = useState('')\n  const onSearchEvent = useEffectEvent(onSearch)\n\n  useEffect(() => {\n    const timeout = setTimeout(() => onSearchEvent(query), 300)\n    return () => clearTimeout(timeout)\n  }, [query])\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/async-api-routes.md",
    "content": "---\ntitle: Prevent Waterfall Chains in API Routes\nimpact: CRITICAL\nimpactDescription: 2-10× improvement\ntags: api-routes, server-actions, waterfalls, parallelization\n---\n\n## Prevent Waterfall Chains in API Routes\n\nIn API routes and Server Actions, start independent operations immediately, even if you don't await them yet.\n\n**Incorrect (config waits for auth, data waits for both):**\n\n```typescript\nexport async function GET(request: Request) {\n  const session = await auth()\n  const config = await fetchConfig()\n  const data = await fetchData(session.user.id)\n  return Response.json({ data, config })\n}\n```\n\n**Correct (auth and config start immediately):**\n\n```typescript\nexport async function GET(request: Request) {\n  const sessionPromise = auth()\n  const configPromise = fetchConfig()\n  const session = await sessionPromise\n  const [config, data] = await Promise.all([\n    configPromise,\n    fetchData(session.user.id)\n  ])\n  return Response.json({ data, config })\n}\n```\n\nFor operations with more complex dependency chains, use `better-all` to automatically maximize parallelism (see Dependency-Based Parallelization).\n"
  },
  {
    "path": "skills/react-best-practices/rules/async-defer-await.md",
    "content": "---\ntitle: Defer Await Until Needed\nimpact: HIGH\nimpactDescription: avoids blocking unused code paths\ntags: async, await, conditional, optimization\n---\n\n## Defer Await Until Needed\n\nMove `await` operations into the branches where they're actually used to avoid blocking code paths that don't need them.\n\n**Incorrect (blocks both branches):**\n\n```typescript\nasync function handleRequest(userId: string, skipProcessing: boolean) {\n  const userData = await fetchUserData(userId)\n  \n  if (skipProcessing) {\n    // Returns immediately but still waited for userData\n    return { skipped: true }\n  }\n  \n  // Only this branch uses userData\n  return processUserData(userData)\n}\n```\n\n**Correct (only blocks when needed):**\n\n```typescript\nasync function handleRequest(userId: string, skipProcessing: boolean) {\n  if (skipProcessing) {\n    // Returns immediately without waiting\n    return { skipped: true }\n  }\n  \n  // Fetch only when needed\n  const userData = await fetchUserData(userId)\n  return processUserData(userData)\n}\n```\n\n**Another example (early return optimization):**\n\n```typescript\n// Incorrect: always fetches permissions\nasync function updateResource(resourceId: string, userId: string) {\n  const permissions = await fetchPermissions(userId)\n  const resource = await getResource(resourceId)\n  \n  if (!resource) {\n    return { error: 'Not found' }\n  }\n  \n  if (!permissions.canEdit) {\n    return { error: 'Forbidden' }\n  }\n  \n  return await updateResourceData(resource, permissions)\n}\n\n// Correct: fetches only when needed\nasync function updateResource(resourceId: string, userId: string) {\n  const resource = await getResource(resourceId)\n  \n  if (!resource) {\n    return { error: 'Not found' }\n  }\n  \n  const permissions = await fetchPermissions(userId)\n  \n  if (!permissions.canEdit) {\n    return { error: 'Forbidden' }\n  }\n  \n  return await updateResourceData(resource, permissions)\n}\n```\n\nThis optimization is especially valuable when the skipped branch is frequently taken, or when the deferred operation is expensive.\n"
  },
  {
    "path": "skills/react-best-practices/rules/async-dependencies.md",
    "content": "---\ntitle: Dependency-Based Parallelization\nimpact: CRITICAL\nimpactDescription: 2-10× improvement\ntags: async, parallelization, dependencies, better-all\n---\n\n## Dependency-Based Parallelization\n\nFor operations with partial dependencies, use `better-all` to maximize parallelism. It automatically starts each task at the earliest possible moment.\n\n**Incorrect (profile waits for config unnecessarily):**\n\n```typescript\nconst [user, config] = await Promise.all([\n  fetchUser(),\n  fetchConfig()\n])\nconst profile = await fetchProfile(user.id)\n```\n\n**Correct (config and profile run in parallel):**\n\n```typescript\nimport { all } from 'better-all'\n\nconst { user, config, profile } = await all({\n  async user() { return fetchUser() },\n  async config() { return fetchConfig() },\n  async profile() {\n    return fetchProfile((await this.$.user).id)\n  }\n})\n```\n\n**Alternative without extra dependencies:**\n\nWe can also create all the promises first, and do `Promise.all()` at the end.\n\n```typescript\nconst userPromise = fetchUser()\nconst profilePromise = userPromise.then(user => fetchProfile(user.id))\n\nconst [user, config, profile] = await Promise.all([\n  userPromise,\n  fetchConfig(),\n  profilePromise\n])\n```\n\nReference: [https://github.com/shuding/better-all](https://github.com/shuding/better-all)\n"
  },
  {
    "path": "skills/react-best-practices/rules/async-parallel.md",
    "content": "---\ntitle: Promise.all() for Independent Operations\nimpact: CRITICAL\nimpactDescription: 2-10× improvement\ntags: async, parallelization, promises, waterfalls\n---\n\n## Promise.all() for Independent Operations\n\nWhen async operations have no interdependencies, execute them concurrently using `Promise.all()`.\n\n**Incorrect (sequential execution, 3 round trips):**\n\n```typescript\nconst user = await fetchUser()\nconst posts = await fetchPosts()\nconst comments = await fetchComments()\n```\n\n**Correct (parallel execution, 1 round trip):**\n\n```typescript\nconst [user, posts, comments] = await Promise.all([\n  fetchUser(),\n  fetchPosts(),\n  fetchComments()\n])\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/async-suspense-boundaries.md",
    "content": "---\ntitle: Strategic Suspense Boundaries\nimpact: HIGH\nimpactDescription: faster initial paint\ntags: async, suspense, streaming, layout-shift\n---\n\n## Strategic Suspense Boundaries\n\nInstead of awaiting data in async components before returning JSX, use Suspense boundaries to show the wrapper UI faster while data loads.\n\n**Incorrect (wrapper blocked by data fetching):**\n\n```tsx\nasync function Page() {\n  const data = await fetchData() // Blocks entire page\n  \n  return (\n    <div>\n      <div>Sidebar</div>\n      <div>Header</div>\n      <div>\n        <DataDisplay data={data} />\n      </div>\n      <div>Footer</div>\n    </div>\n  )\n}\n```\n\nThe entire layout waits for data even though only the middle section needs it.\n\n**Correct (wrapper shows immediately, data streams in):**\n\n```tsx\nfunction Page() {\n  return (\n    <div>\n      <div>Sidebar</div>\n      <div>Header</div>\n      <div>\n        <Suspense fallback={<Skeleton />}>\n          <DataDisplay />\n        </Suspense>\n      </div>\n      <div>Footer</div>\n    </div>\n  )\n}\n\nasync function DataDisplay() {\n  const data = await fetchData() // Only blocks this component\n  return <div>{data.content}</div>\n}\n```\n\nSidebar, Header, and Footer render immediately. Only DataDisplay waits for data.\n\n**Alternative (share promise across components):**\n\n```tsx\nfunction Page() {\n  // Start fetch immediately, but don't await\n  const dataPromise = fetchData()\n  \n  return (\n    <div>\n      <div>Sidebar</div>\n      <div>Header</div>\n      <Suspense fallback={<Skeleton />}>\n        <DataDisplay dataPromise={dataPromise} />\n        <DataSummary dataPromise={dataPromise} />\n      </Suspense>\n      <div>Footer</div>\n    </div>\n  )\n}\n\nfunction DataDisplay({ dataPromise }: { dataPromise: Promise<Data> }) {\n  const data = use(dataPromise) // Unwraps the promise\n  return <div>{data.content}</div>\n}\n\nfunction DataSummary({ dataPromise }: { dataPromise: Promise<Data> }) {\n  const data = use(dataPromise) // Reuses the same promise\n  return <div>{data.summary}</div>\n}\n```\n\nBoth components share the same promise, so only one fetch occurs. Layout renders immediately while both components wait together.\n\n**When NOT to use this pattern:**\n\n- Critical data needed for layout decisions (affects positioning)\n- SEO-critical content above the fold\n- Small, fast queries where suspense overhead isn't worth it\n- When you want to avoid layout shift (loading → content jump)\n\n**Trade-off:** Faster initial paint vs potential layout shift. Choose based on your UX priorities.\n"
  },
  {
    "path": "skills/react-best-practices/rules/bundle-barrel-imports.md",
    "content": "---\ntitle: Avoid Barrel File Imports\nimpact: CRITICAL\nimpactDescription: 200-800ms import cost, slow builds\ntags: bundle, imports, tree-shaking, barrel-files, performance\n---\n\n## Avoid Barrel File Imports\n\nImport directly from source files instead of barrel files to avoid loading thousands of unused modules. **Barrel files** are entry points that re-export multiple modules (e.g., `index.js` that does `export * from './module'`).\n\nPopular icon and component libraries can have **up to 10,000 re-exports** in their entry file. For many React packages, **it takes 200-800ms just to import them**, affecting both development speed and production cold starts.\n\n**Why tree-shaking doesn't help:** When a library is marked as external (not bundled), the bundler can't optimize it. If you bundle it to enable tree-shaking, builds become substantially slower analyzing the entire module graph.\n\n**Incorrect (imports entire library):**\n\n```tsx\nimport { Check, X, Menu } from 'lucide-react'\n// Loads 1,583 modules, takes ~2.8s extra in dev\n// Runtime cost: 200-800ms on every cold start\n\nimport { Button, TextField } from '@mui/material'\n// Loads 2,225 modules, takes ~4.2s extra in dev\n```\n\n**Correct (imports only what you need):**\n\n```tsx\nimport Check from 'lucide-react/dist/esm/icons/check'\nimport X from 'lucide-react/dist/esm/icons/x'\nimport Menu from 'lucide-react/dist/esm/icons/menu'\n// Loads only 3 modules (~2KB vs ~1MB)\n\nimport Button from '@mui/material/Button'\nimport TextField from '@mui/material/TextField'\n// Loads only what you use\n```\n\n**Alternative (Next.js 13.5+):**\n\n```js\n// next.config.js - use optimizePackageImports\nmodule.exports = {\n  experimental: {\n    optimizePackageImports: ['lucide-react', '@mui/material']\n  }\n}\n\n// Then you can keep the ergonomic barrel imports:\nimport { Check, X, Menu } from 'lucide-react'\n// Automatically transformed to direct imports at build time\n```\n\nDirect imports provide 15-70% faster dev boot, 28% faster builds, 40% faster cold starts, and significantly faster HMR.\n\nLibraries commonly affected: `lucide-react`, `@mui/material`, `@mui/icons-material`, `@tabler/icons-react`, `react-icons`, `@headlessui/react`, `@radix-ui/react-*`, `lodash`, `ramda`, `date-fns`, `rxjs`, `react-use`.\n\nReference: [How we optimized package imports in Next.js](https://vercel.com/blog/how-we-optimized-package-imports-in-next-js)\n"
  },
  {
    "path": "skills/react-best-practices/rules/bundle-conditional.md",
    "content": "---\ntitle: Conditional Module Loading\nimpact: HIGH\nimpactDescription: loads large data only when needed\ntags: bundle, conditional-loading, lazy-loading\n---\n\n## Conditional Module Loading\n\nLoad large data or modules only when a feature is activated.\n\n**Example (lazy-load animation frames):**\n\n```tsx\nfunction AnimationPlayer({ enabled, setEnabled }: { enabled: boolean; setEnabled: React.Dispatch<React.SetStateAction<boolean>> }) {\n  const [frames, setFrames] = useState<Frame[] | null>(null)\n\n  useEffect(() => {\n    if (enabled && !frames && typeof window !== 'undefined') {\n      import('./animation-frames.js')\n        .then(mod => setFrames(mod.frames))\n        .catch(() => setEnabled(false))\n    }\n  }, [enabled, frames, setEnabled])\n\n  if (!frames) return <Skeleton />\n  return <Canvas frames={frames} />\n}\n```\n\nThe `typeof window !== 'undefined'` check prevents bundling this module for SSR, optimizing server bundle size and build speed.\n"
  },
  {
    "path": "skills/react-best-practices/rules/bundle-defer-third-party.md",
    "content": "---\ntitle: Defer Non-Critical Third-Party Libraries\nimpact: MEDIUM\nimpactDescription: loads after hydration\ntags: bundle, third-party, analytics, defer\n---\n\n## Defer Non-Critical Third-Party Libraries\n\nAnalytics, logging, and error tracking don't block user interaction. Load them after hydration.\n\n**Incorrect (blocks initial bundle):**\n\n```tsx\nimport { Analytics } from '@vercel/analytics/react'\n\nexport default function RootLayout({ children }) {\n  return (\n    <html>\n      <body>\n        {children}\n        <Analytics />\n      </body>\n    </html>\n  )\n}\n```\n\n**Correct (loads after hydration):**\n\n```tsx\nimport dynamic from 'next/dynamic'\n\nconst Analytics = dynamic(\n  () => import('@vercel/analytics/react').then(m => m.Analytics),\n  { ssr: false }\n)\n\nexport default function RootLayout({ children }) {\n  return (\n    <html>\n      <body>\n        {children}\n        <Analytics />\n      </body>\n    </html>\n  )\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/bundle-dynamic-imports.md",
    "content": "---\ntitle: Dynamic Imports for Heavy Components\nimpact: CRITICAL\nimpactDescription: directly affects TTI and LCP\ntags: bundle, dynamic-import, code-splitting, next-dynamic\n---\n\n## Dynamic Imports for Heavy Components\n\nUse `next/dynamic` to lazy-load large components not needed on initial render.\n\n**Incorrect (Monaco bundles with main chunk ~300KB):**\n\n```tsx\nimport { MonacoEditor } from './monaco-editor'\n\nfunction CodePanel({ code }: { code: string }) {\n  return <MonacoEditor value={code} />\n}\n```\n\n**Correct (Monaco loads on demand):**\n\n```tsx\nimport dynamic from 'next/dynamic'\n\nconst MonacoEditor = dynamic(\n  () => import('./monaco-editor').then(m => m.MonacoEditor),\n  { ssr: false }\n)\n\nfunction CodePanel({ code }: { code: string }) {\n  return <MonacoEditor value={code} />\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/bundle-preload.md",
    "content": "---\ntitle: Preload Based on User Intent\nimpact: MEDIUM\nimpactDescription: reduces perceived latency\ntags: bundle, preload, user-intent, hover\n---\n\n## Preload Based on User Intent\n\nPreload heavy bundles before they're needed to reduce perceived latency.\n\n**Example (preload on hover/focus):**\n\n```tsx\nfunction EditorButton({ onClick }: { onClick: () => void }) {\n  const preload = () => {\n    if (typeof window !== 'undefined') {\n      void import('./monaco-editor')\n    }\n  }\n\n  return (\n    <button\n      onMouseEnter={preload}\n      onFocus={preload}\n      onClick={onClick}\n    >\n      Open Editor\n    </button>\n  )\n}\n```\n\n**Example (preload when feature flag is enabled):**\n\n```tsx\nfunction FlagsProvider({ children, flags }: Props) {\n  useEffect(() => {\n    if (flags.editorEnabled && typeof window !== 'undefined') {\n      void import('./monaco-editor').then(mod => mod.init())\n    }\n  }, [flags.editorEnabled])\n\n  return <FlagsContext.Provider value={flags}>\n    {children}\n  </FlagsContext.Provider>\n}\n```\n\nThe `typeof window !== 'undefined'` check prevents bundling preloaded modules for SSR, optimizing server bundle size and build speed.\n"
  },
  {
    "path": "skills/react-best-practices/rules/client-event-listeners.md",
    "content": "---\ntitle: Deduplicate Global Event Listeners\nimpact: LOW\nimpactDescription: single listener for N components\ntags: client, swr, event-listeners, subscription\n---\n\n## Deduplicate Global Event Listeners\n\nUse `useSWRSubscription()` to share global event listeners across component instances.\n\n**Incorrect (N instances = N listeners):**\n\n```tsx\nfunction useKeyboardShortcut(key: string, callback: () => void) {\n  useEffect(() => {\n    const handler = (e: KeyboardEvent) => {\n      if (e.metaKey && e.key === key) {\n        callback()\n      }\n    }\n    window.addEventListener('keydown', handler)\n    return () => window.removeEventListener('keydown', handler)\n  }, [key, callback])\n}\n```\n\nWhen using the `useKeyboardShortcut` hook multiple times, each instance will register a new listener.\n\n**Correct (N instances = 1 listener):**\n\n```tsx\nimport useSWRSubscription from 'swr/subscription'\n\n// Module-level Map to track callbacks per key\nconst keyCallbacks = new Map<string, Set<() => void>>()\n\nfunction useKeyboardShortcut(key: string, callback: () => void) {\n  // Register this callback in the Map\n  useEffect(() => {\n    if (!keyCallbacks.has(key)) {\n      keyCallbacks.set(key, new Set())\n    }\n    keyCallbacks.get(key)!.add(callback)\n\n    return () => {\n      const set = keyCallbacks.get(key)\n      if (set) {\n        set.delete(callback)\n        if (set.size === 0) {\n          keyCallbacks.delete(key)\n        }\n      }\n    }\n  }, [key, callback])\n\n  useSWRSubscription('global-keydown', () => {\n    const handler = (e: KeyboardEvent) => {\n      if (e.metaKey && keyCallbacks.has(e.key)) {\n        keyCallbacks.get(e.key)!.forEach(cb => cb())\n      }\n    }\n    window.addEventListener('keydown', handler)\n    return () => window.removeEventListener('keydown', handler)\n  })\n}\n\nfunction Profile() {\n  // Multiple shortcuts will share the same listener\n  useKeyboardShortcut('p', () => { /* ... */ }) \n  useKeyboardShortcut('k', () => { /* ... */ })\n  // ...\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/client-localstorage-schema.md",
    "content": "---\ntitle: Version and Minimize localStorage Data\nimpact: MEDIUM\nimpactDescription: prevents schema conflicts, reduces storage size\ntags: client, localStorage, storage, versioning, data-minimization\n---\n\n## Version and Minimize localStorage Data\n\nAdd version prefix to keys and store only needed fields. Prevents schema conflicts and accidental storage of sensitive data.\n\n**Incorrect:**\n\n```typescript\n// No version, stores everything, no error handling\nlocalStorage.setItem('userConfig', JSON.stringify(fullUserObject))\nconst data = localStorage.getItem('userConfig')\n```\n\n**Correct:**\n\n```typescript\nconst VERSION = 'v2'\n\nfunction saveConfig(config: { theme: string; language: string }) {\n  try {\n    localStorage.setItem(`userConfig:${VERSION}`, JSON.stringify(config))\n  } catch {\n    // Throws in incognito/private browsing, quota exceeded, or disabled\n  }\n}\n\nfunction loadConfig() {\n  try {\n    const data = localStorage.getItem(`userConfig:${VERSION}`)\n    return data ? JSON.parse(data) : null\n  } catch {\n    return null\n  }\n}\n\n// Migration from v1 to v2\nfunction migrate() {\n  try {\n    const v1 = localStorage.getItem('userConfig:v1')\n    if (v1) {\n      const old = JSON.parse(v1)\n      saveConfig({ theme: old.darkMode ? 'dark' : 'light', language: old.lang })\n      localStorage.removeItem('userConfig:v1')\n    }\n  } catch {}\n}\n```\n\n**Store minimal fields from server responses:**\n\n```typescript\n// User object has 20+ fields, only store what UI needs\nfunction cachePrefs(user: FullUser) {\n  try {\n    localStorage.setItem('prefs:v1', JSON.stringify({\n      theme: user.preferences.theme,\n      notifications: user.preferences.notifications\n    }))\n  } catch {}\n}\n```\n\n**Always wrap in try-catch:** `getItem()` and `setItem()` throw in incognito/private browsing (Safari, Firefox), when quota exceeded, or when disabled.\n\n**Benefits:** Schema evolution via versioning, reduced storage size, prevents storing tokens/PII/internal flags.\n"
  },
  {
    "path": "skills/react-best-practices/rules/client-passive-event-listeners.md",
    "content": "---\ntitle: Use Passive Event Listeners for Scrolling Performance\nimpact: MEDIUM\nimpactDescription: eliminates scroll delay caused by event listeners\ntags: client, event-listeners, scrolling, performance, touch, wheel\n---\n\n## Use Passive Event Listeners for Scrolling Performance\n\nAdd `{ passive: true }` to touch and wheel event listeners to enable immediate scrolling. Browsers normally wait for listeners to finish to check if `preventDefault()` is called, causing scroll delay.\n\n**Incorrect:**\n\n```typescript\nuseEffect(() => {\n  const handleTouch = (e: TouchEvent) => console.log(e.touches[0].clientX)\n  const handleWheel = (e: WheelEvent) => console.log(e.deltaY)\n  \n  document.addEventListener('touchstart', handleTouch)\n  document.addEventListener('wheel', handleWheel)\n  \n  return () => {\n    document.removeEventListener('touchstart', handleTouch)\n    document.removeEventListener('wheel', handleWheel)\n  }\n}, [])\n```\n\n**Correct:**\n\n```typescript\nuseEffect(() => {\n  const handleTouch = (e: TouchEvent) => console.log(e.touches[0].clientX)\n  const handleWheel = (e: WheelEvent) => console.log(e.deltaY)\n  \n  document.addEventListener('touchstart', handleTouch, { passive: true })\n  document.addEventListener('wheel', handleWheel, { passive: true })\n  \n  return () => {\n    document.removeEventListener('touchstart', handleTouch)\n    document.removeEventListener('wheel', handleWheel)\n  }\n}, [])\n```\n\n**Use passive when:** tracking/analytics, logging, any listener that doesn't call `preventDefault()`.\n\n**Don't use passive when:** implementing custom swipe gestures, custom zoom controls, or any listener that needs `preventDefault()`.\n"
  },
  {
    "path": "skills/react-best-practices/rules/client-swr-dedup.md",
    "content": "---\ntitle: Use SWR for Automatic Deduplication\nimpact: MEDIUM-HIGH\nimpactDescription: automatic deduplication\ntags: client, swr, deduplication, data-fetching\n---\n\n## Use SWR for Automatic Deduplication\n\nSWR enables request deduplication, caching, and revalidation across component instances.\n\n**Incorrect (no deduplication, each instance fetches):**\n\n```tsx\nfunction UserList() {\n  const [users, setUsers] = useState([])\n  useEffect(() => {\n    fetch('/api/users')\n      .then(r => r.json())\n      .then(setUsers)\n  }, [])\n}\n```\n\n**Correct (multiple instances share one request):**\n\n```tsx\nimport useSWR from 'swr'\n\nfunction UserList() {\n  const { data: users } = useSWR('/api/users', fetcher)\n}\n```\n\n**For immutable data:**\n\n```tsx\nimport { useImmutableSWR } from '@/lib/swr'\n\nfunction StaticContent() {\n  const { data } = useImmutableSWR('/api/config', fetcher)\n}\n```\n\n**For mutations:**\n\n```tsx\nimport { useSWRMutation } from 'swr/mutation'\n\nfunction UpdateButton() {\n  const { trigger } = useSWRMutation('/api/user', updateUser)\n  return <button onClick={() => trigger()}>Update</button>\n}\n```\n\nReference: [https://swr.vercel.app](https://swr.vercel.app)\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-batch-dom-css.md",
    "content": "---\ntitle: Avoid Layout Thrashing\nimpact: MEDIUM\nimpactDescription: prevents forced synchronous layouts and reduces performance bottlenecks\ntags: javascript, dom, css, performance, reflow, layout-thrashing\n---\n\n## Avoid Layout Thrashing\n\nAvoid interleaving style writes with layout reads. When you read a layout property (like `offsetWidth`, `getBoundingClientRect()`, or `getComputedStyle()`) between style changes, the browser is forced to trigger a synchronous reflow.\n\n**This is OK (browser batches style changes):**\n```typescript\nfunction updateElementStyles(element: HTMLElement) {\n  // Each line invalidates style, but browser batches the recalculation\n  element.style.width = '100px'\n  element.style.height = '200px'\n  element.style.backgroundColor = 'blue'\n  element.style.border = '1px solid black'\n}\n```\n\n**Incorrect (interleaved reads and writes force reflows):**\n```typescript\nfunction layoutThrashing(element: HTMLElement) {\n  element.style.width = '100px'\n  const width = element.offsetWidth  // Forces reflow\n  element.style.height = '200px'\n  const height = element.offsetHeight  // Forces another reflow\n}\n```\n\n**Correct (batch writes, then read once):**\n```typescript\nfunction updateElementStyles(element: HTMLElement) {\n  // Batch all writes together\n  element.style.width = '100px'\n  element.style.height = '200px'\n  element.style.backgroundColor = 'blue'\n  element.style.border = '1px solid black'\n  \n  // Read after all writes are done (single reflow)\n  const { width, height } = element.getBoundingClientRect()\n}\n```\n\n**Correct (batch reads, then writes):**\n```typescript\nfunction avoidThrashing(element: HTMLElement) {\n  // Read phase - all layout queries first\n  const rect1 = element.getBoundingClientRect()\n  const offsetWidth = element.offsetWidth\n  const offsetHeight = element.offsetHeight\n  \n  // Write phase - all style changes after\n  element.style.width = '100px'\n  element.style.height = '200px'\n}\n```\n\n**Better: use CSS classes**\n```css\n.highlighted-box {\n  width: 100px;\n  height: 200px;\n  background-color: blue;\n  border: 1px solid black;\n}\n```\n```typescript\nfunction updateElementStyles(element: HTMLElement) {\n  element.classList.add('highlighted-box')\n  \n  const { width, height } = element.getBoundingClientRect()\n}\n```\n\n**React example:**\n```tsx\n// Incorrect: interleaving style changes with layout queries\nfunction Box({ isHighlighted }: { isHighlighted: boolean }) {\n  const ref = useRef<HTMLDivElement>(null)\n  \n  useEffect(() => {\n    if (ref.current && isHighlighted) {\n      ref.current.style.width = '100px'\n      const width = ref.current.offsetWidth // Forces layout\n      ref.current.style.height = '200px'\n    }\n  }, [isHighlighted])\n  \n  return <div ref={ref}>Content</div>\n}\n\n// Correct: toggle class\nfunction Box({ isHighlighted }: { isHighlighted: boolean }) {\n  return (\n    <div className={isHighlighted ? 'highlighted-box' : ''}>\n      Content\n    </div>\n  )\n}\n```\n\nPrefer CSS classes over inline styles when possible. CSS files are cached by the browser, and classes provide better separation of concerns and are easier to maintain.\n\nSee [this gist](https://gist.github.com/paulirish/5d52fb081b3570c81e3a) and [CSS Triggers](https://csstriggers.com/) for more information on layout-forcing operations.\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-cache-function-results.md",
    "content": "---\ntitle: Cache Repeated Function Calls\nimpact: MEDIUM\nimpactDescription: avoid redundant computation\ntags: javascript, cache, memoization, performance\n---\n\n## Cache Repeated Function Calls\n\nUse a module-level Map to cache function results when the same function is called repeatedly with the same inputs during render.\n\n**Incorrect (redundant computation):**\n\n```typescript\nfunction ProjectList({ projects }: { projects: Project[] }) {\n  return (\n    <div>\n      {projects.map(project => {\n        // slugify() called 100+ times for same project names\n        const slug = slugify(project.name)\n        \n        return <ProjectCard key={project.id} slug={slug} />\n      })}\n    </div>\n  )\n}\n```\n\n**Correct (cached results):**\n\n```typescript\n// Module-level cache\nconst slugifyCache = new Map<string, string>()\n\nfunction cachedSlugify(text: string): string {\n  if (slugifyCache.has(text)) {\n    return slugifyCache.get(text)!\n  }\n  const result = slugify(text)\n  slugifyCache.set(text, result)\n  return result\n}\n\nfunction ProjectList({ projects }: { projects: Project[] }) {\n  return (\n    <div>\n      {projects.map(project => {\n        // Computed only once per unique project name\n        const slug = cachedSlugify(project.name)\n        \n        return <ProjectCard key={project.id} slug={slug} />\n      })}\n    </div>\n  )\n}\n```\n\n**Simpler pattern for single-value functions:**\n\n```typescript\nlet isLoggedInCache: boolean | null = null\n\nfunction isLoggedIn(): boolean {\n  if (isLoggedInCache !== null) {\n    return isLoggedInCache\n  }\n  \n  isLoggedInCache = document.cookie.includes('auth=')\n  return isLoggedInCache\n}\n\n// Clear cache when auth changes\nfunction onAuthChange() {\n  isLoggedInCache = null\n}\n```\n\nUse a Map (not a hook) so it works everywhere: utilities, event handlers, not just React components.\n\nReference: [How we made the Vercel Dashboard twice as fast](https://vercel.com/blog/how-we-made-the-vercel-dashboard-twice-as-fast)\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-cache-property-access.md",
    "content": "---\ntitle: Cache Property Access in Loops\nimpact: LOW-MEDIUM\nimpactDescription: reduces lookups\ntags: javascript, loops, optimization, caching\n---\n\n## Cache Property Access in Loops\n\nCache object property lookups in hot paths.\n\n**Incorrect (3 lookups × N iterations):**\n\n```typescript\nfor (let i = 0; i < arr.length; i++) {\n  process(obj.config.settings.value)\n}\n```\n\n**Correct (1 lookup total):**\n\n```typescript\nconst value = obj.config.settings.value\nconst len = arr.length\nfor (let i = 0; i < len; i++) {\n  process(value)\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-cache-storage.md",
    "content": "---\ntitle: Cache Storage API Calls\nimpact: LOW-MEDIUM\nimpactDescription: reduces expensive I/O\ntags: javascript, localStorage, storage, caching, performance\n---\n\n## Cache Storage API Calls\n\n`localStorage`, `sessionStorage`, and `document.cookie` are synchronous and expensive. Cache reads in memory.\n\n**Incorrect (reads storage on every call):**\n\n```typescript\nfunction getTheme() {\n  return localStorage.getItem('theme') ?? 'light'\n}\n// Called 10 times = 10 storage reads\n```\n\n**Correct (Map cache):**\n\n```typescript\nconst storageCache = new Map<string, string | null>()\n\nfunction getLocalStorage(key: string) {\n  if (!storageCache.has(key)) {\n    storageCache.set(key, localStorage.getItem(key))\n  }\n  return storageCache.get(key)\n}\n\nfunction setLocalStorage(key: string, value: string) {\n  localStorage.setItem(key, value)\n  storageCache.set(key, value)  // keep cache in sync\n}\n```\n\nUse a Map (not a hook) so it works everywhere: utilities, event handlers, not just React components.\n\n**Cookie caching:**\n\n```typescript\nlet cookieCache: Record<string, string> | null = null\n\nfunction getCookie(name: string) {\n  if (!cookieCache) {\n    cookieCache = Object.fromEntries(\n      document.cookie.split('; ').map(c => c.split('='))\n    )\n  }\n  return cookieCache[name]\n}\n```\n\n**Important (invalidate on external changes):**\n\nIf storage can change externally (another tab, server-set cookies), invalidate cache:\n\n```typescript\nwindow.addEventListener('storage', (e) => {\n  if (e.key) storageCache.delete(e.key)\n})\n\ndocument.addEventListener('visibilitychange', () => {\n  if (document.visibilityState === 'visible') {\n    storageCache.clear()\n  }\n})\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-combine-iterations.md",
    "content": "---\ntitle: Combine Multiple Array Iterations\nimpact: LOW-MEDIUM\nimpactDescription: reduces iterations\ntags: javascript, arrays, loops, performance\n---\n\n## Combine Multiple Array Iterations\n\nMultiple `.filter()` or `.map()` calls iterate the array multiple times. Combine into one loop.\n\n**Incorrect (3 iterations):**\n\n```typescript\nconst admins = users.filter(u => u.isAdmin)\nconst testers = users.filter(u => u.isTester)\nconst inactive = users.filter(u => !u.isActive)\n```\n\n**Correct (1 iteration):**\n\n```typescript\nconst admins: User[] = []\nconst testers: User[] = []\nconst inactive: User[] = []\n\nfor (const user of users) {\n  if (user.isAdmin) admins.push(user)\n  if (user.isTester) testers.push(user)\n  if (!user.isActive) inactive.push(user)\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-early-exit.md",
    "content": "---\ntitle: Early Return from Functions\nimpact: LOW-MEDIUM\nimpactDescription: avoids unnecessary computation\ntags: javascript, functions, optimization, early-return\n---\n\n## Early Return from Functions\n\nReturn early when result is determined to skip unnecessary processing.\n\n**Incorrect (processes all items even after finding answer):**\n\n```typescript\nfunction validateUsers(users: User[]) {\n  let hasError = false\n  let errorMessage = ''\n  \n  for (const user of users) {\n    if (!user.email) {\n      hasError = true\n      errorMessage = 'Email required'\n    }\n    if (!user.name) {\n      hasError = true\n      errorMessage = 'Name required'\n    }\n    // Continues checking all users even after error found\n  }\n  \n  return hasError ? { valid: false, error: errorMessage } : { valid: true }\n}\n```\n\n**Correct (returns immediately on first error):**\n\n```typescript\nfunction validateUsers(users: User[]) {\n  for (const user of users) {\n    if (!user.email) {\n      return { valid: false, error: 'Email required' }\n    }\n    if (!user.name) {\n      return { valid: false, error: 'Name required' }\n    }\n  }\n\n  return { valid: true }\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-flatmap-filter.md",
    "content": "---\ntitle: Use flatMap to Map and Filter in One Pass\nimpact: LOW-MEDIUM\nimpactDescription: eliminates intermediate array\ntags: javascript, arrays, flatMap, filter, performance\n---\n\n## Use flatMap to Map and Filter in One Pass\n\n**Impact: LOW-MEDIUM (eliminates intermediate array)**\n\nChaining `.map().filter(Boolean)` creates an intermediate array and iterates twice. Use `.flatMap()` to transform and filter in a single pass.\n\n**Incorrect (2 iterations, intermediate array):**\n\n```typescript\nconst userNames = users\n  .map(user => user.isActive ? user.name : null)\n  .filter(Boolean)\n```\n\n**Correct (1 iteration, no intermediate array):**\n\n```typescript\nconst userNames = users.flatMap(user =>\n  user.isActive ? [user.name] : []\n)\n```\n\n**More examples:**\n\n```typescript\n// Extract valid emails from responses\n// Before\nconst emails = responses\n  .map(r => r.success ? r.data.email : null)\n  .filter(Boolean)\n\n// After\nconst emails = responses.flatMap(r =>\n  r.success ? [r.data.email] : []\n)\n\n// Parse and filter valid numbers\n// Before\nconst numbers = strings\n  .map(s => parseInt(s, 10))\n  .filter(n => !isNaN(n))\n\n// After\nconst numbers = strings.flatMap(s => {\n  const n = parseInt(s, 10)\n  return isNaN(n) ? [] : [n]\n})\n```\n\n**When to use:**\n- Transforming items while filtering some out\n- Conditional mapping where some inputs produce no output\n- Parsing/validating where invalid inputs should be skipped\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-hoist-regexp.md",
    "content": "---\ntitle: Hoist RegExp Creation\nimpact: LOW-MEDIUM\nimpactDescription: avoids recreation\ntags: javascript, regexp, optimization, memoization\n---\n\n## Hoist RegExp Creation\n\nDon't create RegExp inside render. Hoist to module scope or memoize with `useMemo()`.\n\n**Incorrect (new RegExp every render):**\n\n```tsx\nfunction Highlighter({ text, query }: Props) {\n  const regex = new RegExp(`(${query})`, 'gi')\n  const parts = text.split(regex)\n  return <>{parts.map((part, i) => ...)}</>\n}\n```\n\n**Correct (memoize or hoist):**\n\n```tsx\nconst EMAIL_REGEX = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/\n\nfunction Highlighter({ text, query }: Props) {\n  const regex = useMemo(\n    () => new RegExp(`(${escapeRegex(query)})`, 'gi'),\n    [query]\n  )\n  const parts = text.split(regex)\n  return <>{parts.map((part, i) => ...)}</>\n}\n```\n\n**Warning (global regex has mutable state):**\n\nGlobal regex (`/g`) has mutable `lastIndex` state:\n\n```typescript\nconst regex = /foo/g\nregex.test('foo')  // true, lastIndex = 3\nregex.test('foo')  // false, lastIndex = 0\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-index-maps.md",
    "content": "---\ntitle: Build Index Maps for Repeated Lookups\nimpact: LOW-MEDIUM\nimpactDescription: 1M ops to 2K ops\ntags: javascript, map, indexing, optimization, performance\n---\n\n## Build Index Maps for Repeated Lookups\n\nMultiple `.find()` calls by the same key should use a Map.\n\n**Incorrect (O(n) per lookup):**\n\n```typescript\nfunction processOrders(orders: Order[], users: User[]) {\n  return orders.map(order => ({\n    ...order,\n    user: users.find(u => u.id === order.userId)\n  }))\n}\n```\n\n**Correct (O(1) per lookup):**\n\n```typescript\nfunction processOrders(orders: Order[], users: User[]) {\n  const userById = new Map(users.map(u => [u.id, u]))\n\n  return orders.map(order => ({\n    ...order,\n    user: userById.get(order.userId)\n  }))\n}\n```\n\nBuild map once (O(n)), then all lookups are O(1).\nFor 1000 orders × 1000 users: 1M ops → 2K ops.\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-length-check-first.md",
    "content": "---\ntitle: Early Length Check for Array Comparisons\nimpact: MEDIUM-HIGH\nimpactDescription: avoids expensive operations when lengths differ\ntags: javascript, arrays, performance, optimization, comparison\n---\n\n## Early Length Check for Array Comparisons\n\nWhen comparing arrays with expensive operations (sorting, deep equality, serialization), check lengths first. If lengths differ, the arrays cannot be equal.\n\nIn real-world applications, this optimization is especially valuable when the comparison runs in hot paths (event handlers, render loops).\n\n**Incorrect (always runs expensive comparison):**\n\n```typescript\nfunction hasChanges(current: string[], original: string[]) {\n  // Always sorts and joins, even when lengths differ\n  return current.sort().join() !== original.sort().join()\n}\n```\n\nTwo O(n log n) sorts run even when `current.length` is 5 and `original.length` is 100. There is also overhead of joining the arrays and comparing the strings.\n\n**Correct (O(1) length check first):**\n\n```typescript\nfunction hasChanges(current: string[], original: string[]) {\n  // Early return if lengths differ\n  if (current.length !== original.length) {\n    return true\n  }\n  // Only sort when lengths match\n  const currentSorted = current.toSorted()\n  const originalSorted = original.toSorted()\n  for (let i = 0; i < currentSorted.length; i++) {\n    if (currentSorted[i] !== originalSorted[i]) {\n      return true\n    }\n  }\n  return false\n}\n```\n\nThis new approach is more efficient because:\n- It avoids the overhead of sorting and joining the arrays when lengths differ\n- It avoids consuming memory for the joined strings (especially important for large arrays)\n- It avoids mutating the original arrays\n- It returns early when a difference is found\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-min-max-loop.md",
    "content": "---\ntitle: Use Loop for Min/Max Instead of Sort\nimpact: LOW\nimpactDescription: O(n) instead of O(n log n)\ntags: javascript, arrays, performance, sorting, algorithms\n---\n\n## Use Loop for Min/Max Instead of Sort\n\nFinding the smallest or largest element only requires a single pass through the array. Sorting is wasteful and slower.\n\n**Incorrect (O(n log n) - sort to find latest):**\n\n```typescript\ninterface Project {\n  id: string\n  name: string\n  updatedAt: number\n}\n\nfunction getLatestProject(projects: Project[]) {\n  const sorted = [...projects].sort((a, b) => b.updatedAt - a.updatedAt)\n  return sorted[0]\n}\n```\n\nSorts the entire array just to find the maximum value.\n\n**Incorrect (O(n log n) - sort for oldest and newest):**\n\n```typescript\nfunction getOldestAndNewest(projects: Project[]) {\n  const sorted = [...projects].sort((a, b) => a.updatedAt - b.updatedAt)\n  return { oldest: sorted[0], newest: sorted[sorted.length - 1] }\n}\n```\n\nStill sorts unnecessarily when only min/max are needed.\n\n**Correct (O(n) - single loop):**\n\n```typescript\nfunction getLatestProject(projects: Project[]) {\n  if (projects.length === 0) return null\n  \n  let latest = projects[0]\n  \n  for (let i = 1; i < projects.length; i++) {\n    if (projects[i].updatedAt > latest.updatedAt) {\n      latest = projects[i]\n    }\n  }\n  \n  return latest\n}\n\nfunction getOldestAndNewest(projects: Project[]) {\n  if (projects.length === 0) return { oldest: null, newest: null }\n  \n  let oldest = projects[0]\n  let newest = projects[0]\n  \n  for (let i = 1; i < projects.length; i++) {\n    if (projects[i].updatedAt < oldest.updatedAt) oldest = projects[i]\n    if (projects[i].updatedAt > newest.updatedAt) newest = projects[i]\n  }\n  \n  return { oldest, newest }\n}\n```\n\nSingle pass through the array, no copying, no sorting.\n\n**Alternative (Math.min/Math.max for small arrays):**\n\n```typescript\nconst numbers = [5, 2, 8, 1, 9]\nconst min = Math.min(...numbers)\nconst max = Math.max(...numbers)\n```\n\nThis works for small arrays, but can be slower or just throw an error for very large arrays due to spread operator limitations. Maximal array length is approximately 124000 in Chrome 143 and 638000 in Safari 18; exact numbers may vary - see [the fiddle](https://jsfiddle.net/qw1jabsx/4/). Use the loop approach for reliability.\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-set-map-lookups.md",
    "content": "---\ntitle: Use Set/Map for O(1) Lookups\nimpact: LOW-MEDIUM\nimpactDescription: O(n) to O(1)\ntags: javascript, set, map, data-structures, performance\n---\n\n## Use Set/Map for O(1) Lookups\n\nConvert arrays to Set/Map for repeated membership checks.\n\n**Incorrect (O(n) per check):**\n\n```typescript\nconst allowedIds = ['a', 'b', 'c', ...]\nitems.filter(item => allowedIds.includes(item.id))\n```\n\n**Correct (O(1) per check):**\n\n```typescript\nconst allowedIds = new Set(['a', 'b', 'c', ...])\nitems.filter(item => allowedIds.has(item.id))\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/js-tosorted-immutable.md",
    "content": "---\ntitle: Use toSorted() Instead of sort() for Immutability\nimpact: MEDIUM-HIGH\nimpactDescription: prevents mutation bugs in React state\ntags: javascript, arrays, immutability, react, state, mutation\n---\n\n## Use toSorted() Instead of sort() for Immutability\n\n`.sort()` mutates the array in place, which can cause bugs with React state and props. Use `.toSorted()` to create a new sorted array without mutation.\n\n**Incorrect (mutates original array):**\n\n```typescript\nfunction UserList({ users }: { users: User[] }) {\n  // Mutates the users prop array!\n  const sorted = useMemo(\n    () => users.sort((a, b) => a.name.localeCompare(b.name)),\n    [users]\n  )\n  return <div>{sorted.map(renderUser)}</div>\n}\n```\n\n**Correct (creates new array):**\n\n```typescript\nfunction UserList({ users }: { users: User[] }) {\n  // Creates new sorted array, original unchanged\n  const sorted = useMemo(\n    () => users.toSorted((a, b) => a.name.localeCompare(b.name)),\n    [users]\n  )\n  return <div>{sorted.map(renderUser)}</div>\n}\n```\n\n**Why this matters in React:**\n\n1. Props/state mutations break React's immutability model - React expects props and state to be treated as read-only\n2. Causes stale closure bugs - Mutating arrays inside closures (callbacks, effects) can lead to unexpected behavior\n\n**Browser support (fallback for older browsers):**\n\n`.toSorted()` is available in all modern browsers (Chrome 110+, Safari 16+, Firefox 115+, Node.js 20+). For older environments, use spread operator:\n\n```typescript\n// Fallback for older browsers\nconst sorted = [...items].sort((a, b) => a.value - b.value)\n```\n\n**Other immutable array methods:**\n\n- `.toSorted()` - immutable sort\n- `.toReversed()` - immutable reverse\n- `.toSpliced()` - immutable splice\n- `.with()` - immutable element replacement\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-activity.md",
    "content": "---\ntitle: Use Activity Component for Show/Hide\nimpact: MEDIUM\nimpactDescription: preserves state/DOM\ntags: rendering, activity, visibility, state-preservation\n---\n\n## Use Activity Component for Show/Hide\n\nUse React's `<Activity>` to preserve state/DOM for expensive components that frequently toggle visibility.\n\n**Usage:**\n\n```tsx\nimport { Activity } from 'react'\n\nfunction Dropdown({ isOpen }: Props) {\n  return (\n    <Activity mode={isOpen ? 'visible' : 'hidden'}>\n      <ExpensiveMenu />\n    </Activity>\n  )\n}\n```\n\nAvoids expensive re-renders and state loss.\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-animate-svg-wrapper.md",
    "content": "---\ntitle: Animate SVG Wrapper Instead of SVG Element\nimpact: LOW\nimpactDescription: enables hardware acceleration\ntags: rendering, svg, css, animation, performance\n---\n\n## Animate SVG Wrapper Instead of SVG Element\n\nMany browsers don't have hardware acceleration for CSS3 animations on SVG elements. Wrap SVG in a `<div>` and animate the wrapper instead.\n\n**Incorrect (animating SVG directly - no hardware acceleration):**\n\n```tsx\nfunction LoadingSpinner() {\n  return (\n    <svg \n      className=\"animate-spin\"\n      width=\"24\" \n      height=\"24\" \n      viewBox=\"0 0 24 24\"\n    >\n      <circle cx=\"12\" cy=\"12\" r=\"10\" stroke=\"currentColor\" />\n    </svg>\n  )\n}\n```\n\n**Correct (animating wrapper div - hardware accelerated):**\n\n```tsx\nfunction LoadingSpinner() {\n  return (\n    <div className=\"animate-spin\">\n      <svg \n        width=\"24\" \n        height=\"24\" \n        viewBox=\"0 0 24 24\"\n      >\n        <circle cx=\"12\" cy=\"12\" r=\"10\" stroke=\"currentColor\" />\n      </svg>\n    </div>\n  )\n}\n```\n\nThis applies to all CSS transforms and transitions (`transform`, `opacity`, `translate`, `scale`, `rotate`). The wrapper div allows browsers to use GPU acceleration for smoother animations.\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-conditional-render.md",
    "content": "---\ntitle: Use Explicit Conditional Rendering\nimpact: LOW\nimpactDescription: prevents rendering 0 or NaN\ntags: rendering, conditional, jsx, falsy-values\n---\n\n## Use Explicit Conditional Rendering\n\nUse explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be `0`, `NaN`, or other falsy values that render.\n\n**Incorrect (renders \"0\" when count is 0):**\n\n```tsx\nfunction Badge({ count }: { count: number }) {\n  return (\n    <div>\n      {count && <span className=\"badge\">{count}</span>}\n    </div>\n  )\n}\n\n// When count = 0, renders: <div>0</div>\n// When count = 5, renders: <div><span class=\"badge\">5</span></div>\n```\n\n**Correct (renders nothing when count is 0):**\n\n```tsx\nfunction Badge({ count }: { count: number }) {\n  return (\n    <div>\n      {count > 0 ? <span className=\"badge\">{count}</span> : null}\n    </div>\n  )\n}\n\n// When count = 0, renders: <div></div>\n// When count = 5, renders: <div><span class=\"badge\">5</span></div>\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-content-visibility.md",
    "content": "---\ntitle: CSS content-visibility for Long Lists\nimpact: HIGH\nimpactDescription: faster initial render\ntags: rendering, css, content-visibility, long-lists\n---\n\n## CSS content-visibility for Long Lists\n\nApply `content-visibility: auto` to defer off-screen rendering.\n\n**CSS:**\n\n```css\n.message-item {\n  content-visibility: auto;\n  contain-intrinsic-size: 0 80px;\n}\n```\n\n**Example:**\n\n```tsx\nfunction MessageList({ messages }: { messages: Message[] }) {\n  return (\n    <div className=\"overflow-y-auto h-screen\">\n      {messages.map(msg => (\n        <div key={msg.id} className=\"message-item\">\n          <Avatar user={msg.author} />\n          <div>{msg.content}</div>\n        </div>\n      ))}\n    </div>\n  )\n}\n```\n\nFor 1000 messages, browser skips layout/paint for ~990 off-screen items (10× faster initial render).\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-hoist-jsx.md",
    "content": "---\ntitle: Hoist Static JSX Elements\nimpact: LOW\nimpactDescription: avoids re-creation\ntags: rendering, jsx, static, optimization\n---\n\n## Hoist Static JSX Elements\n\nExtract static JSX outside components to avoid re-creation.\n\n**Incorrect (recreates element every render):**\n\n```tsx\nfunction LoadingSkeleton() {\n  return <div className=\"animate-pulse h-20 bg-gray-200\" />\n}\n\nfunction Container() {\n  return (\n    <div>\n      {loading && <LoadingSkeleton />}\n    </div>\n  )\n}\n```\n\n**Correct (reuses same element):**\n\n```tsx\nconst loadingSkeleton = (\n  <div className=\"animate-pulse h-20 bg-gray-200\" />\n)\n\nfunction Container() {\n  return (\n    <div>\n      {loading && loadingSkeleton}\n    </div>\n  )\n}\n```\n\nThis is especially helpful for large and static SVG nodes, which can be expensive to recreate on every render.\n\n**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler automatically hoists static JSX elements and optimizes component re-renders, making manual hoisting unnecessary.\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-hydration-no-flicker.md",
    "content": "---\ntitle: Prevent Hydration Mismatch Without Flickering\nimpact: MEDIUM\nimpactDescription: avoids visual flicker and hydration errors\ntags: rendering, ssr, hydration, localStorage, flicker\n---\n\n## Prevent Hydration Mismatch Without Flickering\n\nWhen rendering content that depends on client-side storage (localStorage, cookies), avoid both SSR breakage and post-hydration flickering by injecting a synchronous script that updates the DOM before React hydrates.\n\n**Incorrect (breaks SSR):**\n\n```tsx\nfunction ThemeWrapper({ children }: { children: ReactNode }) {\n  // localStorage is not available on server - throws error\n  const theme = localStorage.getItem('theme') || 'light'\n  \n  return (\n    <div className={theme}>\n      {children}\n    </div>\n  )\n}\n```\n\nServer-side rendering will fail because `localStorage` is undefined.\n\n**Incorrect (visual flickering):**\n\n```tsx\nfunction ThemeWrapper({ children }: { children: ReactNode }) {\n  const [theme, setTheme] = useState('light')\n  \n  useEffect(() => {\n    // Runs after hydration - causes visible flash\n    const stored = localStorage.getItem('theme')\n    if (stored) {\n      setTheme(stored)\n    }\n  }, [])\n  \n  return (\n    <div className={theme}>\n      {children}\n    </div>\n  )\n}\n```\n\nComponent first renders with default value (`light`), then updates after hydration, causing a visible flash of incorrect content.\n\n**Correct (no flicker, no hydration mismatch):**\n\n```tsx\nfunction ThemeWrapper({ children }: { children: ReactNode }) {\n  return (\n    <>\n      <div id=\"theme-wrapper\">\n        {children}\n      </div>\n      <script\n        dangerouslySetInnerHTML={{\n          __html: `\n            (function() {\n              try {\n                var theme = localStorage.getItem('theme') || 'light';\n                var el = document.getElementById('theme-wrapper');\n                if (el) el.className = theme;\n              } catch (e) {}\n            })();\n          `,\n        }}\n      />\n    </>\n  )\n}\n```\n\nThe inline script executes synchronously before showing the element, ensuring the DOM already has the correct value. No flickering, no hydration mismatch.\n\nThis pattern is especially useful for theme toggles, user preferences, authentication states, and any client-only data that should render immediately without flashing default values.\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-hydration-suppress-warning.md",
    "content": "---\ntitle: Suppress Expected Hydration Mismatches\nimpact: LOW-MEDIUM\nimpactDescription: avoids noisy hydration warnings for known differences\ntags: rendering, hydration, ssr, nextjs\n---\n\n## Suppress Expected Hydration Mismatches\n\nIn SSR frameworks (e.g., Next.js), some values are intentionally different on server vs client (random IDs, dates, locale/timezone formatting). For these *expected* mismatches, wrap the dynamic text in an element with `suppressHydrationWarning` to prevent noisy warnings. Do not use this to hide real bugs. Don’t overuse it.\n\n**Incorrect (known mismatch warnings):**\n\n```tsx\nfunction Timestamp() {\n  return <span>{new Date().toLocaleString()}</span>\n}\n```\n\n**Correct (suppress expected mismatch only):**\n\n```tsx\nfunction Timestamp() {\n  return (\n    <span suppressHydrationWarning>\n      {new Date().toLocaleString()}\n    </span>\n  )\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-resource-hints.md",
    "content": "---\ntitle: Use React DOM Resource Hints\nimpact: HIGH\nimpactDescription: reduces load time for critical resources\ntags: rendering, preload, preconnect, prefetch, resource-hints\n---\n\n## Use React DOM Resource Hints\n\n**Impact: HIGH (reduces load time for critical resources)**\n\nReact DOM provides APIs to hint the browser about resources it will need. These are especially useful in server components to start loading resources before the client even receives the HTML.\n\n- **`prefetchDNS(href)`**: Resolve DNS for a domain you expect to connect to\n- **`preconnect(href)`**: Establish connection (DNS + TCP + TLS) to a server\n- **`preload(href, options)`**: Fetch a resource (stylesheet, font, script, image) you'll use soon\n- **`preloadModule(href)`**: Fetch an ES module you'll use soon\n- **`preinit(href, options)`**: Fetch and evaluate a stylesheet or script\n- **`preinitModule(href)`**: Fetch and evaluate an ES module\n\n**Example (preconnect to third-party APIs):**\n\n```tsx\nimport { preconnect, prefetchDNS } from 'react-dom'\n\nexport default function App() {\n  prefetchDNS('https://analytics.example.com')\n  preconnect('https://api.example.com')\n\n  return <main>{/* content */}</main>\n}\n```\n\n**Example (preload critical fonts and styles):**\n\n```tsx\nimport { preload, preinit } from 'react-dom'\n\nexport default function RootLayout({ children }) {\n  // Preload font file\n  preload('/fonts/inter.woff2', { as: 'font', type: 'font/woff2', crossOrigin: 'anonymous' })\n\n  // Fetch and apply critical stylesheet immediately\n  preinit('/styles/critical.css', { as: 'style' })\n\n  return (\n    <html>\n      <body>{children}</body>\n    </html>\n  )\n}\n```\n\n**Example (preload modules for code-split routes):**\n\n```tsx\nimport { preloadModule, preinitModule } from 'react-dom'\n\nfunction Navigation() {\n  const preloadDashboard = () => {\n    preloadModule('/dashboard.js', { as: 'script' })\n  }\n\n  return (\n    <nav>\n      <a href=\"/dashboard\" onMouseEnter={preloadDashboard}>\n        Dashboard\n      </a>\n    </nav>\n  )\n}\n```\n\n**When to use each:**\n\n| API | Use case |\n|-----|----------|\n| `prefetchDNS` | Third-party domains you'll connect to later |\n| `preconnect` | APIs or CDNs you'll fetch from immediately |\n| `preload` | Critical resources needed for current page |\n| `preloadModule` | JS modules for likely next navigation |\n| `preinit` | Stylesheets/scripts that must execute early |\n| `preinitModule` | ES modules that must execute early |\n\nReference: [React DOM Resource Preloading APIs](https://react.dev/reference/react-dom#resource-preloading-apis)\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-script-defer-async.md",
    "content": "---\ntitle: Use defer or async on Script Tags\nimpact: HIGH\nimpactDescription: eliminates render-blocking\ntags: rendering, script, defer, async, performance\n---\n\n## Use defer or async on Script Tags\n\n**Impact: HIGH (eliminates render-blocking)**\n\nScript tags without `defer` or `async` block HTML parsing while the script downloads and executes. This delays First Contentful Paint and Time to Interactive.\n\n- **`defer`**: Downloads in parallel, executes after HTML parsing completes, maintains execution order\n- **`async`**: Downloads in parallel, executes immediately when ready, no guaranteed order\n\nUse `defer` for scripts that depend on DOM or other scripts. Use `async` for independent scripts like analytics.\n\n**Incorrect (blocks rendering):**\n\n```tsx\nexport default function Document() {\n  return (\n    <html>\n      <head>\n        <script src=\"https://example.com/analytics.js\" />\n        <script src=\"/scripts/utils.js\" />\n      </head>\n      <body>{/* content */}</body>\n    </html>\n  )\n}\n```\n\n**Correct (non-blocking):**\n\n```tsx\nexport default function Document() {\n  return (\n    <html>\n      <head>\n        {/* Independent script - use async */}\n        <script src=\"https://example.com/analytics.js\" async />\n        {/* DOM-dependent script - use defer */}\n        <script src=\"/scripts/utils.js\" defer />\n      </head>\n      <body>{/* content */}</body>\n    </html>\n  )\n}\n```\n\n**Note:** In Next.js, prefer the `next/script` component with `strategy` prop instead of raw script tags:\n\n```tsx\nimport Script from 'next/script'\n\nexport default function Page() {\n  return (\n    <>\n      <Script src=\"https://example.com/analytics.js\" strategy=\"afterInteractive\" />\n      <Script src=\"/scripts/utils.js\" strategy=\"beforeInteractive\" />\n    </>\n  )\n}\n```\n\nReference: [MDN - Script element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#defer)\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-svg-precision.md",
    "content": "---\ntitle: Optimize SVG Precision\nimpact: LOW\nimpactDescription: reduces file size\ntags: rendering, svg, optimization, svgo\n---\n\n## Optimize SVG Precision\n\nReduce SVG coordinate precision to decrease file size. The optimal precision depends on the viewBox size, but in general reducing precision should be considered.\n\n**Incorrect (excessive precision):**\n\n```svg\n<path d=\"M 10.293847 20.847362 L 30.938472 40.192837\" />\n```\n\n**Correct (1 decimal place):**\n\n```svg\n<path d=\"M 10.3 20.8 L 30.9 40.2\" />\n```\n\n**Automate with SVGO:**\n\n```bash\nnpx svgo --precision=1 --multipass icon.svg\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rendering-usetransition-loading.md",
    "content": "---\ntitle: Use useTransition Over Manual Loading States\nimpact: LOW\nimpactDescription: reduces re-renders and improves code clarity\ntags: rendering, transitions, useTransition, loading, state\n---\n\n## Use useTransition Over Manual Loading States\n\nUse `useTransition` instead of manual `useState` for loading states. This provides built-in `isPending` state and automatically manages transitions.\n\n**Incorrect (manual loading state):**\n\n```tsx\nfunction SearchResults() {\n  const [query, setQuery] = useState('')\n  const [results, setResults] = useState([])\n  const [isLoading, setIsLoading] = useState(false)\n\n  const handleSearch = async (value: string) => {\n    setIsLoading(true)\n    setQuery(value)\n    const data = await fetchResults(value)\n    setResults(data)\n    setIsLoading(false)\n  }\n\n  return (\n    <>\n      <input onChange={(e) => handleSearch(e.target.value)} />\n      {isLoading && <Spinner />}\n      <ResultsList results={results} />\n    </>\n  )\n}\n```\n\n**Correct (useTransition with built-in pending state):**\n\n```tsx\nimport { useTransition, useState } from 'react'\n\nfunction SearchResults() {\n  const [query, setQuery] = useState('')\n  const [results, setResults] = useState([])\n  const [isPending, startTransition] = useTransition()\n\n  const handleSearch = (value: string) => {\n    setQuery(value) // Update input immediately\n    \n    startTransition(async () => {\n      // Fetch and update results\n      const data = await fetchResults(value)\n      setResults(data)\n    })\n  }\n\n  return (\n    <>\n      <input onChange={(e) => handleSearch(e.target.value)} />\n      {isPending && <Spinner />}\n      <ResultsList results={results} />\n    </>\n  )\n}\n```\n\n**Benefits:**\n\n- **Automatic pending state**: No need to manually manage `setIsLoading(true/false)`\n- **Error resilience**: Pending state correctly resets even if the transition throws\n- **Better responsiveness**: Keeps the UI responsive during updates\n- **Interrupt handling**: New transitions automatically cancel pending ones\n\nReference: [useTransition](https://react.dev/reference/react/useTransition)\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-defer-reads.md",
    "content": "---\ntitle: Defer State Reads to Usage Point\nimpact: MEDIUM\nimpactDescription: avoids unnecessary subscriptions\ntags: rerender, searchParams, localStorage, optimization\n---\n\n## Defer State Reads to Usage Point\n\nDon't subscribe to dynamic state (searchParams, localStorage) if you only read it inside callbacks.\n\n**Incorrect (subscribes to all searchParams changes):**\n\n```tsx\nfunction ShareButton({ chatId }: { chatId: string }) {\n  const searchParams = useSearchParams()\n\n  const handleShare = () => {\n    const ref = searchParams.get('ref')\n    shareChat(chatId, { ref })\n  }\n\n  return <button onClick={handleShare}>Share</button>\n}\n```\n\n**Correct (reads on demand, no subscription):**\n\n```tsx\nfunction ShareButton({ chatId }: { chatId: string }) {\n  const handleShare = () => {\n    const params = new URLSearchParams(window.location.search)\n    const ref = params.get('ref')\n    shareChat(chatId, { ref })\n  }\n\n  return <button onClick={handleShare}>Share</button>\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-dependencies.md",
    "content": "---\ntitle: Narrow Effect Dependencies\nimpact: LOW\nimpactDescription: minimizes effect re-runs\ntags: rerender, useEffect, dependencies, optimization\n---\n\n## Narrow Effect Dependencies\n\nSpecify primitive dependencies instead of objects to minimize effect re-runs.\n\n**Incorrect (re-runs on any user field change):**\n\n```tsx\nuseEffect(() => {\n  console.log(user.id)\n}, [user])\n```\n\n**Correct (re-runs only when id changes):**\n\n```tsx\nuseEffect(() => {\n  console.log(user.id)\n}, [user.id])\n```\n\n**For derived state, compute outside effect:**\n\n```tsx\n// Incorrect: runs on width=767, 766, 765...\nuseEffect(() => {\n  if (width < 768) {\n    enableMobileMode()\n  }\n}, [width])\n\n// Correct: runs only on boolean transition\nconst isMobile = width < 768\nuseEffect(() => {\n  if (isMobile) {\n    enableMobileMode()\n  }\n}, [isMobile])\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-derived-state-no-effect.md",
    "content": "---\ntitle: Calculate Derived State During Rendering\nimpact: MEDIUM\nimpactDescription: avoids redundant renders and state drift\ntags: rerender, derived-state, useEffect, state\n---\n\n## Calculate Derived State During Rendering\n\nIf a value can be computed from current props/state, do not store it in state or update it in an effect. Derive it during render to avoid extra renders and state drift. Do not set state in effects solely in response to prop changes; prefer derived values or keyed resets instead.\n\n**Incorrect (redundant state and effect):**\n\n```tsx\nfunction Form() {\n  const [firstName, setFirstName] = useState('First')\n  const [lastName, setLastName] = useState('Last')\n  const [fullName, setFullName] = useState('')\n\n  useEffect(() => {\n    setFullName(firstName + ' ' + lastName)\n  }, [firstName, lastName])\n\n  return <p>{fullName}</p>\n}\n```\n\n**Correct (derive during render):**\n\n```tsx\nfunction Form() {\n  const [firstName, setFirstName] = useState('First')\n  const [lastName, setLastName] = useState('Last')\n  const fullName = firstName + ' ' + lastName\n\n  return <p>{fullName}</p>\n}\n```\n\nReferences: [You Might Not Need an Effect](https://react.dev/learn/you-might-not-need-an-effect)\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-derived-state.md",
    "content": "---\ntitle: Subscribe to Derived State\nimpact: MEDIUM\nimpactDescription: reduces re-render frequency\ntags: rerender, derived-state, media-query, optimization\n---\n\n## Subscribe to Derived State\n\nSubscribe to derived boolean state instead of continuous values to reduce re-render frequency.\n\n**Incorrect (re-renders on every pixel change):**\n\n```tsx\nfunction Sidebar() {\n  const width = useWindowWidth()  // updates continuously\n  const isMobile = width < 768\n  return <nav className={isMobile ? 'mobile' : 'desktop'} />\n}\n```\n\n**Correct (re-renders only when boolean changes):**\n\n```tsx\nfunction Sidebar() {\n  const isMobile = useMediaQuery('(max-width: 767px)')\n  return <nav className={isMobile ? 'mobile' : 'desktop'} />\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-functional-setstate.md",
    "content": "---\ntitle: Use Functional setState Updates\nimpact: MEDIUM\nimpactDescription: prevents stale closures and unnecessary callback recreations\ntags: react, hooks, useState, useCallback, callbacks, closures\n---\n\n## Use Functional setState Updates\n\nWhen updating state based on the current state value, use the functional update form of setState instead of directly referencing the state variable. This prevents stale closures, eliminates unnecessary dependencies, and creates stable callback references.\n\n**Incorrect (requires state as dependency):**\n\n```tsx\nfunction TodoList() {\n  const [items, setItems] = useState(initialItems)\n  \n  // Callback must depend on items, recreated on every items change\n  const addItems = useCallback((newItems: Item[]) => {\n    setItems([...items, ...newItems])\n  }, [items])  // ❌ items dependency causes recreations\n  \n  // Risk of stale closure if dependency is forgotten\n  const removeItem = useCallback((id: string) => {\n    setItems(items.filter(item => item.id !== id))\n  }, [])  // ❌ Missing items dependency - will use stale items!\n  \n  return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />\n}\n```\n\nThe first callback is recreated every time `items` changes, which can cause child components to re-render unnecessarily. The second callback has a stale closure bug—it will always reference the initial `items` value.\n\n**Correct (stable callbacks, no stale closures):**\n\n```tsx\nfunction TodoList() {\n  const [items, setItems] = useState(initialItems)\n  \n  // Stable callback, never recreated\n  const addItems = useCallback((newItems: Item[]) => {\n    setItems(curr => [...curr, ...newItems])\n  }, [])  // ✅ No dependencies needed\n  \n  // Always uses latest state, no stale closure risk\n  const removeItem = useCallback((id: string) => {\n    setItems(curr => curr.filter(item => item.id !== id))\n  }, [])  // ✅ Safe and stable\n  \n  return <ItemsEditor items={items} onAdd={addItems} onRemove={removeItem} />\n}\n```\n\n**Benefits:**\n\n1. **Stable callback references** - Callbacks don't need to be recreated when state changes\n2. **No stale closures** - Always operates on the latest state value\n3. **Fewer dependencies** - Simplifies dependency arrays and reduces memory leaks\n4. **Prevents bugs** - Eliminates the most common source of React closure bugs\n\n**When to use functional updates:**\n\n- Any setState that depends on the current state value\n- Inside useCallback/useMemo when state is needed\n- Event handlers that reference state\n- Async operations that update state\n\n**When direct updates are fine:**\n\n- Setting state to a static value: `setCount(0)`\n- Setting state from props/arguments only: `setName(newName)`\n- State doesn't depend on previous value\n\n**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler can automatically optimize some cases, but functional updates are still recommended for correctness and to prevent stale closure bugs.\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-lazy-state-init.md",
    "content": "---\ntitle: Use Lazy State Initialization\nimpact: MEDIUM\nimpactDescription: wasted computation on every render\ntags: react, hooks, useState, performance, initialization\n---\n\n## Use Lazy State Initialization\n\nPass a function to `useState` for expensive initial values. Without the function form, the initializer runs on every render even though the value is only used once.\n\n**Incorrect (runs on every render):**\n\n```tsx\nfunction FilteredList({ items }: { items: Item[] }) {\n  // buildSearchIndex() runs on EVERY render, even after initialization\n  const [searchIndex, setSearchIndex] = useState(buildSearchIndex(items))\n  const [query, setQuery] = useState('')\n  \n  // When query changes, buildSearchIndex runs again unnecessarily\n  return <SearchResults index={searchIndex} query={query} />\n}\n\nfunction UserProfile() {\n  // JSON.parse runs on every render\n  const [settings, setSettings] = useState(\n    JSON.parse(localStorage.getItem('settings') || '{}')\n  )\n  \n  return <SettingsForm settings={settings} onChange={setSettings} />\n}\n```\n\n**Correct (runs only once):**\n\n```tsx\nfunction FilteredList({ items }: { items: Item[] }) {\n  // buildSearchIndex() runs ONLY on initial render\n  const [searchIndex, setSearchIndex] = useState(() => buildSearchIndex(items))\n  const [query, setQuery] = useState('')\n  \n  return <SearchResults index={searchIndex} query={query} />\n}\n\nfunction UserProfile() {\n  // JSON.parse runs only on initial render\n  const [settings, setSettings] = useState(() => {\n    const stored = localStorage.getItem('settings')\n    return stored ? JSON.parse(stored) : {}\n  })\n  \n  return <SettingsForm settings={settings} onChange={setSettings} />\n}\n```\n\nUse lazy initialization when computing initial values from localStorage/sessionStorage, building data structures (indexes, maps), reading from the DOM, or performing heavy transformations.\n\nFor simple primitives (`useState(0)`), direct references (`useState(props.value)`), or cheap literals (`useState({})`), the function form is unnecessary.\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-memo-with-default-value.md",
    "content": "---\n\ntitle: Extract Default Non-primitive Parameter Value from Memoized Component to Constant\nimpact: MEDIUM\nimpactDescription: restores memoization by using a constant for default value\ntags: rerender, memo, optimization\n\n---\n\n## Extract Default Non-primitive Parameter Value from Memoized Component to Constant\n\nWhen memoized component has a default value for some non-primitive optional parameter, such as an array, function, or object, calling the component without that parameter results in broken memoization. This is because new value instances are created on every rerender, and they do not pass strict equality comparison in `memo()`.\n\nTo address this issue, extract the default value into a constant.\n\n**Incorrect (`onClick` has different values on every rerender):**\n\n```tsx\nconst UserAvatar = memo(function UserAvatar({ onClick = () => {} }: { onClick?: () => void }) {\n  // ...\n})\n\n// Used without optional onClick\n<UserAvatar />\n```\n\n**Correct (stable default value):**\n\n```tsx\nconst NOOP = () => {};\n\nconst UserAvatar = memo(function UserAvatar({ onClick = NOOP }: { onClick?: () => void }) {\n  // ...\n})\n\n// Used without optional onClick\n<UserAvatar />\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-memo.md",
    "content": "---\ntitle: Extract to Memoized Components\nimpact: MEDIUM\nimpactDescription: enables early returns\ntags: rerender, memo, useMemo, optimization\n---\n\n## Extract to Memoized Components\n\nExtract expensive work into memoized components to enable early returns before computation.\n\n**Incorrect (computes avatar even when loading):**\n\n```tsx\nfunction Profile({ user, loading }: Props) {\n  const avatar = useMemo(() => {\n    const id = computeAvatarId(user)\n    return <Avatar id={id} />\n  }, [user])\n\n  if (loading) return <Skeleton />\n  return <div>{avatar}</div>\n}\n```\n\n**Correct (skips computation when loading):**\n\n```tsx\nconst UserAvatar = memo(function UserAvatar({ user }: { user: User }) {\n  const id = useMemo(() => computeAvatarId(user), [user])\n  return <Avatar id={id} />\n})\n\nfunction Profile({ user, loading }: Props) {\n  if (loading) return <Skeleton />\n  return (\n    <div>\n      <UserAvatar user={user} />\n    </div>\n  )\n}\n```\n\n**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, manual memoization with `memo()` and `useMemo()` is not necessary. The compiler automatically optimizes re-renders.\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-move-effect-to-event.md",
    "content": "---\ntitle: Put Interaction Logic in Event Handlers\nimpact: MEDIUM\nimpactDescription: avoids effect re-runs and duplicate side effects\ntags: rerender, useEffect, events, side-effects, dependencies\n---\n\n## Put Interaction Logic in Event Handlers\n\nIf a side effect is triggered by a specific user action (submit, click, drag), run it in that event handler. Do not model the action as state + effect; it makes effects re-run on unrelated changes and can duplicate the action.\n\n**Incorrect (event modeled as state + effect):**\n\n```tsx\nfunction Form() {\n  const [submitted, setSubmitted] = useState(false)\n  const theme = useContext(ThemeContext)\n\n  useEffect(() => {\n    if (submitted) {\n      post('/api/register')\n      showToast('Registered', theme)\n    }\n  }, [submitted, theme])\n\n  return <button onClick={() => setSubmitted(true)}>Submit</button>\n}\n```\n\n**Correct (do it in the handler):**\n\n```tsx\nfunction Form() {\n  const theme = useContext(ThemeContext)\n\n  function handleSubmit() {\n    post('/api/register')\n    showToast('Registered', theme)\n  }\n\n  return <button onClick={handleSubmit}>Submit</button>\n}\n```\n\nReference: [Should this code move to an event handler?](https://react.dev/learn/removing-effect-dependencies#should-this-code-move-to-an-event-handler)\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-no-inline-components.md",
    "content": "---\ntitle: Don't Define Components Inside Components\nimpact: HIGH\nimpactDescription: prevents remount on every render\ntags: rerender, components, remount, performance\n---\n\n## Don't Define Components Inside Components\n\n**Impact: HIGH (prevents remount on every render)**\n\nDefining a component inside another component creates a new component type on every render. React sees a different component each time and fully remounts it, destroying all state and DOM.\n\nA common reason developers do this is to access parent variables without passing props. Always pass props instead.\n\n**Incorrect (remounts on every render):**\n\n```tsx\nfunction UserProfile({ user, theme }) {\n  // Defined inside to access `theme` - BAD\n  const Avatar = () => (\n    <img\n      src={user.avatarUrl}\n      className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}\n    />\n  )\n\n  // Defined inside to access `user` - BAD\n  const Stats = () => (\n    <div>\n      <span>{user.followers} followers</span>\n      <span>{user.posts} posts</span>\n    </div>\n  )\n\n  return (\n    <div>\n      <Avatar />\n      <Stats />\n    </div>\n  )\n}\n```\n\nEvery time `UserProfile` renders, `Avatar` and `Stats` are new component types. React unmounts the old instances and mounts new ones, losing any internal state, running effects again, and recreating DOM nodes.\n\n**Correct (pass props instead):**\n\n```tsx\nfunction Avatar({ src, theme }: { src: string; theme: string }) {\n  return (\n    <img\n      src={src}\n      className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}\n    />\n  )\n}\n\nfunction Stats({ followers, posts }: { followers: number; posts: number }) {\n  return (\n    <div>\n      <span>{followers} followers</span>\n      <span>{posts} posts</span>\n    </div>\n  )\n}\n\nfunction UserProfile({ user, theme }) {\n  return (\n    <div>\n      <Avatar src={user.avatarUrl} theme={theme} />\n      <Stats followers={user.followers} posts={user.posts} />\n    </div>\n  )\n}\n```\n\n**Symptoms of this bug:**\n- Input fields lose focus on every keystroke\n- Animations restart unexpectedly\n- `useEffect` cleanup/setup runs on every parent render\n- Scroll position resets inside the component\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-simple-expression-in-memo.md",
    "content": "---\ntitle: Do not wrap a simple expression with a primitive result type in useMemo\nimpact: LOW-MEDIUM\nimpactDescription: wasted computation on every render\ntags: rerender, useMemo, optimization\n---\n\n## Do not wrap a simple expression with a primitive result type in useMemo\n\nWhen an expression is simple (few logical or arithmetical operators) and has a primitive result type (boolean, number, string), do not wrap it in `useMemo`.\nCalling `useMemo` and comparing hook dependencies may consume more resources than the expression itself.\n\n**Incorrect:**\n\n```tsx\nfunction Header({ user, notifications }: Props) {\n  const isLoading = useMemo(() => {\n    return user.isLoading || notifications.isLoading\n  }, [user.isLoading, notifications.isLoading])\n\n  if (isLoading) return <Skeleton />\n  // return some markup\n}\n```\n\n**Correct:**\n\n```tsx\nfunction Header({ user, notifications }: Props) {\n  const isLoading = user.isLoading || notifications.isLoading\n\n  if (isLoading) return <Skeleton />\n  // return some markup\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-split-combined-hooks.md",
    "content": "---\ntitle: Split Combined Hook Computations\nimpact: MEDIUM\nimpactDescription: avoids recomputing independent steps\ntags: rerender, useMemo, useEffect, dependencies, optimization\n---\n\n## Split Combined Hook Computations\n\nWhen a hook contains multiple independent tasks with different dependencies, split them into separate hooks. A combined hook reruns all tasks when any dependency changes, even if some tasks don't use the changed value.\n\n**Incorrect (changing `sortOrder` recomputes filtering):**\n\n```tsx\nconst sortedProducts = useMemo(() => {\n  const filtered = products.filter((p) => p.category === category)\n  const sorted = filtered.toSorted((a, b) =>\n    sortOrder === \"asc\" ? a.price - b.price : b.price - a.price\n  )\n  return sorted\n}, [products, category, sortOrder])\n```\n\n**Correct (filtering only recomputes when products or category change):**\n\n```tsx\nconst filteredProducts = useMemo(\n  () => products.filter((p) => p.category === category),\n  [products, category]\n)\n\nconst sortedProducts = useMemo(\n  () =>\n    filteredProducts.toSorted((a, b) =>\n      sortOrder === \"asc\" ? a.price - b.price : b.price - a.price\n    ),\n  [filteredProducts, sortOrder]\n)\n```\n\nThis pattern also applies to `useEffect` when combining unrelated side effects:\n\n**Incorrect (both effects run when either dependency changes):**\n\n```tsx\nuseEffect(() => {\n  analytics.trackPageView(pathname)\n  document.title = `${pageTitle} | My App`\n}, [pathname, pageTitle])\n```\n\n**Correct (effects run independently):**\n\n```tsx\nuseEffect(() => {\n  analytics.trackPageView(pathname)\n}, [pathname])\n\nuseEffect(() => {\n  document.title = `${pageTitle} | My App`\n}, [pageTitle])\n```\n\n**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, it automatically optimizes dependency tracking and may handle some of these cases for you.\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-transitions.md",
    "content": "---\ntitle: Use Transitions for Non-Urgent Updates\nimpact: MEDIUM\nimpactDescription: maintains UI responsiveness\ntags: rerender, transitions, startTransition, performance\n---\n\n## Use Transitions for Non-Urgent Updates\n\nMark frequent, non-urgent state updates as transitions to maintain UI responsiveness.\n\n**Incorrect (blocks UI on every scroll):**\n\n```tsx\nfunction ScrollTracker() {\n  const [scrollY, setScrollY] = useState(0)\n  useEffect(() => {\n    const handler = () => setScrollY(window.scrollY)\n    window.addEventListener('scroll', handler, { passive: true })\n    return () => window.removeEventListener('scroll', handler)\n  }, [])\n}\n```\n\n**Correct (non-blocking updates):**\n\n```tsx\nimport { startTransition } from 'react'\n\nfunction ScrollTracker() {\n  const [scrollY, setScrollY] = useState(0)\n  useEffect(() => {\n    const handler = () => {\n      startTransition(() => setScrollY(window.scrollY))\n    }\n    window.addEventListener('scroll', handler, { passive: true })\n    return () => window.removeEventListener('scroll', handler)\n  }, [])\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-use-deferred-value.md",
    "content": "---\ntitle: Use useDeferredValue for Expensive Derived Renders\nimpact: MEDIUM\nimpactDescription: keeps input responsive during heavy computation\ntags: rerender, useDeferredValue, optimization, concurrent\n---\n\n## Use useDeferredValue for Expensive Derived Renders\n\nWhen user input triggers expensive computations or renders, use `useDeferredValue` to keep the input responsive. The deferred value lags behind, allowing React to prioritize the input update and render the expensive result when idle.\n\n**Incorrect (input feels laggy while filtering):**\n\n```tsx\nfunction Search({ items }: { items: Item[] }) {\n  const [query, setQuery] = useState('')\n  const filtered = items.filter(item => fuzzyMatch(item, query))\n\n  return (\n    <>\n      <input value={query} onChange={e => setQuery(e.target.value)} />\n      <ResultsList results={filtered} />\n    </>\n  )\n}\n```\n\n**Correct (input stays snappy, results render when ready):**\n\n```tsx\nfunction Search({ items }: { items: Item[] }) {\n  const [query, setQuery] = useState('')\n  const deferredQuery = useDeferredValue(query)\n  const filtered = useMemo(\n    () => items.filter(item => fuzzyMatch(item, deferredQuery)),\n    [items, deferredQuery]\n  )\n  const isStale = query !== deferredQuery\n\n  return (\n    <>\n      <input value={query} onChange={e => setQuery(e.target.value)} />\n      <div style={{ opacity: isStale ? 0.7 : 1 }}>\n        <ResultsList results={filtered} />\n      </div>\n    </>\n  )\n}\n```\n\n**When to use:**\n\n- Filtering/searching large lists\n- Expensive visualizations (charts, graphs) reacting to input\n- Any derived state that causes noticeable render delays\n\n**Note:** Wrap the expensive computation in `useMemo` with the deferred value as a dependency, otherwise it still runs on every render.\n\nReference: [React useDeferredValue](https://react.dev/reference/react/useDeferredValue)\n"
  },
  {
    "path": "skills/react-best-practices/rules/rerender-use-ref-transient-values.md",
    "content": "---\ntitle: Use useRef for Transient Values\nimpact: MEDIUM\nimpactDescription: avoids unnecessary re-renders on frequent updates\ntags: rerender, useref, state, performance\n---\n\n## Use useRef for Transient Values\n\nWhen a value changes frequently and you don't want a re-render on every update (e.g., mouse trackers, intervals, transient flags), store it in `useRef` instead of `useState`. Keep component state for UI; use refs for temporary DOM-adjacent values. Updating a ref does not trigger a re-render.\n\n**Incorrect (renders every update):**\n\n```tsx\nfunction Tracker() {\n  const [lastX, setLastX] = useState(0)\n\n  useEffect(() => {\n    const onMove = (e: MouseEvent) => setLastX(e.clientX)\n    window.addEventListener('mousemove', onMove)\n    return () => window.removeEventListener('mousemove', onMove)\n  }, [])\n\n  return (\n    <div\n      style={{\n        position: 'fixed',\n        top: 0,\n        left: lastX,\n        width: 8,\n        height: 8,\n        background: 'black',\n      }}\n    />\n  )\n}\n```\n\n**Correct (no re-render for tracking):**\n\n```tsx\nfunction Tracker() {\n  const lastXRef = useRef(0)\n  const dotRef = useRef<HTMLDivElement>(null)\n\n  useEffect(() => {\n    const onMove = (e: MouseEvent) => {\n      lastXRef.current = e.clientX\n      const node = dotRef.current\n      if (node) {\n        node.style.transform = `translateX(${e.clientX}px)`\n      }\n    }\n    window.addEventListener('mousemove', onMove)\n    return () => window.removeEventListener('mousemove', onMove)\n  }, [])\n\n  return (\n    <div\n      ref={dotRef}\n      style={{\n        position: 'fixed',\n        top: 0,\n        left: 0,\n        width: 8,\n        height: 8,\n        background: 'black',\n        transform: 'translateX(0px)',\n      }}\n    />\n  )\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/server-after-nonblocking.md",
    "content": "---\ntitle: Use after() for Non-Blocking Operations\nimpact: MEDIUM\nimpactDescription: faster response times\ntags: server, async, logging, analytics, side-effects\n---\n\n## Use after() for Non-Blocking Operations\n\nUse Next.js's `after()` to schedule work that should execute after a response is sent. This prevents logging, analytics, and other side effects from blocking the response.\n\n**Incorrect (blocks response):**\n\n```tsx\nimport { logUserAction } from '@/app/utils'\n\nexport async function POST(request: Request) {\n  // Perform mutation\n  await updateDatabase(request)\n  \n  // Logging blocks the response\n  const userAgent = request.headers.get('user-agent') || 'unknown'\n  await logUserAction({ userAgent })\n  \n  return new Response(JSON.stringify({ status: 'success' }), {\n    status: 200,\n    headers: { 'Content-Type': 'application/json' }\n  })\n}\n```\n\n**Correct (non-blocking):**\n\n```tsx\nimport { after } from 'next/server'\nimport { headers, cookies } from 'next/headers'\nimport { logUserAction } from '@/app/utils'\n\nexport async function POST(request: Request) {\n  // Perform mutation\n  await updateDatabase(request)\n  \n  // Log after response is sent\n  after(async () => {\n    const userAgent = (await headers()).get('user-agent') || 'unknown'\n    const sessionCookie = (await cookies()).get('session-id')?.value || 'anonymous'\n    \n    logUserAction({ sessionCookie, userAgent })\n  })\n  \n  return new Response(JSON.stringify({ status: 'success' }), {\n    status: 200,\n    headers: { 'Content-Type': 'application/json' }\n  })\n}\n```\n\nThe response is sent immediately while logging happens in the background.\n\n**Common use cases:**\n\n- Analytics tracking\n- Audit logging\n- Sending notifications\n- Cache invalidation\n- Cleanup tasks\n\n**Important notes:**\n\n- `after()` runs even if the response fails or redirects\n- Works in Server Actions, Route Handlers, and Server Components\n\nReference: [https://nextjs.org/docs/app/api-reference/functions/after](https://nextjs.org/docs/app/api-reference/functions/after)\n"
  },
  {
    "path": "skills/react-best-practices/rules/server-auth-actions.md",
    "content": "---\ntitle: Authenticate Server Actions Like API Routes\nimpact: CRITICAL\nimpactDescription: prevents unauthorized access to server mutations\ntags: server, server-actions, authentication, security, authorization\n---\n\n## Authenticate Server Actions Like API Routes\n\n**Impact: CRITICAL (prevents unauthorized access to server mutations)**\n\nServer Actions (functions with `\"use server\"`) are exposed as public endpoints, just like API routes. Always verify authentication and authorization **inside** each Server Action—do not rely solely on middleware, layout guards, or page-level checks, as Server Actions can be invoked directly.\n\nNext.js documentation explicitly states: \"Treat Server Actions with the same security considerations as public-facing API endpoints, and verify if the user is allowed to perform a mutation.\"\n\n**Incorrect (no authentication check):**\n\n```typescript\n'use server'\n\nexport async function deleteUser(userId: string) {\n  // Anyone can call this! No auth check\n  await db.user.delete({ where: { id: userId } })\n  return { success: true }\n}\n```\n\n**Correct (authentication inside the action):**\n\n```typescript\n'use server'\n\nimport { verifySession } from '@/lib/auth'\nimport { unauthorized } from '@/lib/errors'\n\nexport async function deleteUser(userId: string) {\n  // Always check auth inside the action\n  const session = await verifySession()\n  \n  if (!session) {\n    throw unauthorized('Must be logged in')\n  }\n  \n  // Check authorization too\n  if (session.user.role !== 'admin' && session.user.id !== userId) {\n    throw unauthorized('Cannot delete other users')\n  }\n  \n  await db.user.delete({ where: { id: userId } })\n  return { success: true }\n}\n```\n\n**With input validation:**\n\n```typescript\n'use server'\n\nimport { verifySession } from '@/lib/auth'\nimport { z } from 'zod'\n\nconst updateProfileSchema = z.object({\n  userId: z.string().uuid(),\n  name: z.string().min(1).max(100),\n  email: z.string().email()\n})\n\nexport async function updateProfile(data: unknown) {\n  // Validate input first\n  const validated = updateProfileSchema.parse(data)\n  \n  // Then authenticate\n  const session = await verifySession()\n  if (!session) {\n    throw new Error('Unauthorized')\n  }\n  \n  // Then authorize\n  if (session.user.id !== validated.userId) {\n    throw new Error('Can only update own profile')\n  }\n  \n  // Finally perform the mutation\n  await db.user.update({\n    where: { id: validated.userId },\n    data: {\n      name: validated.name,\n      email: validated.email\n    }\n  })\n  \n  return { success: true }\n}\n```\n\nReference: [https://nextjs.org/docs/app/guides/authentication](https://nextjs.org/docs/app/guides/authentication)\n"
  },
  {
    "path": "skills/react-best-practices/rules/server-cache-lru.md",
    "content": "---\ntitle: Cross-Request LRU Caching\nimpact: HIGH\nimpactDescription: caches across requests\ntags: server, cache, lru, cross-request\n---\n\n## Cross-Request LRU Caching\n\n`React.cache()` only works within one request. For data shared across sequential requests (user clicks button A then button B), use an LRU cache.\n\n**Implementation:**\n\n```typescript\nimport { LRUCache } from 'lru-cache'\n\nconst cache = new LRUCache<string, any>({\n  max: 1000,\n  ttl: 5 * 60 * 1000  // 5 minutes\n})\n\nexport async function getUser(id: string) {\n  const cached = cache.get(id)\n  if (cached) return cached\n\n  const user = await db.user.findUnique({ where: { id } })\n  cache.set(id, user)\n  return user\n}\n\n// Request 1: DB query, result cached\n// Request 2: cache hit, no DB query\n```\n\nUse when sequential user actions hit multiple endpoints needing the same data within seconds.\n\n**With Vercel's [Fluid Compute](https://vercel.com/docs/fluid-compute):** LRU caching is especially effective because multiple concurrent requests can share the same function instance and cache. This means the cache persists across requests without needing external storage like Redis.\n\n**In traditional serverless:** Each invocation runs in isolation, so consider Redis for cross-process caching.\n\nReference: [https://github.com/isaacs/node-lru-cache](https://github.com/isaacs/node-lru-cache)\n"
  },
  {
    "path": "skills/react-best-practices/rules/server-cache-react.md",
    "content": "---\ntitle: Per-Request Deduplication with React.cache()\nimpact: MEDIUM\nimpactDescription: deduplicates within request\ntags: server, cache, react-cache, deduplication\n---\n\n## Per-Request Deduplication with React.cache()\n\nUse `React.cache()` for server-side request deduplication. Authentication and database queries benefit most.\n\n**Usage:**\n\n```typescript\nimport { cache } from 'react'\n\nexport const getCurrentUser = cache(async () => {\n  const session = await auth()\n  if (!session?.user?.id) return null\n  return await db.user.findUnique({\n    where: { id: session.user.id }\n  })\n})\n```\n\nWithin a single request, multiple calls to `getCurrentUser()` execute the query only once.\n\n**Avoid inline objects as arguments:**\n\n`React.cache()` uses shallow equality (`Object.is`) to determine cache hits. Inline objects create new references each call, preventing cache hits.\n\n**Incorrect (always cache miss):**\n\n```typescript\nconst getUser = cache(async (params: { uid: number }) => {\n  return await db.user.findUnique({ where: { id: params.uid } })\n})\n\n// Each call creates new object, never hits cache\ngetUser({ uid: 1 })\ngetUser({ uid: 1 })  // Cache miss, runs query again\n```\n\n**Correct (cache hit):**\n\n```typescript\nconst getUser = cache(async (uid: number) => {\n  return await db.user.findUnique({ where: { id: uid } })\n})\n\n// Primitive args use value equality\ngetUser(1)\ngetUser(1)  // Cache hit, returns cached result\n```\n\nIf you must pass objects, pass the same reference:\n\n```typescript\nconst params = { uid: 1 }\ngetUser(params)  // Query runs\ngetUser(params)  // Cache hit (same reference)\n```\n\n**Next.js-Specific Note:**\n\nIn Next.js, the `fetch` API is automatically extended with request memoization. Requests with the same URL and options are automatically deduplicated within a single request, so you don't need `React.cache()` for `fetch` calls. However, `React.cache()` is still essential for other async tasks:\n\n- Database queries (Prisma, Drizzle, etc.)\n- Heavy computations\n- Authentication checks\n- File system operations\n- Any non-fetch async work\n\nUse `React.cache()` to deduplicate these operations across your component tree.\n\nReference: [React.cache documentation](https://react.dev/reference/react/cache)\n"
  },
  {
    "path": "skills/react-best-practices/rules/server-dedup-props.md",
    "content": "---\ntitle: Avoid Duplicate Serialization in RSC Props\nimpact: LOW\nimpactDescription: reduces network payload by avoiding duplicate serialization\ntags: server, rsc, serialization, props, client-components\n---\n\n## Avoid Duplicate Serialization in RSC Props\n\n**Impact: LOW (reduces network payload by avoiding duplicate serialization)**\n\nRSC→client serialization deduplicates by object reference, not value. Same reference = serialized once; new reference = serialized again. Do transformations (`.toSorted()`, `.filter()`, `.map()`) in client, not server.\n\n**Incorrect (duplicates array):**\n\n```tsx\n// RSC: sends 6 strings (2 arrays × 3 items)\n<ClientList usernames={usernames} usernamesOrdered={usernames.toSorted()} />\n```\n\n**Correct (sends 3 strings):**\n\n```tsx\n// RSC: send once\n<ClientList usernames={usernames} />\n\n// Client: transform there\n'use client'\nconst sorted = useMemo(() => [...usernames].sort(), [usernames])\n```\n\n**Nested deduplication behavior:**\n\nDeduplication works recursively. Impact varies by data type:\n\n- `string[]`, `number[]`, `boolean[]`: **HIGH impact** - array + all primitives fully duplicated\n- `object[]`: **LOW impact** - array duplicated, but nested objects deduplicated by reference\n\n```tsx\n// string[] - duplicates everything\nusernames={['a','b']} sorted={usernames.toSorted()} // sends 4 strings\n\n// object[] - duplicates array structure only\nusers={[{id:1},{id:2}]} sorted={users.toSorted()} // sends 2 arrays + 2 unique objects (not 4)\n```\n\n**Operations breaking deduplication (create new references):**\n\n- Arrays: `.toSorted()`, `.filter()`, `.map()`, `.slice()`, `[...arr]`\n- Objects: `{...obj}`, `Object.assign()`, `structuredClone()`, `JSON.parse(JSON.stringify())`\n\n**More examples:**\n\n```tsx\n// ❌ Bad\n<C users={users} active={users.filter(u => u.active)} />\n<C product={product} productName={product.name} />\n\n// ✅ Good\n<C users={users} />\n<C product={product} />\n// Do filtering/destructuring in client\n```\n\n**Exception:** Pass derived data when transformation is expensive or client doesn't need original.\n"
  },
  {
    "path": "skills/react-best-practices/rules/server-hoist-static-io.md",
    "content": "---\ntitle: Hoist Static I/O to Module Level\nimpact: HIGH\nimpactDescription: avoids repeated file/network I/O per request\ntags: server, io, performance, next.js, route-handlers, og-image\n---\n\n## Hoist Static I/O to Module Level\n\n**Impact: HIGH (avoids repeated file/network I/O per request)**\n\nWhen loading static assets (fonts, logos, images, config files) in route handlers or server functions, hoist the I/O operation to module level. Module-level code runs once when the module is first imported, not on every request. This eliminates redundant file system reads or network fetches that would otherwise run on every invocation.\n\n**Incorrect: reads font file on every request**\n\n```typescript\n// app/api/og/route.tsx\nimport { ImageResponse } from 'next/og'\n\nexport async function GET(request: Request) {\n  // Runs on EVERY request - expensive!\n  const fontData = await fetch(\n    new URL('./fonts/Inter.ttf', import.meta.url)\n  ).then(res => res.arrayBuffer())\n  \n  const logoData = await fetch(\n    new URL('./images/logo.png', import.meta.url)\n  ).then(res => res.arrayBuffer())\n\n  return new ImageResponse(\n    <div style={{ fontFamily: 'Inter' }}>\n      <img src={logoData} />\n      Hello World\n    </div>,\n    { fonts: [{ name: 'Inter', data: fontData }] }\n  )\n}\n```\n\n**Correct: loads once at module initialization**\n\n```typescript\n// app/api/og/route.tsx\nimport { ImageResponse } from 'next/og'\n\n// Module-level: runs ONCE when module is first imported\nconst fontData = fetch(\n  new URL('./fonts/Inter.ttf', import.meta.url)\n).then(res => res.arrayBuffer())\n\nconst logoData = fetch(\n  new URL('./images/logo.png', import.meta.url)\n).then(res => res.arrayBuffer())\n\nexport async function GET(request: Request) {\n  // Await the already-started promises\n  const [font, logo] = await Promise.all([fontData, logoData])\n\n  return new ImageResponse(\n    <div style={{ fontFamily: 'Inter' }}>\n      <img src={logo} />\n      Hello World\n    </div>,\n    { fonts: [{ name: 'Inter', data: font }] }\n  )\n}\n```\n\n**Alternative: synchronous file reads with Node.js fs**\n\n```typescript\n// app/api/og/route.tsx\nimport { ImageResponse } from 'next/og'\nimport { readFileSync } from 'fs'\nimport { join } from 'path'\n\n// Synchronous read at module level - blocks only during module init\nconst fontData = readFileSync(\n  join(process.cwd(), 'public/fonts/Inter.ttf')\n)\n\nconst logoData = readFileSync(\n  join(process.cwd(), 'public/images/logo.png')\n)\n\nexport async function GET(request: Request) {\n  return new ImageResponse(\n    <div style={{ fontFamily: 'Inter' }}>\n      <img src={logoData} />\n      Hello World\n    </div>,\n    { fonts: [{ name: 'Inter', data: fontData }] }\n  )\n}\n```\n\n**General Node.js example: loading config or templates**\n\n```typescript\n// Incorrect: reads config on every call\nexport async function processRequest(data: Data) {\n  const config = JSON.parse(\n    await fs.readFile('./config.json', 'utf-8')\n  )\n  const template = await fs.readFile('./template.html', 'utf-8')\n  \n  return render(template, data, config)\n}\n\n// Correct: loads once at module level\nconst configPromise = fs.readFile('./config.json', 'utf-8')\n  .then(JSON.parse)\nconst templatePromise = fs.readFile('./template.html', 'utf-8')\n\nexport async function processRequest(data: Data) {\n  const [config, template] = await Promise.all([\n    configPromise,\n    templatePromise\n  ])\n  \n  return render(template, data, config)\n}\n```\n\n**When to use this pattern:**\n\n- Loading fonts for OG image generation\n- Loading static logos, icons, or watermarks\n- Reading configuration files that don't change at runtime\n- Loading email templates or other static templates\n- Any static asset that's the same across all requests\n\n**When NOT to use this pattern:**\n\n- Assets that vary per request or user\n- Files that may change during runtime (use caching with TTL instead)\n- Large files that would consume too much memory if kept loaded\n- Sensitive data that shouldn't persist in memory\n\n**With Vercel's [Fluid Compute](https://vercel.com/docs/fluid-compute):** Module-level caching is especially effective because multiple concurrent requests share the same function instance. The static assets stay loaded in memory across requests without cold start penalties.\n\n**In traditional serverless:** Each cold start re-executes module-level code, but subsequent warm invocations reuse the loaded assets until the instance is recycled.\n"
  },
  {
    "path": "skills/react-best-practices/rules/server-parallel-fetching.md",
    "content": "---\ntitle: Parallel Data Fetching with Component Composition\nimpact: CRITICAL\nimpactDescription: eliminates server-side waterfalls\ntags: server, rsc, parallel-fetching, composition\n---\n\n## Parallel Data Fetching with Component Composition\n\nReact Server Components execute sequentially within a tree. Restructure with composition to parallelize data fetching.\n\n**Incorrect (Sidebar waits for Page's fetch to complete):**\n\n```tsx\nexport default async function Page() {\n  const header = await fetchHeader()\n  return (\n    <div>\n      <div>{header}</div>\n      <Sidebar />\n    </div>\n  )\n}\n\nasync function Sidebar() {\n  const items = await fetchSidebarItems()\n  return <nav>{items.map(renderItem)}</nav>\n}\n```\n\n**Correct (both fetch simultaneously):**\n\n```tsx\nasync function Header() {\n  const data = await fetchHeader()\n  return <div>{data}</div>\n}\n\nasync function Sidebar() {\n  const items = await fetchSidebarItems()\n  return <nav>{items.map(renderItem)}</nav>\n}\n\nexport default function Page() {\n  return (\n    <div>\n      <Header />\n      <Sidebar />\n    </div>\n  )\n}\n```\n\n**Alternative with children prop:**\n\n```tsx\nasync function Header() {\n  const data = await fetchHeader()\n  return <div>{data}</div>\n}\n\nasync function Sidebar() {\n  const items = await fetchSidebarItems()\n  return <nav>{items.map(renderItem)}</nav>\n}\n\nfunction Layout({ children }: { children: ReactNode }) {\n  return (\n    <div>\n      <Header />\n      {children}\n    </div>\n  )\n}\n\nexport default function Page() {\n  return (\n    <Layout>\n      <Sidebar />\n    </Layout>\n  )\n}\n```\n"
  },
  {
    "path": "skills/react-best-practices/rules/server-serialization.md",
    "content": "---\ntitle: Minimize Serialization at RSC Boundaries\nimpact: HIGH\nimpactDescription: reduces data transfer size\ntags: server, rsc, serialization, props\n---\n\n## Minimize Serialization at RSC Boundaries\n\nThe React Server/Client boundary serializes all object properties into strings and embeds them in the HTML response and subsequent RSC requests. This serialized data directly impacts page weight and load time, so **size matters a lot**. Only pass fields that the client actually uses.\n\n**Incorrect (serializes all 50 fields):**\n\n```tsx\nasync function Page() {\n  const user = await fetchUser()  // 50 fields\n  return <Profile user={user} />\n}\n\n'use client'\nfunction Profile({ user }: { user: User }) {\n  return <div>{user.name}</div>  // uses 1 field\n}\n```\n\n**Correct (serializes only 1 field):**\n\n```tsx\nasync function Page() {\n  const user = await fetchUser()\n  return <Profile name={user.name} />\n}\n\n'use client'\nfunction Profile({ name }: { name: string }) {\n  return <div>{name}</div>\n}\n```\n"
  },
  {
    "path": "skills/react-native-skills/AGENTS.md",
    "content": "# React Native Skills\n\n**Version 1.0.0**  \nEngineering  \nJanuary 2026\n\n> **Note:**  \n> This document is mainly for agents and LLMs to follow when maintaining,  \n> generating, or refactoring React Native codebases. Humans  \n> may also find it useful, but guidance here is optimized for automation  \n> and consistency by AI-assisted workflows.\n\n---\n\n## Abstract\n\nComprehensive performance optimization guide for React Native applications, designed for AI agents and LLMs. Contains 35+ rules across 13 categories, prioritized by impact from critical (core rendering, list performance) to incremental (fonts, imports). Each rule includes detailed explanations, real-world examples comparing incorrect vs. correct implementations, and specific impact metrics to guide automated refactoring and code generation.\n\n---\n\n## Table of Contents\n\n1. [Core Rendering](#1-core-rendering) — **CRITICAL**\n   - 1.1 [Never Use && with Potentially Falsy Values](#11-never-use--with-potentially-falsy-values)\n   - 1.2 [Wrap Strings in Text Components](#12-wrap-strings-in-text-components)\n2. [List Performance](#2-list-performance) — **HIGH**\n   - 2.1 [Avoid Inline Objects in renderItem](#21-avoid-inline-objects-in-renderitem)\n   - 2.2 [Hoist callbacks to the root of lists](#22-hoist-callbacks-to-the-root-of-lists)\n   - 2.3 [Keep List Items Lightweight](#23-keep-list-items-lightweight)\n   - 2.4 [Optimize List Performance with Stable Object References](#24-optimize-list-performance-with-stable-object-references)\n   - 2.5 [Pass Primitives to List Items for Memoization](#25-pass-primitives-to-list-items-for-memoization)\n   - 2.6 [Use a List Virtualizer for Any List](#26-use-a-list-virtualizer-for-any-list)\n   - 2.7 [Use Compressed Images in Lists](#27-use-compressed-images-in-lists)\n   - 2.8 [Use Item Types for Heterogeneous Lists](#28-use-item-types-for-heterogeneous-lists)\n3. [Animation](#3-animation) — **HIGH**\n   - 3.1 [Animate Transform and Opacity Instead of Layout Properties](#31-animate-transform-and-opacity-instead-of-layout-properties)\n   - 3.2 [Prefer useDerivedValue Over useAnimatedReaction](#32-prefer-usederivedvalue-over-useanimatedreaction)\n   - 3.3 [Use GestureDetector for Animated Press States](#33-use-gesturedetector-for-animated-press-states)\n4. [Scroll Performance](#4-scroll-performance) — **HIGH**\n   - 4.1 [Never Track Scroll Position in useState](#41-never-track-scroll-position-in-usestate)\n5. [Navigation](#5-navigation) — **HIGH**\n   - 5.1 [Use Native Navigators for Navigation](#51-use-native-navigators-for-navigation)\n6. [React State](#6-react-state) — **MEDIUM**\n   - 6.1 [Minimize State Variables and Derive Values](#61-minimize-state-variables-and-derive-values)\n   - 6.2 [Use fallback state instead of initialState](#62-use-fallback-state-instead-of-initialstate)\n   - 6.3 [useState Dispatch updaters for State That Depends on Current Value](#63-usestate-dispatch-updaters-for-state-that-depends-on-current-value)\n7. [State Architecture](#7-state-architecture) — **MEDIUM**\n   - 7.1 [State Must Represent Ground Truth](#71-state-must-represent-ground-truth)\n8. [React Compiler](#8-react-compiler) — **MEDIUM**\n   - 8.1 [Destructure Functions Early in Render (React Compiler)](#81-destructure-functions-early-in-render-react-compiler)\n   - 8.2 [Use .get() and .set() for Reanimated Shared Values (not .value)](#82-use-get-and-set-for-reanimated-shared-values-not-value)\n9. [User Interface](#9-user-interface) — **MEDIUM**\n   - 9.1 [Measuring View Dimensions](#91-measuring-view-dimensions)\n   - 9.2 [Modern React Native Styling Patterns](#92-modern-react-native-styling-patterns)\n   - 9.3 [Use contentInset for Dynamic ScrollView Spacing](#93-use-contentinset-for-dynamic-scrollview-spacing)\n   - 9.4 [Use contentInsetAdjustmentBehavior for Safe Areas](#94-use-contentinsetadjustmentbehavior-for-safe-areas)\n   - 9.5 [Use expo-image for Optimized Images](#95-use-expo-image-for-optimized-images)\n   - 9.6 [Use Galeria for Image Galleries and Lightbox](#96-use-galeria-for-image-galleries-and-lightbox)\n   - 9.7 [Use Native Menus for Dropdowns and Context Menus](#97-use-native-menus-for-dropdowns-and-context-menus)\n   - 9.8 [Use Native Modals Over JS-Based Bottom Sheets](#98-use-native-modals-over-js-based-bottom-sheets)\n   - 9.9 [Use Pressable Instead of Touchable Components](#99-use-pressable-instead-of-touchable-components)\n10. [Design System](#10-design-system) — **MEDIUM**\n   - 10.1 [Use Compound Components Over Polymorphic Children](#101-use-compound-components-over-polymorphic-children)\n11. [Monorepo](#11-monorepo) — **LOW**\n   - 11.1 [Install Native Dependencies in App Directory](#111-install-native-dependencies-in-app-directory)\n   - 11.2 [Use Single Dependency Versions Across Monorepo](#112-use-single-dependency-versions-across-monorepo)\n12. [Third-Party Dependencies](#12-third-party-dependencies) — **LOW**\n   - 12.1 [Import from Design System Folder](#121-import-from-design-system-folder)\n13. [JavaScript](#13-javascript) — **LOW**\n   - 13.1 [Hoist Intl Formatter Creation](#131-hoist-intl-formatter-creation)\n14. [Fonts](#14-fonts) — **LOW**\n   - 14.1 [Load fonts natively at build time](#141-load-fonts-natively-at-build-time)\n\n---\n\n## 1. Core Rendering\n\n**Impact: CRITICAL**\n\nFundamental React Native rendering rules. Violations cause\nruntime crashes or broken UI.\n\n### 1.1 Never Use && with Potentially Falsy Values\n\n**Impact: CRITICAL (prevents production crash)**\n\nNever use `{value && <Component />}` when `value` could be an empty string or\n\n`0`. These are falsy but JSX-renderable—React Native will try to render them as\n\ntext outside a `<Text>` component, causing a hard crash in production.\n\n**Incorrect: crashes if count is 0 or name is \"\"**\n\n```tsx\nfunction Profile({ name, count }: { name: string; count: number }) {\n  return (\n    <View>\n      {name && <Text>{name}</Text>}\n      {count && <Text>{count} items</Text>}\n    </View>\n  )\n}\n// If name=\"\" or count=0, renders the falsy value → crash\n```\n\n**Correct: ternary with null**\n\n```tsx\nfunction Profile({ name, count }: { name: string; count: number }) {\n  return (\n    <View>\n      {name ? <Text>{name}</Text> : null}\n      {count ? <Text>{count} items</Text> : null}\n    </View>\n  )\n}\n```\n\n**Correct: explicit boolean coercion**\n\n```tsx\nfunction Profile({ name, count }: { name: string; count: number }) {\n  return (\n    <View>\n      {!!name && <Text>{name}</Text>}\n      {!!count && <Text>{count} items</Text>}\n    </View>\n  )\n}\n```\n\n**Best: early return**\n\n```tsx\nfunction Profile({ name, count }: { name: string; count: number }) {\n  if (!name) return null\n\n  return (\n    <View>\n      <Text>{name}</Text>\n      {count > 0 ? <Text>{count} items</Text> : null}\n    </View>\n  )\n}\n```\n\nEarly returns are clearest. When using conditionals inline, prefer ternary or\n\nexplicit boolean checks.\n\n**Lint rule:** Enable `react/jsx-no-leaked-render` from\n\n[eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-leaked-render.md)\n\nto catch this automatically.\n\n### 1.2 Wrap Strings in Text Components\n\n**Impact: CRITICAL (prevents runtime crash)**\n\nStrings must be rendered inside `<Text>`. React Native crashes if a string is a\n\ndirect child of `<View>`.\n\n**Incorrect: crashes**\n\n```tsx\nimport { View } from 'react-native'\n\nfunction Greeting({ name }: { name: string }) {\n  return <View>Hello, {name}!</View>\n}\n// Error: Text strings must be rendered within a <Text> component.\n```\n\n**Correct:**\n\n```tsx\nimport { View, Text } from 'react-native'\n\nfunction Greeting({ name }: { name: string }) {\n  return (\n    <View>\n      <Text>Hello, {name}!</Text>\n    </View>\n  )\n}\n```\n\n---\n\n## 2. List Performance\n\n**Impact: HIGH**\n\nOptimizing virtualized lists (FlatList, LegendList, FlashList)\nfor smooth scrolling and fast updates.\n\n### 2.1 Avoid Inline Objects in renderItem\n\n**Impact: HIGH (prevents unnecessary re-renders of memoized list items)**\n\nDon't create new objects inside `renderItem` to pass as props. Inline objects\n\ncreate new references on every render, breaking memoization. Pass primitive\n\nvalues directly from `item` instead.\n\n**Incorrect: inline object breaks memoization**\n\n```tsx\nfunction UserList({ users }: { users: User[] }) {\n  return (\n    <LegendList\n      data={users}\n      renderItem={({ item }) => (\n        <UserRow\n          // Bad: new object on every render\n          user={{ id: item.id, name: item.name, avatar: item.avatar }}\n        />\n      )}\n    />\n  )\n}\n```\n\n**Incorrect: inline style object**\n\n```tsx\nrenderItem={({ item }) => (\n  <UserRow\n    name={item.name}\n    // Bad: new style object on every render\n    style={{ backgroundColor: item.isActive ? 'green' : 'gray' }}\n  />\n)}\n```\n\n**Correct: pass item directly or primitives**\n\n```tsx\nfunction UserList({ users }: { users: User[] }) {\n  return (\n    <LegendList\n      data={users}\n      renderItem={({ item }) => (\n        // Good: pass the item directly\n        <UserRow user={item} />\n      )}\n    />\n  )\n}\n```\n\n**Correct: pass primitives, derive inside child**\n\n```tsx\nrenderItem={({ item }) => (\n  <UserRow\n    id={item.id}\n    name={item.name}\n    isActive={item.isActive}\n  />\n)}\n\nconst UserRow = memo(function UserRow({ id, name, isActive }: Props) {\n  // Good: derive style inside memoized component\n  const backgroundColor = isActive ? 'green' : 'gray'\n  return <View style={[styles.row, { backgroundColor }]}>{/* ... */}</View>\n})\n```\n\n**Correct: hoist static styles in module scope**\n\n```tsx\nconst activeStyle = { backgroundColor: 'green' }\nconst inactiveStyle = { backgroundColor: 'gray' }\n\nrenderItem={({ item }) => (\n  <UserRow\n    name={item.name}\n    // Good: stable references\n    style={item.isActive ? activeStyle : inactiveStyle}\n  />\n)}\n```\n\nPassing primitives or stable references allows `memo()` to skip re-renders when\n\nthe actual values haven't changed.\n\n**Note:** If you have the React Compiler enabled, it handles memoization\n\nautomatically and these manual optimizations become less critical.\n\n### 2.2 Hoist callbacks to the root of lists\n\n**Impact: MEDIUM (Fewer re-renders and faster lists)**\n\nWhen passing callback functions to list items, create a single instance of the\n\ncallback at the root of the list. Items should then call it with a unique\n\nidentifier.\n\n**Incorrect: creates a new callback on each render**\n\n```typescript\nreturn (\n  <LegendList\n    renderItem={({ item }) => {\n      // bad: creates a new callback on each render\n      const onPress = () => handlePress(item.id)\n      return <Item key={item.id} item={item} onPress={onPress} />\n    }}\n  />\n)\n```\n\n**Correct: a single function instance passed to each item**\n\n```typescript\nconst onPress = useCallback(() => handlePress(item.id), [handlePress, item.id])\n\nreturn (\n  <LegendList\n    renderItem={({ item }) => (\n      <Item key={item.id} item={item} onPress={onPress} />\n    )}\n  />\n)\n```\n\nReference: [https://example.com](https://example.com)\n\n### 2.3 Keep List Items Lightweight\n\n**Impact: HIGH (reduces render time for visible items during scroll)**\n\nList items should be as inexpensive as possible to render. Minimize hooks, avoid\n\nqueries, and limit React Context access. Virtualized lists render many items\n\nduring scroll—expensive items cause jank.\n\n**Incorrect: heavy list item**\n\n```tsx\nfunction ProductRow({ id }: { id: string }) {\n  // Bad: query inside list item\n  const { data: product } = useQuery(['product', id], () => fetchProduct(id))\n  // Bad: multiple context accesses\n  const theme = useContext(ThemeContext)\n  const user = useContext(UserContext)\n  const cart = useContext(CartContext)\n  // Bad: expensive computation\n  const recommendations = useMemo(\n    () => computeRecommendations(product),\n    [product]\n  )\n\n  return <View>{/* ... */}</View>\n}\n```\n\n**Correct: lightweight list item**\n\n```tsx\nfunction ProductRow({ name, price, imageUrl }: Props) {\n  // Good: receives only primitives, minimal hooks\n  return (\n    <View>\n      <Image source={{ uri: imageUrl }} />\n      <Text>{name}</Text>\n      <Text>{price}</Text>\n    </View>\n  )\n}\n```\n\n**Move data fetching to parent:**\n\n```tsx\n// Parent fetches all data once\nfunction ProductList() {\n  const { data: products } = useQuery(['products'], fetchProducts)\n\n  return (\n    <LegendList\n      data={products}\n      renderItem={({ item }) => (\n        <ProductRow name={item.name} price={item.price} imageUrl={item.image} />\n      )}\n    />\n  )\n}\n```\n\n**For shared values, use Zustand selectors instead of Context:**\n\n```tsx\n// Incorrect: Context causes re-render when any cart value changes\nfunction ProductRow({ id, name }: Props) {\n  const { items } = useContext(CartContext)\n  const inCart = items.includes(id)\n  // ...\n}\n\n// Correct: Zustand selector only re-renders when this specific value changes\nfunction ProductRow({ id, name }: Props) {\n  // use Set.has (created once at the root) instead of Array.includes()\n  const inCart = useCartStore((s) => s.items.has(id))\n  // ...\n}\n```\n\n**Guidelines for list items:**\n\n- No queries or data fetching\n\n- No expensive computations (move to parent or memoize at parent level)\n\n- Prefer Zustand selectors over React Context\n\n- Minimize useState/useEffect hooks\n\n- Pass pre-computed values as props\n\nThe goal: list items should be simple rendering functions that take props and\n\nreturn JSX.\n\n### 2.4 Optimize List Performance with Stable Object References\n\n**Impact: CRITICAL (virtualization relies on reference stability)**\n\nDon't map or filter data before passing to virtualized lists. Virtualization\n\nrelies on object reference stability to know what changed—new references cause\n\nfull re-renders of all visible items. Attempt to prevent frequent renders at the\n\nlist-parent level.\n\nWhere needed, use context selectors within list items.\n\n**Incorrect: creates new object references on every keystroke**\n\n```tsx\nfunction DomainSearch() {\n  const { keyword, setKeyword } = useKeywordZustandState()\n  const { data: tlds } = useTlds()\n\n  // Bad: creates new objects on every render, reparenting the entire list on every keystroke\n  const domains = tlds.map((tld) => ({\n    domain: `${keyword}.${tld.name}`,\n    tld: tld.name,\n    price: tld.price,\n  }))\n\n  return (\n    <>\n      <TextInput value={keyword} onChangeText={setKeyword} />\n      <LegendList\n        data={domains}\n        renderItem={({ item }) => <DomainItem item={item} keyword={keyword} />}\n      />\n    </>\n  )\n}\n```\n\n**Correct: stable references, transform inside items**\n\n```tsx\nconst renderItem = ({ item }) => <DomainItem tld={item} />\n\nfunction DomainSearch() {\n  const { data: tlds } = useTlds()\n\n  return (\n    <LegendList\n      // good: as long as the data is stable, LegendList will not re-render the entire list\n      data={tlds}\n      renderItem={renderItem}\n    />\n  )\n}\n\nfunction DomainItem({ tld }: { tld: Tld }) {\n  // good: transform within items, and don't pass the dynamic data as a prop\n  // good: use a selector function from zustand to receive a stable string back\n  const domain = useKeywordZustandState((s) => s.keyword + '.' + tld.name)\n  return <Text>{domain}</Text>\n}\n```\n\n**Updating parent array reference:**\n\n```tsx\n// good: creates a new array instance without mutating the inner objects\n// good: parent array reference is unaffected by typing and updating \"keyword\"\nconst sortedTlds = tlds.toSorted((a, b) => a.name.localeCompare(b.name))\n\nreturn <LegendList data={sortedTlds} renderItem={renderItem} />\n```\n\nCreating a new array instance can be okay, as long as its inner object\n\nreferences are stable. For instance, if you sort a list of objects:\n\nEven though this creates a new array instance `sortedTlds`, the inner object\n\nreferences are stable.\n\n**With zustand for dynamic data: avoids parent re-renders**\n\n```tsx\nfunction DomainItemFavoriteButton({ tld }: { tld: Tld }) {\n  const isFavorited = useFavoritesStore((s) => s.favorites.has(tld.id))\n  return <TldFavoriteButton isFavorited={isFavorited} />\n}\n```\n\nVirtualization can now skip items that haven't changed when typing. Only visible\n\nitems (~20) re-render on keystroke, rather than the parent.\n\n**Deriving state within list items based on parent data (avoids parent\n\nre-renders):**\n\nFor components where the data is conditional based on the parent state, this\n\npattern is even more important. For example, if you are checking if an item is\n\nfavorited, toggling favorites only re-renders one component if the item itself\n\nis in charge of accessing the state rather than the parent:\n\nNote: if you're using the React Compiler, you can read React Context values\n\ndirectly within list items. Although this is slightly slower than using a\n\nZustand selector in most cases, the effect may be negligible.\n\n### 2.5 Pass Primitives to List Items for Memoization\n\n**Impact: HIGH (enables effective memo() comparison)**\n\nWhen possible, pass only primitive values (strings, numbers, booleans) as props\n\nto list item components. Primitives enable shallow comparison in `memo()` to\n\nwork correctly, skipping re-renders when values haven't changed.\n\n**Incorrect: object prop requires deep comparison**\n\n```tsx\ntype User = { id: string; name: string; email: string; avatar: string }\n\nconst UserRow = memo(function UserRow({ user }: { user: User }) {\n  // memo() compares user by reference, not value\n  // If parent creates new user object, this re-renders even if data is same\n  return <Text>{user.name}</Text>\n})\n\nrenderItem={({ item }) => <UserRow user={item} />}\n```\n\nThis can still be optimized, but it is harder to memoize properly.\n\n**Correct: primitive props enable shallow comparison**\n\n```tsx\nconst UserRow = memo(function UserRow({\n  id,\n  name,\n  email,\n}: {\n  id: string\n  name: string\n  email: string\n}) {\n  // memo() compares each primitive directly\n  // Re-renders only if id, name, or email actually changed\n  return <Text>{name}</Text>\n})\n\nrenderItem={({ item }) => (\n  <UserRow id={item.id} name={item.name} email={item.email} />\n)}\n```\n\n**Pass only what you need:**\n\n```tsx\n// Incorrect: passing entire item when you only need name\n<UserRow user={item} />\n\n// Correct: pass only the fields the component uses\n<UserRow name={item.name} avatarUrl={item.avatar} />\n```\n\n**For callbacks, hoist or use item ID:**\n\n```tsx\n// Incorrect: inline function creates new reference\n<UserRow name={item.name} onPress={() => handlePress(item.id)} />\n\n// Correct: pass ID, handle in child\n<UserRow id={item.id} name={item.name} />\n\nconst UserRow = memo(function UserRow({ id, name }: Props) {\n  const handlePress = useCallback(() => {\n    // use id here\n  }, [id])\n  return <Pressable onPress={handlePress}><Text>{name}</Text></Pressable>\n})\n```\n\nPrimitive props make memoization predictable and effective.\n\n**Note:** If you have the React Compiler enabled, you do not need to use\n\n`memo()` or `useCallback()`, but the object references still apply.\n\n### 2.6 Use a List Virtualizer for Any List\n\n**Impact: HIGH (reduced memory, faster mounts)**\n\nUse a list virtualizer like LegendList or FlashList instead of ScrollView with\n\nmapped children—even for short lists. Virtualizers only render visible items,\n\nreducing memory usage and mount time. ScrollView renders all children upfront,\n\nwhich gets expensive quickly.\n\n**Incorrect: ScrollView renders all items at once**\n\n```tsx\nfunction Feed({ items }: { items: Item[] }) {\n  return (\n    <ScrollView>\n      {items.map((item) => (\n        <ItemCard key={item.id} item={item} />\n      ))}\n    </ScrollView>\n  )\n}\n// 50 items = 50 components mounted, even if only 10 visible\n```\n\n**Correct: virtualizer renders only visible items**\n\n```tsx\nimport { LegendList } from '@legendapp/list'\n\nfunction Feed({ items }: { items: Item[] }) {\n  return (\n    <LegendList\n      data={items}\n      // if you aren't using React Compiler, wrap these with useCallback\n      renderItem={({ item }) => <ItemCard item={item} />}\n      keyExtractor={(item) => item.id}\n      estimatedItemSize={80}\n    />\n  )\n}\n// Only ~10-15 visible items mounted at a time\n```\n\n**Alternative: FlashList**\n\n```tsx\nimport { FlashList } from '@shopify/flash-list'\n\nfunction Feed({ items }: { items: Item[] }) {\n  return (\n    <FlashList\n      data={items}\n      // if you aren't using React Compiler, wrap these with useCallback\n      renderItem={({ item }) => <ItemCard item={item} />}\n      keyExtractor={(item) => item.id}\n    />\n  )\n}\n```\n\nBenefits apply to any screen with scrollable content—profiles, settings, feeds,\n\nsearch results. Default to virtualization.\n\n### 2.7 Use Compressed Images in Lists\n\n**Impact: HIGH (faster load times, less memory)**\n\nAlways load compressed, appropriately-sized images in lists. Full-resolution\n\nimages consume excessive memory and cause scroll jank. Request thumbnails from\n\nyour server or use an image CDN with resize parameters.\n\n**Incorrect: full-resolution images**\n\n```tsx\nfunction ProductItem({ product }: { product: Product }) {\n  return (\n    <View>\n      {/* 4000x3000 image loaded for a 100x100 thumbnail */}\n      <Image\n        source={{ uri: product.imageUrl }}\n        style={{ width: 100, height: 100 }}\n      />\n      <Text>{product.name}</Text>\n    </View>\n  )\n}\n```\n\n**Correct: request appropriately-sized image**\n\n```tsx\nfunction ProductItem({ product }: { product: Product }) {\n  // Request a 200x200 image (2x for retina)\n  const thumbnailUrl = `${product.imageUrl}?w=200&h=200&fit=cover`\n\n  return (\n    <View>\n      <Image\n        source={{ uri: thumbnailUrl }}\n        style={{ width: 100, height: 100 }}\n        contentFit='cover'\n      />\n      <Text>{product.name}</Text>\n    </View>\n  )\n}\n```\n\nUse an optimized image component with built-in caching and placeholder support,\n\nsuch as `expo-image` or `SolitoImage` (which uses `expo-image` under the hood).\n\nRequest images at 2x the display size for retina screens.\n\n### 2.8 Use Item Types for Heterogeneous Lists\n\n**Impact: HIGH (efficient recycling, less layout thrashing)**\n\nWhen a list has different item layouts (messages, images, headers, etc.), use a\n\n`type` field on each item and provide `getItemType` to the list. This puts items\n\ninto separate recycling pools so a message component never gets recycled into an\n\nimage component.\n\n[LegendList getItemType](https://legendapp.com/open-source/list/api/props/#getitemtype-v2)\n\n**Incorrect: single component with conditionals**\n\n```tsx\ntype Item = { id: string; text?: string; imageUrl?: string; isHeader?: boolean }\n\nfunction ListItem({ item }: { item: Item }) {\n  if (item.isHeader) {\n    return <HeaderItem title={item.text} />\n  }\n  if (item.imageUrl) {\n    return <ImageItem url={item.imageUrl} />\n  }\n  return <MessageItem text={item.text} />\n}\n\nfunction Feed({ items }: { items: Item[] }) {\n  return (\n    <LegendList\n      data={items}\n      renderItem={({ item }) => <ListItem item={item} />}\n      recycleItems\n    />\n  )\n}\n```\n\n**Correct: typed items with separate components**\n\n```tsx\ntype HeaderItem = { id: string; type: 'header'; title: string }\ntype MessageItem = { id: string; type: 'message'; text: string }\ntype ImageItem = { id: string; type: 'image'; url: string }\ntype FeedItem = HeaderItem | MessageItem | ImageItem\n\nfunction Feed({ items }: { items: FeedItem[] }) {\n  return (\n    <LegendList\n      data={items}\n      keyExtractor={(item) => item.id}\n      getItemType={(item) => item.type}\n      renderItem={({ item }) => {\n        switch (item.type) {\n          case 'header':\n            return <SectionHeader title={item.title} />\n          case 'message':\n            return <MessageRow text={item.text} />\n          case 'image':\n            return <ImageRow url={item.url} />\n        }\n      }}\n      recycleItems\n    />\n  )\n}\n```\n\n**Why this matters:**\n\n```tsx\n<LegendList\n  data={items}\n  keyExtractor={(item) => item.id}\n  getItemType={(item) => item.type}\n  getEstimatedItemSize={(index, item, itemType) => {\n    switch (itemType) {\n      case 'header':\n        return 48\n      case 'message':\n        return 72\n      case 'image':\n        return 300\n      default:\n        return 72\n    }\n  }}\n  renderItem={({ item }) => {\n    /* ... */\n  }}\n  recycleItems\n/>\n```\n\n- **Recycling efficiency**: Items with the same type share a recycling pool\n\n- **No layout thrashing**: A header never recycles into an image cell\n\n- **Type safety**: TypeScript can narrow the item type in each branch\n\n- **Better size estimation**: Use `getEstimatedItemSize` with `itemType` for\n\n  accurate estimates per type\n\n---\n\n## 3. Animation\n\n**Impact: HIGH**\n\nGPU-accelerated animations, Reanimated patterns, and avoiding\nrender thrashing during gestures.\n\n### 3.1 Animate Transform and Opacity Instead of Layout Properties\n\n**Impact: HIGH (GPU-accelerated animations, no layout recalculation)**\n\nAvoid animating `width`, `height`, `top`, `left`, `margin`, or `padding`. These trigger layout recalculation on every frame. Instead, use `transform` (scale, translate) and `opacity` which run on the GPU without triggering layout.\n\n**Incorrect: animates height, triggers layout every frame**\n\n```tsx\nimport Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated'\n\nfunction CollapsiblePanel({ expanded }: { expanded: boolean }) {\n  const animatedStyle = useAnimatedStyle(() => ({\n    height: withTiming(expanded ? 200 : 0), // triggers layout on every frame\n    overflow: 'hidden',\n  }))\n\n  return <Animated.View style={animatedStyle}>{children}</Animated.View>\n}\n```\n\n**Correct: animates scaleY, GPU-accelerated**\n\n```tsx\nimport Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated'\n\nfunction CollapsiblePanel({ expanded }: { expanded: boolean }) {\n  const animatedStyle = useAnimatedStyle(() => ({\n    transform: [\n      { scaleY: withTiming(expanded ? 1 : 0) },\n    ],\n    opacity: withTiming(expanded ? 1 : 0),\n  }))\n\n  return (\n    <Animated.View style={[{ height: 200, transformOrigin: 'top' }, animatedStyle]}>\n      {children}\n    </Animated.View>\n  )\n}\n```\n\n**Correct: animates translateY for slide animations**\n\n```tsx\nimport Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated'\n\nfunction SlideIn({ visible }: { visible: boolean }) {\n  const animatedStyle = useAnimatedStyle(() => ({\n    transform: [\n      { translateY: withTiming(visible ? 0 : 100) },\n    ],\n    opacity: withTiming(visible ? 1 : 0),\n  }))\n\n  return <Animated.View style={animatedStyle}>{children}</Animated.View>\n}\n```\n\nGPU-accelerated properties: `transform` (translate, scale, rotate), `opacity`. Everything else triggers layout.\n\n### 3.2 Prefer useDerivedValue Over useAnimatedReaction\n\n**Impact: MEDIUM (cleaner code, automatic dependency tracking)**\n\nWhen deriving a shared value from another, use `useDerivedValue` instead of\n\n`useAnimatedReaction`. Derived values are declarative, automatically track\n\ndependencies, and return a value you can use directly. Animated reactions are\n\nfor side effects, not derivations.\n\n[Reanimated useDerivedValue](https://docs.swmansion.com/react-native-reanimated/docs/core/useDerivedValue)\n\n**Incorrect: useAnimatedReaction for derivation**\n\n```tsx\nimport { useSharedValue, useAnimatedReaction } from 'react-native-reanimated'\n\nfunction MyComponent() {\n  const progress = useSharedValue(0)\n  const opacity = useSharedValue(1)\n\n  useAnimatedReaction(\n    () => progress.value,\n    (current) => {\n      opacity.value = 1 - current\n    }\n  )\n\n  // ...\n}\n```\n\n**Correct: useDerivedValue**\n\n```tsx\nimport { useSharedValue, useDerivedValue } from 'react-native-reanimated'\n\nfunction MyComponent() {\n  const progress = useSharedValue(0)\n\n  const opacity = useDerivedValue(() => 1 - progress.get())\n\n  // ...\n}\n```\n\nUse `useAnimatedReaction` only for side effects that don't produce a value\n\n(e.g., triggering haptics, logging, calling `runOnJS`).\n\n### 3.3 Use GestureDetector for Animated Press States\n\n**Impact: MEDIUM (UI thread animations, smoother press feedback)**\n\nFor animated press states (scale, opacity on press), use `GestureDetector` with\n\n`Gesture.Tap()` and shared values instead of Pressable's\n\n`onPressIn`/`onPressOut`. Gesture callbacks run on the UI thread as worklets—no\n\nJS thread round-trip for press animations.\n\n[Gesture Handler Tap Gesture](https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/tap-gesture)\n\n**Incorrect: Pressable with JS thread callbacks**\n\n```tsx\nimport { Pressable } from 'react-native'\nimport Animated, {\n  useSharedValue,\n  useAnimatedStyle,\n  withTiming,\n} from 'react-native-reanimated'\n\nfunction AnimatedButton({ onPress }: { onPress: () => void }) {\n  const scale = useSharedValue(1)\n\n  const animatedStyle = useAnimatedStyle(() => ({\n    transform: [{ scale: scale.value }],\n  }))\n\n  return (\n    <Pressable\n      onPress={onPress}\n      onPressIn={() => (scale.value = withTiming(0.95))}\n      onPressOut={() => (scale.value = withTiming(1))}\n    >\n      <Animated.View style={animatedStyle}>\n        <Text>Press me</Text>\n      </Animated.View>\n    </Pressable>\n  )\n}\n```\n\n**Correct: GestureDetector with UI thread worklets**\n\n```tsx\nimport { Gesture, GestureDetector } from 'react-native-gesture-handler'\nimport Animated, {\n  useSharedValue,\n  useAnimatedStyle,\n  withTiming,\n  interpolate,\n  runOnJS,\n} from 'react-native-reanimated'\n\nfunction AnimatedButton({ onPress }: { onPress: () => void }) {\n  // Store the press STATE (0 = not pressed, 1 = pressed)\n  const pressed = useSharedValue(0)\n\n  const tap = Gesture.Tap()\n    .onBegin(() => {\n      pressed.set(withTiming(1))\n    })\n    .onFinalize(() => {\n      pressed.set(withTiming(0))\n    })\n    .onEnd(() => {\n      runOnJS(onPress)()\n    })\n\n  // Derive visual values from the state\n  const animatedStyle = useAnimatedStyle(() => ({\n    transform: [\n      { scale: interpolate(withTiming(pressed.get()), [0, 1], [1, 0.95]) },\n    ],\n  }))\n\n  return (\n    <GestureDetector gesture={tap}>\n      <Animated.View style={animatedStyle}>\n        <Text>Press me</Text>\n      </Animated.View>\n    </GestureDetector>\n  )\n}\n```\n\nStore the press **state** (0 or 1), then derive the scale via `interpolate`.\n\nThis keeps the shared value as ground truth. Use `runOnJS` to call JS functions\n\nfrom worklets. Use `.set()` and `.get()` for React Compiler compatibility.\n\n---\n\n## 4. Scroll Performance\n\n**Impact: HIGH**\n\nTracking scroll position without causing render thrashing.\n\n### 4.1 Never Track Scroll Position in useState\n\n**Impact: HIGH (prevents render thrashing during scroll)**\n\nNever store scroll position in `useState`. Scroll events fire rapidly—state\n\nupdates cause render thrashing and dropped frames. Use a Reanimated shared value\n\nfor animations or a ref for non-reactive tracking.\n\n**Incorrect: useState causes jank**\n\n```tsx\nimport { useState } from 'react'\nimport {\n  ScrollView,\n  NativeSyntheticEvent,\n  NativeScrollEvent,\n} from 'react-native'\n\nfunction Feed() {\n  const [scrollY, setScrollY] = useState(0)\n\n  const onScroll = (e: NativeSyntheticEvent<NativeScrollEvent>) => {\n    setScrollY(e.nativeEvent.contentOffset.y) // re-renders on every frame\n  }\n\n  return <ScrollView onScroll={onScroll} scrollEventThrottle={16} />\n}\n```\n\n**Correct: Reanimated for animations**\n\n```tsx\nimport Animated, {\n  useSharedValue,\n  useAnimatedScrollHandler,\n} from 'react-native-reanimated'\n\nfunction Feed() {\n  const scrollY = useSharedValue(0)\n\n  const onScroll = useAnimatedScrollHandler({\n    onScroll: (e) => {\n      scrollY.value = e.contentOffset.y // runs on UI thread, no re-render\n    },\n  })\n\n  return (\n    <Animated.ScrollView\n      onScroll={onScroll}\n      // higher number has better performance, but it fires less often.\n      // unset this if you need higher precision over performance.\n      scrollEventThrottle={16}\n    />\n  )\n}\n```\n\n**Correct: ref for non-reactive tracking**\n\n```tsx\nimport { useRef } from 'react'\nimport {\n  ScrollView,\n  NativeSyntheticEvent,\n  NativeScrollEvent,\n} from 'react-native'\n\nfunction Feed() {\n  const scrollY = useRef(0)\n\n  const onScroll = (e: NativeSyntheticEvent<NativeScrollEvent>) => {\n    scrollY.current = e.nativeEvent.contentOffset.y // no re-render\n  }\n\n  return <ScrollView onScroll={onScroll} scrollEventThrottle={16} />\n}\n```\n\n---\n\n## 5. Navigation\n\n**Impact: HIGH**\n\nUsing native navigators for stack and tab navigation instead of\nJS-based alternatives.\n\n### 5.1 Use Native Navigators for Navigation\n\n**Impact: HIGH (native performance, platform-appropriate UI)**\n\nAlways use native navigators instead of JS-based ones. Native navigators use\n\nplatform APIs (UINavigationController on iOS, Fragment on Android) for better\n\nperformance and native behavior.\n\n**For stacks:** Use `@react-navigation/native-stack` or expo-router's default\n\nstack (which uses native-stack). Avoid `@react-navigation/stack`.\n\n**For tabs:** Use `react-native-bottom-tabs` (native) or expo-router's native\n\ntabs. Avoid `@react-navigation/bottom-tabs` when native feel matters.\n\n- [React Navigation Native Stack](https://reactnavigation.org/docs/native-stack-navigator)\n\n- [React Native Bottom Tabs with React Navigation](https://oss.callstack.com/react-native-bottom-tabs/docs/guides/usage-with-react-navigation)\n\n- [React Native Bottom Tabs with Expo Router](https://oss.callstack.com/react-native-bottom-tabs/docs/guides/usage-with-expo-router)\n\n- [Expo Router Native Tabs](https://docs.expo.dev/router/advanced/native-tabs)\n\n**Incorrect: JS stack navigator**\n\n```tsx\nimport { createStackNavigator } from '@react-navigation/stack'\n\nconst Stack = createStackNavigator()\n\nfunction App() {\n  return (\n    <Stack.Navigator>\n      <Stack.Screen name='Home' component={HomeScreen} />\n      <Stack.Screen name='Details' component={DetailsScreen} />\n    </Stack.Navigator>\n  )\n}\n```\n\n**Correct: native stack with react-navigation**\n\n```tsx\nimport { createNativeStackNavigator } from '@react-navigation/native-stack'\n\nconst Stack = createNativeStackNavigator()\n\nfunction App() {\n  return (\n    <Stack.Navigator>\n      <Stack.Screen name='Home' component={HomeScreen} />\n      <Stack.Screen name='Details' component={DetailsScreen} />\n    </Stack.Navigator>\n  )\n}\n```\n\n**Correct: expo-router uses native stack by default**\n\n```tsx\n// app/_layout.tsx\nimport { Stack } from 'expo-router'\n\nexport default function Layout() {\n  return <Stack />\n}\n```\n\n**Incorrect: JS bottom tabs**\n\n```tsx\nimport { createBottomTabNavigator } from '@react-navigation/bottom-tabs'\n\nconst Tab = createBottomTabNavigator()\n\nfunction App() {\n  return (\n    <Tab.Navigator>\n      <Tab.Screen name='Home' component={HomeScreen} />\n      <Tab.Screen name='Settings' component={SettingsScreen} />\n    </Tab.Navigator>\n  )\n}\n```\n\n**Correct: native bottom tabs with react-navigation**\n\n```tsx\nimport { createNativeBottomTabNavigator } from '@bottom-tabs/react-navigation'\n\nconst Tab = createNativeBottomTabNavigator()\n\nfunction App() {\n  return (\n    <Tab.Navigator>\n      <Tab.Screen\n        name='Home'\n        component={HomeScreen}\n        options={{\n          tabBarIcon: () => ({ sfSymbol: 'house' }),\n        }}\n      />\n      <Tab.Screen\n        name='Settings'\n        component={SettingsScreen}\n        options={{\n          tabBarIcon: () => ({ sfSymbol: 'gear' }),\n        }}\n      />\n    </Tab.Navigator>\n  )\n}\n```\n\n**Correct: expo-router native tabs**\n\n```tsx\n// app/(tabs)/_layout.tsx\nimport { NativeTabs } from 'expo-router/unstable-native-tabs'\n\nexport default function TabLayout() {\n  return (\n    <NativeTabs>\n      <NativeTabs.Trigger name='index'>\n        <NativeTabs.Trigger.Label>Home</NativeTabs.Trigger.Label>\n        <NativeTabs.Trigger.Icon sf='house.fill' md='home' />\n      </NativeTabs.Trigger>\n      <NativeTabs.Trigger name='settings'>\n        <NativeTabs.Trigger.Label>Settings</NativeTabs.Trigger.Label>\n        <NativeTabs.Trigger.Icon sf='gear' md='settings' />\n      </NativeTabs.Trigger>\n    </NativeTabs>\n  )\n}\n```\n\nOn iOS, native tabs automatically enable `contentInsetAdjustmentBehavior` on the\n\nfirst `ScrollView` at the root of each tab screen, so content scrolls correctly\n\nbehind the translucent tab bar. If you need to disable this, use\n\n`disableAutomaticContentInsets` on the trigger.\n\n**Incorrect: custom header component**\n\n```tsx\n<Stack.Screen\n  name='Profile'\n  component={ProfileScreen}\n  options={{\n    header: () => <CustomHeader title='Profile' />,\n  }}\n/>\n```\n\n**Correct: native header options**\n\n```tsx\n<Stack.Screen\n  name='Profile'\n  component={ProfileScreen}\n  options={{\n    title: 'Profile',\n    headerLargeTitleEnabled: true,\n    headerSearchBarOptions: {\n      placeholder: 'Search',\n    },\n  }}\n/>\n```\n\nNative headers support iOS large titles, search bars, blur effects, and proper\n\nsafe area handling automatically.\n\n- **Performance**: Native transitions and gestures run on the UI thread\n\n- **Platform behavior**: Automatic iOS large titles, Android material design\n\n- **System integration**: Scroll-to-top on tab tap, PiP avoidance, proper safe\n\n  areas\n\n- **Accessibility**: Platform accessibility features work automatically\n\n---\n\n## 6. React State\n\n**Impact: MEDIUM**\n\nPatterns for managing React state to avoid stale closures and\nunnecessary re-renders.\n\n### 6.1 Minimize State Variables and Derive Values\n\n**Impact: MEDIUM (fewer re-renders, less state drift)**\n\nUse the fewest state variables possible. If a value can be computed from existing state or props, derive it during render instead of storing it in state. Redundant state causes unnecessary re-renders and can drift out of sync.\n\n**Incorrect: redundant state**\n\n```tsx\nfunction Cart({ items }: { items: Item[] }) {\n  const [total, setTotal] = useState(0)\n  const [itemCount, setItemCount] = useState(0)\n\n  useEffect(() => {\n    setTotal(items.reduce((sum, item) => sum + item.price, 0))\n    setItemCount(items.length)\n  }, [items])\n\n  return (\n    <View>\n      <Text>{itemCount} items</Text>\n      <Text>Total: ${total}</Text>\n    </View>\n  )\n}\n```\n\n**Correct: derived values**\n\n```tsx\nfunction Cart({ items }: { items: Item[] }) {\n  const total = items.reduce((sum, item) => sum + item.price, 0)\n  const itemCount = items.length\n\n  return (\n    <View>\n      <Text>{itemCount} items</Text>\n      <Text>Total: ${total}</Text>\n    </View>\n  )\n}\n```\n\n**Another example:**\n\n```tsx\n// Incorrect: storing both firstName, lastName, AND fullName\nconst [firstName, setFirstName] = useState('')\nconst [lastName, setLastName] = useState('')\nconst [fullName, setFullName] = useState('')\n\n// Correct: derive fullName\nconst [firstName, setFirstName] = useState('')\nconst [lastName, setLastName] = useState('')\nconst fullName = `${firstName} ${lastName}`\n```\n\nState should be the minimal source of truth. Everything else is derived.\n\nReference: [https://react.dev/learn/choosing-the-state-structure](https://react.dev/learn/choosing-the-state-structure)\n\n### 6.2 Use fallback state instead of initialState\n\n**Impact: MEDIUM (reactive fallbacks without syncing)**\n\nUse `undefined` as initial state and nullish coalescing (`??`) to fall back to\n\nparent or server values. State represents user intent only—`undefined` means\n\n\"user hasn't chosen yet.\" This enables reactive fallbacks that update when the\n\nsource changes, not just on initial render.\n\n**Incorrect: syncs state, loses reactivity**\n\n```tsx\ntype Props = { fallbackEnabled: boolean }\n\nfunction Toggle({ fallbackEnabled }: Props) {\n  const [enabled, setEnabled] = useState(defaultEnabled)\n  // If fallbackEnabled changes, state is stale\n  // State mixes user intent with default value\n\n  return <Switch value={enabled} onValueChange={setEnabled} />\n}\n```\n\n**Correct: state is user intent, reactive fallback**\n\n```tsx\ntype Props = { fallbackEnabled: boolean }\n\nfunction Toggle({ fallbackEnabled }: Props) {\n  const [_enabled, setEnabled] = useState<boolean | undefined>(undefined)\n  const enabled = _enabled ?? defaultEnabled\n  // undefined = user hasn't touched it, falls back to prop\n  // If defaultEnabled changes, component reflects it\n  // Once user interacts, their choice persists\n\n  return <Switch value={enabled} onValueChange={setEnabled} />\n}\n```\n\n**With server data:**\n\n```tsx\nfunction ProfileForm({ data }: { data: User }) {\n  const [_theme, setTheme] = useState<string | undefined>(undefined)\n  const theme = _theme ?? data.theme\n  // Shows server value until user overrides\n  // Server refetch updates the fallback automatically\n\n  return <ThemePicker value={theme} onChange={setTheme} />\n}\n```\n\n### 6.3 useState Dispatch updaters for State That Depends on Current Value\n\n**Impact: MEDIUM (avoids stale closures, prevents unnecessary re-renders)**\n\nWhen the next state depends on the current state, use a dispatch updater\n\n(`setState(prev => ...)`) instead of reading the state variable directly in a\n\ncallback. This avoids stale closures and ensures you're comparing against the\n\nlatest value.\n\n**Incorrect: reads state directly**\n\n```tsx\nconst [size, setSize] = useState<Size | undefined>(undefined)\n\nconst onLayout = (e: LayoutChangeEvent) => {\n  const { width, height } = e.nativeEvent.layout\n  // size may be stale in this closure\n  if (size?.width !== width || size?.height !== height) {\n    setSize({ width, height })\n  }\n}\n```\n\n**Correct: dispatch updater**\n\n```tsx\nconst [size, setSize] = useState<Size | undefined>(undefined)\n\nconst onLayout = (e: LayoutChangeEvent) => {\n  const { width, height } = e.nativeEvent.layout\n  setSize((prev) => {\n    if (prev?.width === width && prev?.height === height) return prev\n    return { width, height }\n  })\n}\n```\n\nReturning the previous value from the updater skips the re-render.\n\nFor primitive states, you don't need to compare values before firing a\n\nre-render.\n\n**Incorrect: unnecessary comparison for primitive state**\n\n```tsx\nconst [size, setSize] = useState<Size | undefined>(undefined)\n\nconst onLayout = (e: LayoutChangeEvent) => {\n  const { width, height } = e.nativeEvent.layout\n  setSize((prev) => (prev === width ? prev : width))\n}\n```\n\n**Correct: sets primitive state directly**\n\n```tsx\nconst [size, setSize] = useState<Size | undefined>(undefined)\n\nconst onLayout = (e: LayoutChangeEvent) => {\n  const { width, height } = e.nativeEvent.layout\n  setSize(width)\n}\n```\n\nHowever, if the next state depends on the current state, you should still use a\n\ndispatch updater.\n\n**Incorrect: reads state directly from the callback**\n\n```tsx\nconst [count, setCount] = useState(0)\n\nconst onTap = () => {\n  setCount(count + 1)\n}\n```\n\n**Correct: dispatch updater**\n\n```tsx\nconst [count, setCount] = useState(0)\n\nconst onTap = () => {\n  setCount((prev) => prev + 1)\n}\n```\n\n---\n\n## 7. State Architecture\n\n**Impact: MEDIUM**\n\nGround truth principles for state variables and derived values.\n\n### 7.1 State Must Represent Ground Truth\n\n**Impact: HIGH (cleaner logic, easier debugging, single source of truth)**\n\nState variables—both React `useState` and Reanimated shared values—should\n\nrepresent the actual state of something (e.g., `pressed`, `progress`, `isOpen`),\n\nnot derived visual values (e.g., `scale`, `opacity`, `translateY`). Derive\n\nvisual values from state using computation or interpolation.\n\n**Incorrect: storing the visual output**\n\n```tsx\nconst scale = useSharedValue(1)\n\nconst tap = Gesture.Tap()\n  .onBegin(() => {\n    scale.set(withTiming(0.95))\n  })\n  .onFinalize(() => {\n    scale.set(withTiming(1))\n  })\n\nconst animatedStyle = useAnimatedStyle(() => ({\n  transform: [{ scale: scale.get() }],\n}))\n```\n\n**Correct: storing the state, deriving the visual**\n\n```tsx\nconst pressed = useSharedValue(0) // 0 = not pressed, 1 = pressed\n\nconst tap = Gesture.Tap()\n  .onBegin(() => {\n    pressed.set(withTiming(1))\n  })\n  .onFinalize(() => {\n    pressed.set(withTiming(0))\n  })\n\nconst animatedStyle = useAnimatedStyle(() => ({\n  transform: [{ scale: interpolate(pressed.get(), [0, 1], [1, 0.95]) }],\n}))\n```\n\n**Why this matters:**\n\nState variables should represent real \"state\", not necessarily a desired end\n\nresult.\n\n1. **Single source of truth** — The state (`pressed`) describes what's\n\n   happening; visuals are derived\n\n2. **Easier to extend** — Adding opacity, rotation, or other effects just\n\n   requires more interpolations from the same state\n\n3. **Debugging** — Inspecting `pressed = 1` is clearer than `scale = 0.95`\n\n4. **Reusable logic** — The same `pressed` value can drive multiple visual\n\n   properties\n\n**Same principle for React state:**\n\n```tsx\n// Incorrect: storing derived values\nconst [isExpanded, setIsExpanded] = useState(false)\nconst [height, setHeight] = useState(0)\n\nuseEffect(() => {\n  setHeight(isExpanded ? 200 : 0)\n}, [isExpanded])\n\n// Correct: derive from state\nconst [isExpanded, setIsExpanded] = useState(false)\nconst height = isExpanded ? 200 : 0\n```\n\nState is the minimal truth. Everything else is derived.\n\n---\n\n## 8. React Compiler\n\n**Impact: MEDIUM**\n\nCompatibility patterns for React Compiler with React Native and\nReanimated.\n\n### 8.1 Destructure Functions Early in Render (React Compiler)\n\n**Impact: HIGH (stable references, fewer re-renders)**\n\nThis rule is only applicable if you are using the React Compiler.\n\nDestructure functions from hooks at the top of render scope. Never dot into\n\nobjects to call functions. Destructured functions are stable references; dotting\n\ncreates new references and breaks memoization.\n\n**Incorrect: dotting into object**\n\n```tsx\nimport { useRouter } from 'expo-router'\n\nfunction SaveButton(props) {\n  const router = useRouter()\n\n  // bad: react-compiler will key the cache on \"props\" and \"router\", which are objects that change each render\n  const handlePress = () => {\n    props.onSave()\n    router.push('/success') // unstable reference\n  }\n\n  return <Button onPress={handlePress}>Save</Button>\n}\n```\n\n**Correct: destructure early**\n\n```tsx\nimport { useRouter } from 'expo-router'\n\nfunction SaveButton({ onSave }) {\n  const { push } = useRouter()\n\n  // good: react-compiler will key on push and onSave\n  const handlePress = () => {\n    onSave()\n    push('/success') // stable reference\n  }\n\n  return <Button onPress={handlePress}>Save</Button>\n}\n```\n\n### 8.2 Use .get() and .set() for Reanimated Shared Values (not .value)\n\n**Impact: LOW (required for React Compiler compatibility)**\n\nWith React Compiler enabled, use `.get()` and `.set()` instead of reading or\n\nwriting `.value` directly on Reanimated shared values. The compiler can't track\n\nproperty access—explicit methods ensure correct behavior.\n\n**Incorrect: breaks with React Compiler**\n\n```tsx\nimport { useSharedValue } from 'react-native-reanimated'\n\nfunction Counter() {\n  const count = useSharedValue(0)\n\n  const increment = () => {\n    count.value = count.value + 1 // opts out of react compiler\n  }\n\n  return <Button onPress={increment} title={`Count: ${count.value}`} />\n}\n```\n\n**Correct: React Compiler compatible**\n\n```tsx\nimport { useSharedValue } from 'react-native-reanimated'\n\nfunction Counter() {\n  const count = useSharedValue(0)\n\n  const increment = () => {\n    count.set(count.get() + 1)\n  }\n\n  return <Button onPress={increment} title={`Count: ${count.get()}`} />\n}\n```\n\nSee the\n\n[Reanimated docs](https://docs.swmansion.com/react-native-reanimated/docs/core/useSharedValue/#react-compiler-support)\n\nfor more.\n\n---\n\n## 9. User Interface\n\n**Impact: MEDIUM**\n\nNative UI patterns for images, menus, modals, styling, and\nplatform-consistent interfaces.\n\n### 9.1 Measuring View Dimensions\n\n**Impact: MEDIUM (synchronous measurement, avoid unnecessary re-renders)**\n\nUse both `useLayoutEffect` (synchronous) and `onLayout` (for updates). The sync\n\nmeasurement gives you the initial size immediately; `onLayout` keeps it current\n\nwhen the view changes. For non-primitive states, use a dispatch updater to\n\ncompare values and avoid unnecessary re-renders.\n\n**Height only:**\n\n```tsx\nimport { useLayoutEffect, useRef, useState } from 'react'\nimport { View, LayoutChangeEvent } from 'react-native'\n\nfunction MeasuredBox({ children }: { children: React.ReactNode }) {\n  const ref = useRef<View>(null)\n  const [height, setHeight] = useState<number | undefined>(undefined)\n\n  useLayoutEffect(() => {\n    // Sync measurement on mount (RN 0.82+)\n    const rect = ref.current?.getBoundingClientRect()\n    if (rect) setHeight(rect.height)\n    // Pre-0.82: ref.current?.measure((x, y, w, h) => setHeight(h))\n  }, [])\n\n  const onLayout = (e: LayoutChangeEvent) => {\n    setHeight(e.nativeEvent.layout.height)\n  }\n\n  return (\n    <View ref={ref} onLayout={onLayout}>\n      {children}\n    </View>\n  )\n}\n```\n\n**Both dimensions:**\n\n```tsx\nimport { useLayoutEffect, useRef, useState } from 'react'\nimport { View, LayoutChangeEvent } from 'react-native'\n\ntype Size = { width: number; height: number }\n\nfunction MeasuredBox({ children }: { children: React.ReactNode }) {\n  const ref = useRef<View>(null)\n  const [size, setSize] = useState<Size | undefined>(undefined)\n\n  useLayoutEffect(() => {\n    const rect = ref.current?.getBoundingClientRect()\n    if (rect) setSize({ width: rect.width, height: rect.height })\n  }, [])\n\n  const onLayout = (e: LayoutChangeEvent) => {\n    const { width, height } = e.nativeEvent.layout\n    setSize((prev) => {\n      // for non-primitive states, compare values before firing a re-render\n      if (prev?.width === width && prev?.height === height) return prev\n      return { width, height }\n    })\n  }\n\n  return (\n    <View ref={ref} onLayout={onLayout}>\n      {children}\n    </View>\n  )\n}\n```\n\nUse functional setState to compare—don't read state directly in the callback.\n\n### 9.2 Modern React Native Styling Patterns\n\n**Impact: MEDIUM (consistent design, smoother borders, cleaner layouts)**\n\nFollow these styling patterns for cleaner, more consistent React Native code.\n\n**Always use `borderCurve: 'continuous'` with `borderRadius`:**\n\n**Use `gap` instead of margin for spacing between elements:**\n\n```tsx\n// Incorrect – margin on children\n<View>\n  <Text style={{ marginBottom: 8 }}>Title</Text>\n  <Text style={{ marginBottom: 8 }}>Subtitle</Text>\n</View>\n\n// Correct – gap on parent\n<View style={{ gap: 8 }}>\n  <Text>Title</Text>\n  <Text>Subtitle</Text>\n</View>\n```\n\n**Use `padding` for space within, `gap` for space between:**\n\n```tsx\n<View style={{ padding: 16, gap: 12 }}>\n  <Text>First</Text>\n  <Text>Second</Text>\n</View>\n```\n\n**Use `experimental_backgroundImage` for linear gradients:**\n\n```tsx\n// Incorrect – third-party gradient library\n<LinearGradient colors={['#000', '#fff']} />\n\n// Correct – native CSS gradient syntax\n<View\n  style={{\n    experimental_backgroundImage: 'linear-gradient(to bottom, #000, #fff)',\n  }}\n/>\n```\n\n**Use CSS `boxShadow` string syntax for shadows:**\n\n```tsx\n// Incorrect – legacy shadow objects or elevation\n{ shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1 }\n{ elevation: 4 }\n\n// Correct – CSS box-shadow syntax\n{ boxShadow: '0 2px 8px rgba(0, 0, 0, 0.1)' }\n```\n\n**Avoid multiple font sizes – use weight and color for emphasis:**\n\n```tsx\n// Incorrect – varying font sizes for hierarchy\n<Text style={{ fontSize: 18 }}>Title</Text>\n<Text style={{ fontSize: 14 }}>Subtitle</Text>\n<Text style={{ fontSize: 12 }}>Caption</Text>\n\n// Correct – consistent size, vary weight and color\n<Text style={{ fontWeight: '600' }}>Title</Text>\n<Text style={{ color: '#666' }}>Subtitle</Text>\n<Text style={{ color: '#999' }}>Caption</Text>\n```\n\nLimiting font sizes creates visual consistency. Use `fontWeight` (bold/semibold)\n\nand grayscale colors for hierarchy instead.\n\n### 9.3 Use contentInset for Dynamic ScrollView Spacing\n\n**Impact: LOW (smoother updates, no layout recalculation)**\n\nWhen adding space to the top or bottom of a ScrollView that may change\n\n(keyboard, toolbars, dynamic content), use `contentInset` instead of padding.\n\nChanging `contentInset` doesn't trigger layout recalculation—it adjusts the\n\nscroll area without re-rendering content.\n\n**Incorrect: padding causes layout recalculation**\n\n```tsx\nfunction Feed({ bottomOffset }: { bottomOffset: number }) {\n  return (\n    <ScrollView contentContainerStyle={{ paddingBottom: bottomOffset }}>\n      {children}\n    </ScrollView>\n  )\n}\n// Changing bottomOffset triggers full layout recalculation\n```\n\n**Correct: contentInset for dynamic spacing**\n\n```tsx\nfunction Feed({ bottomOffset }: { bottomOffset: number }) {\n  return (\n    <ScrollView\n      contentInset={{ bottom: bottomOffset }}\n      scrollIndicatorInsets={{ bottom: bottomOffset }}\n    >\n      {children}\n    </ScrollView>\n  )\n}\n// Changing bottomOffset only adjusts scroll bounds\n```\n\nUse `scrollIndicatorInsets` alongside `contentInset` to keep the scroll\n\nindicator aligned. For static spacing that never changes, padding is fine.\n\n### 9.4 Use contentInsetAdjustmentBehavior for Safe Areas\n\n**Impact: MEDIUM (native safe area handling, no layout shifts)**\n\nUse `contentInsetAdjustmentBehavior=\"automatic\"` on the root ScrollView instead of wrapping content in SafeAreaView or manual padding. This lets iOS handle safe area insets natively with proper scroll behavior.\n\n**Incorrect: SafeAreaView wrapper**\n\n```tsx\nimport { SafeAreaView, ScrollView, View, Text } from 'react-native'\n\nfunction MyScreen() {\n  return (\n    <SafeAreaView style={{ flex: 1 }}>\n      <ScrollView>\n        <View>\n          <Text>Content</Text>\n        </View>\n      </ScrollView>\n    </SafeAreaView>\n  )\n}\n```\n\n**Incorrect: manual safe area padding**\n\n```tsx\nimport { ScrollView, View, Text } from 'react-native'\nimport { useSafeAreaInsets } from 'react-native-safe-area-context'\n\nfunction MyScreen() {\n  const insets = useSafeAreaInsets()\n\n  return (\n    <ScrollView contentContainerStyle={{ paddingTop: insets.top }}>\n      <View>\n        <Text>Content</Text>\n      </View>\n    </ScrollView>\n  )\n}\n```\n\n**Correct: native content inset adjustment**\n\n```tsx\nimport { ScrollView, View, Text } from 'react-native'\n\nfunction MyScreen() {\n  return (\n    <ScrollView contentInsetAdjustmentBehavior='automatic'>\n      <View>\n        <Text>Content</Text>\n      </View>\n    </ScrollView>\n  )\n}\n```\n\nThe native approach handles dynamic safe areas (keyboard, toolbars) and allows content to scroll behind the status bar naturally.\n\n### 9.5 Use expo-image for Optimized Images\n\n**Impact: HIGH (memory efficiency, caching, blurhash placeholders, progressive loading)**\n\nUse `expo-image` instead of React Native's `Image`. It provides memory-efficient caching, blurhash placeholders, progressive loading, and better performance for lists.\n\n**Incorrect: React Native Image**\n\n```tsx\nimport { Image } from 'react-native'\n\nfunction Avatar({ url }: { url: string }) {\n  return <Image source={{ uri: url }} style={styles.avatar} />\n}\n```\n\n**Correct: expo-image**\n\n```tsx\nimport { Image } from 'expo-image'\n\nfunction Avatar({ url }: { url: string }) {\n  return <Image source={{ uri: url }} style={styles.avatar} />\n}\n```\n\n**With blurhash placeholder:**\n\n```tsx\n<Image\n  source={{ uri: url }}\n  placeholder={{ blurhash: 'LGF5]+Yk^6#M@-5c,1J5@[or[Q6.' }}\n  contentFit=\"cover\"\n  transition={200}\n  style={styles.image}\n/>\n```\n\n**With priority and caching:**\n\n```tsx\n<Image\n  source={{ uri: url }}\n  priority=\"high\"\n  cachePolicy=\"memory-disk\"\n  style={styles.hero}\n/>\n```\n\n**Key props:**\n\n- `placeholder` — Blurhash or thumbnail while loading\n\n- `contentFit` — `cover`, `contain`, `fill`, `scale-down`\n\n- `transition` — Fade-in duration (ms)\n\n- `priority` — `low`, `normal`, `high`\n\n- `cachePolicy` — `memory`, `disk`, `memory-disk`, `none`\n\n- `recyclingKey` — Unique key for list recycling\n\nFor cross-platform (web + native), use `SolitoImage` from `solito/image` which uses `expo-image` under the hood.\n\nReference: [https://docs.expo.dev/versions/latest/sdk/image/](https://docs.expo.dev/versions/latest/sdk/image/)\n\n### 9.6 Use Galeria for Image Galleries and Lightbox\n\n**Impact: MEDIUM**\n\nFor image galleries with lightbox (tap to fullscreen), use `@nandorojo/galeria`.\n\nIt provides native shared element transitions with pinch-to-zoom, double-tap\n\nzoom, and pan-to-close. Works with any image component including `expo-image`.\n\n**Incorrect: custom modal implementation**\n\n```tsx\nfunction ImageGallery({ urls }: { urls: string[] }) {\n  const [selected, setSelected] = useState<string | null>(null)\n\n  return (\n    <>\n      {urls.map((url) => (\n        <Pressable key={url} onPress={() => setSelected(url)}>\n          <Image source={{ uri: url }} style={styles.thumbnail} />\n        </Pressable>\n      ))}\n      <Modal visible={!!selected} onRequestClose={() => setSelected(null)}>\n        <Image source={{ uri: selected! }} style={styles.fullscreen} />\n      </Modal>\n    </>\n  )\n}\n```\n\n**Correct: Galeria with expo-image**\n\n```tsx\nimport { Galeria } from '@nandorojo/galeria'\nimport { Image } from 'expo-image'\n\nfunction ImageGallery({ urls }: { urls: string[] }) {\n  return (\n    <Galeria urls={urls}>\n      {urls.map((url, index) => (\n        <Galeria.Image index={index} key={url}>\n          <Image source={{ uri: url }} style={styles.thumbnail} />\n        </Galeria.Image>\n      ))}\n    </Galeria>\n  )\n}\n```\n\n**Single image:**\n\n```tsx\nimport { Galeria } from '@nandorojo/galeria'\nimport { Image } from 'expo-image'\n\nfunction Avatar({ url }: { url: string }) {\n  return (\n    <Galeria urls={[url]}>\n      <Galeria.Image>\n        <Image source={{ uri: url }} style={styles.avatar} />\n      </Galeria.Image>\n    </Galeria>\n  )\n}\n```\n\n**With low-res thumbnails and high-res fullscreen:**\n\n```tsx\n<Galeria urls={highResUrls}>\n  {lowResUrls.map((url, index) => (\n    <Galeria.Image index={index} key={url}>\n      <Image source={{ uri: url }} style={styles.thumbnail} />\n    </Galeria.Image>\n  ))}\n</Galeria>\n```\n\n**With FlashList:**\n\n```tsx\n<Galeria urls={urls}>\n  <FlashList\n    data={urls}\n    renderItem={({ item, index }) => (\n      <Galeria.Image index={index}>\n        <Image source={{ uri: item }} style={styles.thumbnail} />\n      </Galeria.Image>\n    )}\n    numColumns={3}\n    estimatedItemSize={100}\n  />\n</Galeria>\n```\n\nWorks with `expo-image`, `SolitoImage`, `react-native` Image, or any image\n\ncomponent.\n\nReference: [https://github.com/nandorojo/galeria](https://github.com/nandorojo/galeria)\n\n### 9.7 Use Native Menus for Dropdowns and Context Menus\n\n**Impact: HIGH (native accessibility, platform-consistent UX)**\n\nUse native platform menus instead of custom JS implementations. Native menus\n\nprovide built-in accessibility, consistent platform UX, and better performance.\n\nUse [zeego](https://zeego.dev) for cross-platform native menus.\n\n**Incorrect: custom JS menu**\n\n```tsx\nimport { useState } from 'react'\nimport { View, Pressable, Text } from 'react-native'\n\nfunction MyMenu() {\n  const [open, setOpen] = useState(false)\n\n  return (\n    <View>\n      <Pressable onPress={() => setOpen(!open)}>\n        <Text>Open Menu</Text>\n      </Pressable>\n      {open && (\n        <View style={{ position: 'absolute', top: 40 }}>\n          <Pressable onPress={() => console.log('edit')}>\n            <Text>Edit</Text>\n          </Pressable>\n          <Pressable onPress={() => console.log('delete')}>\n            <Text>Delete</Text>\n          </Pressable>\n        </View>\n      )}\n    </View>\n  )\n}\n```\n\n**Correct: native menu with zeego**\n\n```tsx\nimport * as DropdownMenu from 'zeego/dropdown-menu'\n\nfunction MyMenu() {\n  return (\n    <DropdownMenu.Root>\n      <DropdownMenu.Trigger>\n        <Pressable>\n          <Text>Open Menu</Text>\n        </Pressable>\n      </DropdownMenu.Trigger>\n\n      <DropdownMenu.Content>\n        <DropdownMenu.Item key='edit' onSelect={() => console.log('edit')}>\n          <DropdownMenu.ItemTitle>Edit</DropdownMenu.ItemTitle>\n        </DropdownMenu.Item>\n\n        <DropdownMenu.Item\n          key='delete'\n          destructive\n          onSelect={() => console.log('delete')}\n        >\n          <DropdownMenu.ItemTitle>Delete</DropdownMenu.ItemTitle>\n        </DropdownMenu.Item>\n      </DropdownMenu.Content>\n    </DropdownMenu.Root>\n  )\n}\n```\n\n**Context menu: long-press**\n\n```tsx\nimport * as ContextMenu from 'zeego/context-menu'\n\nfunction MyContextMenu() {\n  return (\n    <ContextMenu.Root>\n      <ContextMenu.Trigger>\n        <View style={{ padding: 20 }}>\n          <Text>Long press me</Text>\n        </View>\n      </ContextMenu.Trigger>\n\n      <ContextMenu.Content>\n        <ContextMenu.Item key='copy' onSelect={() => console.log('copy')}>\n          <ContextMenu.ItemTitle>Copy</ContextMenu.ItemTitle>\n        </ContextMenu.Item>\n\n        <ContextMenu.Item key='paste' onSelect={() => console.log('paste')}>\n          <ContextMenu.ItemTitle>Paste</ContextMenu.ItemTitle>\n        </ContextMenu.Item>\n      </ContextMenu.Content>\n    </ContextMenu.Root>\n  )\n}\n```\n\n**Checkbox items:**\n\n```tsx\nimport * as DropdownMenu from 'zeego/dropdown-menu'\n\nfunction SettingsMenu() {\n  const [notifications, setNotifications] = useState(true)\n\n  return (\n    <DropdownMenu.Root>\n      <DropdownMenu.Trigger>\n        <Pressable>\n          <Text>Settings</Text>\n        </Pressable>\n      </DropdownMenu.Trigger>\n\n      <DropdownMenu.Content>\n        <DropdownMenu.CheckboxItem\n          key='notifications'\n          value={notifications}\n          onValueChange={() => setNotifications((prev) => !prev)}\n        >\n          <DropdownMenu.ItemIndicator />\n          <DropdownMenu.ItemTitle>Notifications</DropdownMenu.ItemTitle>\n        </DropdownMenu.CheckboxItem>\n      </DropdownMenu.Content>\n    </DropdownMenu.Root>\n  )\n}\n```\n\n**Submenus:**\n\n```tsx\nimport * as DropdownMenu from 'zeego/dropdown-menu'\n\nfunction MenuWithSubmenu() {\n  return (\n    <DropdownMenu.Root>\n      <DropdownMenu.Trigger>\n        <Pressable>\n          <Text>Options</Text>\n        </Pressable>\n      </DropdownMenu.Trigger>\n\n      <DropdownMenu.Content>\n        <DropdownMenu.Item key='home' onSelect={() => console.log('home')}>\n          <DropdownMenu.ItemTitle>Home</DropdownMenu.ItemTitle>\n        </DropdownMenu.Item>\n\n        <DropdownMenu.Sub>\n          <DropdownMenu.SubTrigger key='more'>\n            <DropdownMenu.ItemTitle>More Options</DropdownMenu.ItemTitle>\n          </DropdownMenu.SubTrigger>\n\n          <DropdownMenu.SubContent>\n            <DropdownMenu.Item key='settings'>\n              <DropdownMenu.ItemTitle>Settings</DropdownMenu.ItemTitle>\n            </DropdownMenu.Item>\n\n            <DropdownMenu.Item key='help'>\n              <DropdownMenu.ItemTitle>Help</DropdownMenu.ItemTitle>\n            </DropdownMenu.Item>\n          </DropdownMenu.SubContent>\n        </DropdownMenu.Sub>\n      </DropdownMenu.Content>\n    </DropdownMenu.Root>\n  )\n}\n```\n\nReference: [https://zeego.dev/components/dropdown-menu](https://zeego.dev/components/dropdown-menu)\n\n### 9.8 Use Native Modals Over JS-Based Bottom Sheets\n\n**Impact: HIGH (native performance, gestures, accessibility)**\n\nUse native `<Modal>` with `presentationStyle=\"formSheet\"` or React Navigation\n\nv7's native form sheet instead of JS-based bottom sheet libraries. Native modals\n\nhave built-in gestures, accessibility, and better performance. Rely on native UI\n\nfor low-level primitives.\n\n**Incorrect: JS-based bottom sheet**\n\n```tsx\nimport BottomSheet from 'custom-js-bottom-sheet'\n\nfunction MyScreen() {\n  const sheetRef = useRef<BottomSheet>(null)\n\n  return (\n    <View style={{ flex: 1 }}>\n      <Button onPress={() => sheetRef.current?.expand()} title='Open' />\n      <BottomSheet ref={sheetRef} snapPoints={['50%', '90%']}>\n        <View>\n          <Text>Sheet content</Text>\n        </View>\n      </BottomSheet>\n    </View>\n  )\n}\n```\n\n**Correct: native Modal with formSheet**\n\n```tsx\nimport { Modal, View, Text, Button } from 'react-native'\n\nfunction MyScreen() {\n  const [visible, setVisible] = useState(false)\n\n  return (\n    <View style={{ flex: 1 }}>\n      <Button onPress={() => setVisible(true)} title='Open' />\n      <Modal\n        visible={visible}\n        presentationStyle='formSheet'\n        animationType='slide'\n        onRequestClose={() => setVisible(false)}\n      >\n        <View>\n          <Text>Sheet content</Text>\n        </View>\n      </Modal>\n    </View>\n  )\n}\n```\n\n**Correct: React Navigation v7 native form sheet**\n\n```tsx\n// In your navigator\n<Stack.Screen\n  name='Details'\n  component={DetailsScreen}\n  options={{\n    presentation: 'formSheet',\n    sheetAllowedDetents: 'fitToContents',\n  }}\n/>\n```\n\nNative modals provide swipe-to-dismiss, proper keyboard avoidance, and\n\naccessibility out of the box.\n\n### 9.9 Use Pressable Instead of Touchable Components\n\n**Impact: LOW (modern API, more flexible)**\n\nNever use `TouchableOpacity` or `TouchableHighlight`. Use `Pressable` from\n\n`react-native` or `react-native-gesture-handler` instead.\n\n**Incorrect: legacy Touchable components**\n\n```tsx\nimport { TouchableOpacity } from 'react-native'\n\nfunction MyButton({ onPress }: { onPress: () => void }) {\n  return (\n    <TouchableOpacity onPress={onPress} activeOpacity={0.7}>\n      <Text>Press me</Text>\n    </TouchableOpacity>\n  )\n}\n```\n\n**Correct: Pressable**\n\n```tsx\nimport { Pressable } from 'react-native'\n\nfunction MyButton({ onPress }: { onPress: () => void }) {\n  return (\n    <Pressable onPress={onPress}>\n      <Text>Press me</Text>\n    </Pressable>\n  )\n}\n```\n\n**Correct: Pressable from gesture handler for lists**\n\n```tsx\nimport { Pressable } from 'react-native-gesture-handler'\n\nfunction ListItem({ onPress }: { onPress: () => void }) {\n  return (\n    <Pressable onPress={onPress}>\n      <Text>Item</Text>\n    </Pressable>\n  )\n}\n```\n\nUse `react-native-gesture-handler` Pressable inside scrollable lists for better\n\ngesture coordination, as long as you are using the ScrollView from\n\n`react-native-gesture-handler` as well.\n\n**For animated press states (scale, opacity changes):** Use `GestureDetector`\n\nwith Reanimated shared values instead of Pressable's style callback. See the\n\n`animation-gesture-detector-press` rule.\n\n---\n\n## 10. Design System\n\n**Impact: MEDIUM**\n\nArchitecture patterns for building maintainable component\nlibraries.\n\n### 10.1 Use Compound Components Over Polymorphic Children\n\n**Impact: MEDIUM (flexible composition, clearer API)**\n\nDon't create components that can accept a string if they aren't a text node. If\n\na component can receive a string child, it must be a dedicated `*Text`\n\ncomponent. For components like buttons, which can have both a View (or\n\nPressable) together with text, use compound components, such a `Button`,\n\n`ButtonText`, and `ButtonIcon`.\n\n**Incorrect: polymorphic children**\n\n```tsx\nimport { Pressable, Text } from 'react-native'\n\ntype ButtonProps = {\n  children: string | React.ReactNode\n  icon?: React.ReactNode\n}\n\nfunction Button({ children, icon }: ButtonProps) {\n  return (\n    <Pressable>\n      {icon}\n      {typeof children === 'string' ? <Text>{children}</Text> : children}\n    </Pressable>\n  )\n}\n\n// Usage is ambiguous\n<Button icon={<Icon />}>Save</Button>\n<Button><CustomText>Save</CustomText></Button>\n```\n\n**Correct: compound components**\n\n```tsx\nimport { Pressable, Text } from 'react-native'\n\nfunction Button({ children }: { children: React.ReactNode }) {\n  return <Pressable>{children}</Pressable>\n}\n\nfunction ButtonText({ children }: { children: React.ReactNode }) {\n  return <Text>{children}</Text>\n}\n\nfunction ButtonIcon({ children }: { children: React.ReactNode }) {\n  return <>{children}</>\n}\n\n// Usage is explicit and composable\n<Button>\n  <ButtonIcon><SaveIcon /></ButtonIcon>\n  <ButtonText>Save</ButtonText>\n</Button>\n\n<Button>\n  <ButtonText>Cancel</ButtonText>\n</Button>\n```\n\n---\n\n## 11. Monorepo\n\n**Impact: LOW**\n\nDependency management and native module configuration in\nmonorepos.\n\n### 11.1 Install Native Dependencies in App Directory\n\n**Impact: CRITICAL (required for autolinking to work)**\n\nIn a monorepo, packages with native code must be installed in the native app's\n\ndirectory directly. Autolinking only scans the app's `node_modules`—it won't\n\nfind native dependencies installed in other packages.\n\n**Incorrect: native dep in shared package only**\n\n```typescript\npackages/\n  ui/\n    package.json  # has react-native-reanimated\n  app/\n    package.json  # missing react-native-reanimated\n```\n\nAutolinking fails—native code not linked.\n\n**Correct: native dep in app directory**\n\n```json\n// packages/app/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"3.16.1\"\n  }\n}\n```\n\nEven if the shared package uses the native dependency, the app must also list it\n\nfor autolinking to detect and link the native code.\n\n### 11.2 Use Single Dependency Versions Across Monorepo\n\n**Impact: MEDIUM (avoids duplicate bundles, version conflicts)**\n\nUse a single version of each dependency across all packages in your monorepo.\n\nPrefer exact versions over ranges. Multiple versions cause duplicate code in\n\nbundles, runtime conflicts, and inconsistent behavior across packages.\n\nUse a tool like syncpack to enforce this. As a last resort, use yarn resolutions\n\nor npm overrides.\n\n**Incorrect: version ranges, multiple versions**\n\n```json\n// packages/app/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"^3.0.0\"\n  }\n}\n\n// packages/ui/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"^3.5.0\"\n  }\n}\n```\n\n**Correct: exact versions, single source of truth**\n\n```json\n// package.json (root)\n{\n  \"pnpm\": {\n    \"overrides\": {\n      \"react-native-reanimated\": \"3.16.1\"\n    }\n  }\n}\n\n// packages/app/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"3.16.1\"\n  }\n}\n\n// packages/ui/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"3.16.1\"\n  }\n}\n```\n\nUse your package manager's override/resolution feature to enforce versions at\n\nthe root. When adding dependencies, specify exact versions without `^` or `~`.\n\n---\n\n## 12. Third-Party Dependencies\n\n**Impact: LOW**\n\nWrapping and re-exporting third-party dependencies for\nmaintainability.\n\n### 12.1 Import from Design System Folder\n\n**Impact: LOW (enables global changes and easy refactoring)**\n\nRe-export dependencies from a design system folder. App code imports from there,\n\nnot directly from packages. This enables global changes and easy refactoring.\n\n**Incorrect: imports directly from package**\n\n```tsx\nimport { View, Text } from 'react-native'\nimport { Button } from '@ui/button'\n\nfunction Profile() {\n  return (\n    <View>\n      <Text>Hello</Text>\n      <Button>Save</Button>\n    </View>\n  )\n}\n```\n\n**Correct: imports from design system**\n\n```tsx\nimport { View } from '@/components/view'\nimport { Text } from '@/components/text'\nimport { Button } from '@/components/button'\n\nfunction Profile() {\n  return (\n    <View>\n      <Text>Hello</Text>\n      <Button>Save</Button>\n    </View>\n  )\n}\n```\n\nStart by simply re-exporting. Customize later without changing app code.\n\n---\n\n## 13. JavaScript\n\n**Impact: LOW**\n\nMicro-optimizations like hoisting expensive object creation.\n\n### 13.1 Hoist Intl Formatter Creation\n\n**Impact: LOW-MEDIUM (avoids expensive object recreation)**\n\nDon't create `Intl.DateTimeFormat`, `Intl.NumberFormat`, or\n\n`Intl.RelativeTimeFormat` inside render or loops. These are expensive to\n\ninstantiate. Hoist to module scope when the locale/options are static.\n\n**Incorrect: new formatter every render**\n\n```tsx\nfunction Price({ amount }: { amount: number }) {\n  const formatter = new Intl.NumberFormat('en-US', {\n    style: 'currency',\n    currency: 'USD',\n  })\n  return <Text>{formatter.format(amount)}</Text>\n}\n```\n\n**Correct: hoisted to module scope**\n\n```tsx\nconst currencyFormatter = new Intl.NumberFormat('en-US', {\n  style: 'currency',\n  currency: 'USD',\n})\n\nfunction Price({ amount }: { amount: number }) {\n  return <Text>{currencyFormatter.format(amount)}</Text>\n}\n```\n\n**For dynamic locales, memoize:**\n\n```tsx\nconst dateFormatter = useMemo(\n  () => new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }),\n  [locale]\n)\n```\n\n**Common formatters to hoist:**\n\n```tsx\n// Module-level formatters\nconst dateFormatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' })\nconst timeFormatter = new Intl.DateTimeFormat('en-US', { timeStyle: 'short' })\nconst percentFormatter = new Intl.NumberFormat('en-US', { style: 'percent' })\nconst relativeFormatter = new Intl.RelativeTimeFormat('en-US', {\n  numeric: 'auto',\n})\n```\n\nCreating `Intl` objects is significantly more expensive than `RegExp` or plain\n\nobjects—each instantiation parses locale data and builds internal lookup tables.\n\n---\n\n## 14. Fonts\n\n**Impact: LOW**\n\nNative font loading for improved performance.\n\n### 14.1 Load fonts natively at build time\n\n**Impact: LOW (fonts available at launch, no async loading)**\n\nUse the `expo-font` config plugin to embed fonts at build time instead of\n\n`useFonts` or `Font.loadAsync`. Embedded fonts are more efficient.\n\n[Expo Font Documentation](https://docs.expo.dev/versions/latest/sdk/font/)\n\n**Incorrect: async font loading**\n\n```tsx\nimport { useFonts } from 'expo-font'\nimport { Text, View } from 'react-native'\n\nfunction App() {\n  const [fontsLoaded] = useFonts({\n    'Geist-Bold': require('./assets/fonts/Geist-Bold.otf'),\n  })\n\n  if (!fontsLoaded) {\n    return null\n  }\n\n  return (\n    <View>\n      <Text style={{ fontFamily: 'Geist-Bold' }}>Hello</Text>\n    </View>\n  )\n}\n```\n\n**Correct: config plugin, fonts embedded at build**\n\n```tsx\nimport { Text, View } from 'react-native'\n\nfunction App() {\n  // No loading state needed—font is already available\n  return (\n    <View>\n      <Text style={{ fontFamily: 'Geist-Bold' }}>Hello</Text>\n    </View>\n  )\n}\n```\n\nAfter adding fonts to the config plugin, run `npx expo prebuild` and rebuild the\n\nnative app.\n\n---\n\n## References\n\n1. [https://react.dev](https://react.dev)\n2. [https://reactnative.dev](https://reactnative.dev)\n3. [https://docs.swmansion.com/react-native-reanimated](https://docs.swmansion.com/react-native-reanimated)\n4. [https://docs.swmansion.com/react-native-gesture-handler](https://docs.swmansion.com/react-native-gesture-handler)\n5. [https://docs.expo.dev](https://docs.expo.dev)\n6. [https://legendapp.com/open-source/legend-list](https://legendapp.com/open-source/legend-list)\n7. [https://github.com/nandorojo/galeria](https://github.com/nandorojo/galeria)\n8. [https://zeego.dev](https://zeego.dev)\n"
  },
  {
    "path": "skills/react-native-skills/README.md",
    "content": "# React Native Guidelines\n\nA structured repository for creating and maintaining React Native Best Practices\noptimized for agents and LLMs.\n\n## Structure\n\n- `rules/` - Individual rule files (one per rule)\n  - `_sections.md` - Section metadata (titles, impacts, descriptions)\n  - `_template.md` - Template for creating new rules\n  - `area-description.md` - Individual rule files\n- `metadata.json` - Document metadata (version, organization, abstract)\n- **`AGENTS.md`** - Compiled output (generated)\n\n## Rules\n\n### Core Rendering (CRITICAL)\n\n- `rendering-text-in-text-component.md` - Wrap strings in Text components\n- `rendering-no-falsy-and.md` - Avoid falsy && operator in JSX\n\n### List Performance (HIGH)\n\n- `list-performance-virtualize.md` - Use virtualized lists (LegendList,\n  FlashList)\n- `list-performance-function-references.md` - Keep stable object references\n- `list-performance-callbacks.md` - Hoist callbacks to list root\n- `list-performance-inline-objects.md` - Avoid inline objects in renderItem\n- `list-performance-item-memo.md` - Pass primitives for memoization\n- `list-performance-item-expensive.md` - Keep list items lightweight\n- `list-performance-images.md` - Use compressed images in lists\n- `list-performance-item-types.md` - Use item types for heterogeneous lists\n\n### Animation (HIGH)\n\n- `animation-gpu-properties.md` - Animate transform/opacity instead of layout\n- `animation-gesture-detector-press.md` - Use GestureDetector for press\n  animations\n- `animation-derived-value.md` - Prefer useDerivedValue over useAnimatedReaction\n\n### Scroll Performance (HIGH)\n\n- `scroll-position-no-state.md` - Never track scroll in useState\n\n### Navigation (HIGH)\n\n- `navigation-native-navigators.md` - Use native stack and native tabs\n\n### React State (MEDIUM)\n\n- `react-state-dispatcher.md` - Use functional setState updates\n- `react-state-fallback.md` - State should represent user intent only\n- `react-state-minimize.md` - Minimize state variables, derive values\n\n### State Architecture (MEDIUM)\n\n- `state-ground-truth.md` - State must represent ground truth\n\n### React Compiler (MEDIUM)\n\n- `react-compiler-destructure-functions.md` - Destructure functions early\n- `react-compiler-reanimated-shared-values.md` - Use .get()/.set() for shared\n  values\n\n### User Interface (MEDIUM)\n\n- `ui-expo-image.md` - Use expo-image for optimized images\n- `ui-image-gallery.md` - Use Galeria for lightbox/galleries\n- `ui-menus.md` - Native dropdown and context menus with Zeego\n- `ui-native-modals.md` - Use native Modal with formSheet\n- `ui-pressable.md` - Use Pressable instead of TouchableOpacity\n- `ui-measure-views.md` - Measuring view dimensions\n- `ui-safe-area-scroll.md` - Use contentInsetAdjustmentBehavior\n- `ui-scrollview-content-inset.md` - Use contentInset for dynamic spacing\n- `ui-styling.md` - Modern styling patterns (gap, boxShadow, gradients)\n\n### Design System (MEDIUM)\n\n- `design-system-compound-components.md` - Use compound components\n\n### Monorepo (LOW)\n\n- `monorepo-native-deps-in-app.md` - Install native deps in app directory\n- `monorepo-single-dependency-versions.md` - Single dependency versions\n\n### Third-Party Dependencies (LOW)\n\n- `imports-design-system-folder.md` - Import from design system folder\n\n### JavaScript (LOW)\n\n- `js-hoist-intl.md` - Hoist Intl formatter creation\n\n### Fonts (LOW)\n\n- `fonts-config-plugin.md` - Load fonts natively at build time\n\n## Creating a New Rule\n\n1. Copy `rules/_template.md` to `rules/area-description.md`\n2. Choose the appropriate area prefix:\n   - `rendering-` for Core Rendering\n   - `list-performance-` for List Performance\n   - `animation-` for Animation\n   - `scroll-` for Scroll Performance\n   - `navigation-` for Navigation\n   - `react-state-` for React State\n   - `state-` for State Architecture\n   - `react-compiler-` for React Compiler\n   - `ui-` for User Interface\n   - `design-system-` for Design System\n   - `monorepo-` for Monorepo\n   - `imports-` for Third-Party Dependencies\n   - `js-` for JavaScript\n   - `fonts-` for Fonts\n3. Fill in the frontmatter and content\n4. Ensure you have clear examples with explanations\n\n## Rule File Structure\n\nEach rule file should follow this structure:\n\n````markdown\n---\ntitle: Rule Title Here\nimpact: MEDIUM\nimpactDescription: Optional description\ntags: tag1, tag2, tag3\n---\n\n## Rule Title Here\n\nBrief explanation of the rule and why it matters.\n\n**Incorrect (description of what's wrong):**\n\n```tsx\n// Bad code example\n```\n````\n\n**Correct (description of what's right):**\n\n```tsx\n// Good code example\n```\n\nReference: [Link](https://example.com)\n\n```\n\n## File Naming Convention\n\n- Files starting with `_` are special (excluded from build)\n- Rule files: `area-description.md` (e.g., `animation-gpu-properties.md`)\n- Section is automatically inferred from filename prefix\n- Rules are sorted alphabetically by title within each section\n\n## Impact Levels\n\n- `CRITICAL` - Highest priority, causes crashes or broken UI\n- `HIGH` - Significant performance improvements\n- `MEDIUM` - Moderate performance improvements\n- `LOW` - Incremental improvements\n```\n"
  },
  {
    "path": "skills/react-native-skills/SKILL.md",
    "content": "---\nname: vercel-react-native-skills\ndescription:\n  React Native and Expo best practices for building performant mobile apps. Use\n  when building React Native components, optimizing list performance,\n  implementing animations, or working with native modules. Triggers on tasks\n  involving React Native, Expo, mobile performance, or native platform APIs.\nlicense: MIT\nmetadata:\n  author: vercel\n  version: '1.0.0'\n---\n\n# React Native Skills\n\nComprehensive best practices for React Native and Expo applications. Contains\nrules across multiple categories covering performance, animations, UI patterns,\nand platform-specific optimizations.\n\n## When to Apply\n\nReference these guidelines when:\n\n- Building React Native or Expo apps\n- Optimizing list and scroll performance\n- Implementing animations with Reanimated\n- Working with images and media\n- Configuring native modules or fonts\n- Structuring monorepo projects with native dependencies\n\n## Rule Categories by Priority\n\n| Priority | Category         | Impact   | Prefix               |\n| -------- | ---------------- | -------- | -------------------- |\n| 1        | List Performance | CRITICAL | `list-performance-`  |\n| 2        | Animation        | HIGH     | `animation-`         |\n| 3        | Navigation       | HIGH     | `navigation-`        |\n| 4        | UI Patterns      | HIGH     | `ui-`                |\n| 5        | State Management | MEDIUM   | `react-state-`       |\n| 6        | Rendering        | MEDIUM   | `rendering-`         |\n| 7        | Monorepo         | MEDIUM   | `monorepo-`          |\n| 8        | Configuration    | LOW      | `fonts-`, `imports-` |\n\n## Quick Reference\n\n### 1. List Performance (CRITICAL)\n\n- `list-performance-virtualize` - Use FlashList for large lists\n- `list-performance-item-memo` - Memoize list item components\n- `list-performance-callbacks` - Stabilize callback references\n- `list-performance-inline-objects` - Avoid inline style objects\n- `list-performance-function-references` - Extract functions outside render\n- `list-performance-images` - Optimize images in lists\n- `list-performance-item-expensive` - Move expensive work outside items\n- `list-performance-item-types` - Use item types for heterogeneous lists\n\n### 2. Animation (HIGH)\n\n- `animation-gpu-properties` - Animate only transform and opacity\n- `animation-derived-value` - Use useDerivedValue for computed animations\n- `animation-gesture-detector-press` - Use Gesture.Tap instead of Pressable\n\n### 3. Navigation (HIGH)\n\n- `navigation-native-navigators` - Use native stack and native tabs over JS navigators\n\n### 4. UI Patterns (HIGH)\n\n- `ui-expo-image` - Use expo-image for all images\n- `ui-image-gallery` - Use Galeria for image lightboxes\n- `ui-pressable` - Use Pressable over TouchableOpacity\n- `ui-safe-area-scroll` - Handle safe areas in ScrollViews\n- `ui-scrollview-content-inset` - Use contentInset for headers\n- `ui-menus` - Use native context menus\n- `ui-native-modals` - Use native modals when possible\n- `ui-measure-views` - Use onLayout, not measure()\n- `ui-styling` - Use StyleSheet.create or Nativewind\n\n### 5. State Management (MEDIUM)\n\n- `react-state-minimize` - Minimize state subscriptions\n- `react-state-dispatcher` - Use dispatcher pattern for callbacks\n- `react-state-fallback` - Show fallback on first render\n- `react-compiler-destructure-functions` - Destructure for React Compiler\n- `react-compiler-reanimated-shared-values` - Handle shared values with compiler\n\n### 6. Rendering (MEDIUM)\n\n- `rendering-text-in-text-component` - Wrap text in Text components\n- `rendering-no-falsy-and` - Avoid falsy && for conditional rendering\n\n### 7. Monorepo (MEDIUM)\n\n- `monorepo-native-deps-in-app` - Keep native dependencies in app package\n- `monorepo-single-dependency-versions` - Use single versions across packages\n\n### 8. Configuration (LOW)\n\n- `fonts-config-plugin` - Use config plugins for custom fonts\n- `imports-design-system-folder` - Organize design system imports\n- `js-hoist-intl` - Hoist Intl object creation\n\n## How to Use\n\nRead individual rule files for detailed explanations and code examples:\n\n```\nrules/list-performance-virtualize.md\nrules/animation-gpu-properties.md\n```\n\nEach rule file contains:\n\n- Brief explanation of why it matters\n- Incorrect code example with explanation\n- Correct code example with explanation\n- Additional context and references\n\n## Full Compiled Document\n\nFor the complete guide with all rules expanded: `AGENTS.md`\n"
  },
  {
    "path": "skills/react-native-skills/metadata.json",
    "content": "{\n  \"version\": \"1.0.0\",\n  \"organization\": \"Engineering\",\n  \"date\": \"January 2026\",\n  \"abstract\": \"Comprehensive performance optimization guide for React Native applications, designed for AI agents and LLMs. Contains 35+ rules across 13 categories, prioritized by impact from critical (core rendering, list performance) to incremental (fonts, imports). Each rule includes detailed explanations, real-world examples comparing incorrect vs. correct implementations, and specific impact metrics to guide automated refactoring and code generation.\",\n  \"references\": [\n    \"https://react.dev\",\n    \"https://reactnative.dev\",\n    \"https://docs.swmansion.com/react-native-reanimated\",\n    \"https://docs.swmansion.com/react-native-gesture-handler\",\n    \"https://docs.expo.dev\",\n    \"https://legendapp.com/open-source/legend-list\",\n    \"https://github.com/nandorojo/galeria\",\n    \"https://zeego.dev\"\n  ]\n}\n"
  },
  {
    "path": "skills/react-native-skills/rules/_sections.md",
    "content": "# Sections\n\nThis file defines all sections, their ordering, impact levels, and descriptions.\nThe section ID (in parentheses) is the filename prefix used to group rules.\n\n---\n\n## 1. Core Rendering (rendering)\n\n**Impact:** CRITICAL  \n**Description:** Fundamental React Native rendering rules. Violations cause\nruntime crashes or broken UI.\n\n## 2. List Performance (list-performance)\n\n**Impact:** HIGH  \n**Description:** Optimizing virtualized lists (FlatList, LegendList, FlashList)\nfor smooth scrolling and fast updates.\n\n## 3. Animation (animation)\n\n**Impact:** HIGH  \n**Description:** GPU-accelerated animations, Reanimated patterns, and avoiding\nrender thrashing during gestures.\n\n## 4. Scroll Performance (scroll)\n\n**Impact:** HIGH  \n**Description:** Tracking scroll position without causing render thrashing.\n\n## 5. Navigation (navigation)\n\n**Impact:** HIGH  \n**Description:** Using native navigators for stack and tab navigation instead of\nJS-based alternatives.\n\n## 6. React State (react-state)\n\n**Impact:** MEDIUM  \n**Description:** Patterns for managing React state to avoid stale closures and\nunnecessary re-renders.\n\n## 7. State Architecture (state)\n\n**Impact:** MEDIUM  \n**Description:** Ground truth principles for state variables and derived values.\n\n## 8. React Compiler (react-compiler)\n\n**Impact:** MEDIUM  \n**Description:** Compatibility patterns for React Compiler with React Native and\nReanimated.\n\n## 9. User Interface (ui)\n\n**Impact:** MEDIUM  \n**Description:** Native UI patterns for images, menus, modals, styling, and\nplatform-consistent interfaces.\n\n## 10. Design System (design-system)\n\n**Impact:** MEDIUM  \n**Description:** Architecture patterns for building maintainable component\nlibraries.\n\n## 11. Monorepo (monorepo)\n\n**Impact:** LOW  \n**Description:** Dependency management and native module configuration in\nmonorepos.\n\n## 12. Third-Party Dependencies (imports)\n\n**Impact:** LOW  \n**Description:** Wrapping and re-exporting third-party dependencies for\nmaintainability.\n\n## 13. JavaScript (js)\n\n**Impact:** LOW  \n**Description:** Micro-optimizations like hoisting expensive object creation.\n\n## 14. Fonts (fonts)\n\n**Impact:** LOW  \n**Description:** Native font loading for improved performance.\n"
  },
  {
    "path": "skills/react-native-skills/rules/_template.md",
    "content": "---\ntitle: Rule Title Here\nimpact: MEDIUM\nimpactDescription: Optional description of impact (e.g., \"20-50% improvement\")\ntags: tag1, tag2\n---\n\n## Rule Title Here\n\n**Impact: MEDIUM (optional impact description)**\n\nBrief explanation of the rule and why it matters. This should be clear and concise, explaining the performance implications.\n\n**Incorrect (description of what's wrong):**\n\n```typescript\n// Bad code example here\nconst bad = example()\n```\n\n**Correct (description of what's right):**\n\n```typescript\n// Good code example here\nconst good = example()\n```\n\nReference: [Link to documentation or resource](https://example.com)\n"
  },
  {
    "path": "skills/react-native-skills/rules/animation-derived-value.md",
    "content": "---\ntitle: Prefer useDerivedValue Over useAnimatedReaction\nimpact: MEDIUM\nimpactDescription: cleaner code, automatic dependency tracking\ntags: animation, reanimated, derived-value\n---\n\n## Prefer useDerivedValue Over useAnimatedReaction\n\nWhen deriving a shared value from another, use `useDerivedValue` instead of\n`useAnimatedReaction`. Derived values are declarative, automatically track\ndependencies, and return a value you can use directly. Animated reactions are\nfor side effects, not derivations.\n\n**Incorrect (useAnimatedReaction for derivation):**\n\n```tsx\nimport { useSharedValue, useAnimatedReaction } from 'react-native-reanimated'\n\nfunction MyComponent() {\n  const progress = useSharedValue(0)\n  const opacity = useSharedValue(1)\n\n  useAnimatedReaction(\n    () => progress.value,\n    (current) => {\n      opacity.value = 1 - current\n    }\n  )\n\n  // ...\n}\n```\n\n**Correct (useDerivedValue):**\n\n```tsx\nimport { useSharedValue, useDerivedValue } from 'react-native-reanimated'\n\nfunction MyComponent() {\n  const progress = useSharedValue(0)\n\n  const opacity = useDerivedValue(() => 1 - progress.get())\n\n  // ...\n}\n```\n\nUse `useAnimatedReaction` only for side effects that don't produce a value\n(e.g., triggering haptics, logging, calling `runOnJS`).\n\nReference:\n[Reanimated useDerivedValue](https://docs.swmansion.com/react-native-reanimated/docs/core/useDerivedValue)\n"
  },
  {
    "path": "skills/react-native-skills/rules/animation-gesture-detector-press.md",
    "content": "---\ntitle: Use GestureDetector for Animated Press States\nimpact: MEDIUM\nimpactDescription: UI thread animations, smoother press feedback\ntags: animation, gestures, press, reanimated\n---\n\n## Use GestureDetector for Animated Press States\n\nFor animated press states (scale, opacity on press), use `GestureDetector` with\n`Gesture.Tap()` and shared values instead of Pressable's\n`onPressIn`/`onPressOut`. Gesture callbacks run on the UI thread as worklets—no\nJS thread round-trip for press animations.\n\n**Incorrect (Pressable with JS thread callbacks):**\n\n```tsx\nimport { Pressable } from 'react-native'\nimport Animated, {\n  useSharedValue,\n  useAnimatedStyle,\n  withTiming,\n} from 'react-native-reanimated'\n\nfunction AnimatedButton({ onPress }: { onPress: () => void }) {\n  const scale = useSharedValue(1)\n\n  const animatedStyle = useAnimatedStyle(() => ({\n    transform: [{ scale: scale.value }],\n  }))\n\n  return (\n    <Pressable\n      onPress={onPress}\n      onPressIn={() => (scale.value = withTiming(0.95))}\n      onPressOut={() => (scale.value = withTiming(1))}\n    >\n      <Animated.View style={animatedStyle}>\n        <Text>Press me</Text>\n      </Animated.View>\n    </Pressable>\n  )\n}\n```\n\n**Correct (GestureDetector with UI thread worklets):**\n\n```tsx\nimport { Gesture, GestureDetector } from 'react-native-gesture-handler'\nimport Animated, {\n  useSharedValue,\n  useAnimatedStyle,\n  withTiming,\n  interpolate,\n  runOnJS,\n} from 'react-native-reanimated'\n\nfunction AnimatedButton({ onPress }: { onPress: () => void }) {\n  // Store the press STATE (0 = not pressed, 1 = pressed)\n  const pressed = useSharedValue(0)\n\n  const tap = Gesture.Tap()\n    .onBegin(() => {\n      pressed.set(withTiming(1))\n    })\n    .onFinalize(() => {\n      pressed.set(withTiming(0))\n    })\n    .onEnd(() => {\n      runOnJS(onPress)()\n    })\n\n  // Derive visual values from the state\n  const animatedStyle = useAnimatedStyle(() => ({\n    transform: [\n      { scale: interpolate(withTiming(pressed.get()), [0, 1], [1, 0.95]) },\n    ],\n  }))\n\n  return (\n    <GestureDetector gesture={tap}>\n      <Animated.View style={animatedStyle}>\n        <Text>Press me</Text>\n      </Animated.View>\n    </GestureDetector>\n  )\n}\n```\n\nStore the press **state** (0 or 1), then derive the scale via `interpolate`.\nThis keeps the shared value as ground truth. Use `runOnJS` to call JS functions\nfrom worklets. Use `.set()` and `.get()` for React Compiler compatibility.\n\nReference:\n[Gesture Handler Tap Gesture](https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/tap-gesture)\n"
  },
  {
    "path": "skills/react-native-skills/rules/animation-gpu-properties.md",
    "content": "---\ntitle: Animate Transform and Opacity Instead of Layout Properties\nimpact: HIGH\nimpactDescription: GPU-accelerated animations, no layout recalculation\ntags: animation, performance, reanimated, transform, opacity\n---\n\n## Animate Transform and Opacity Instead of Layout Properties\n\nAvoid animating `width`, `height`, `top`, `left`, `margin`, or `padding`. These trigger layout recalculation on every frame. Instead, use `transform` (scale, translate) and `opacity` which run on the GPU without triggering layout.\n\n**Incorrect (animates height, triggers layout every frame):**\n\n```tsx\nimport Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated'\n\nfunction CollapsiblePanel({ expanded }: { expanded: boolean }) {\n  const animatedStyle = useAnimatedStyle(() => ({\n    height: withTiming(expanded ? 200 : 0), // triggers layout on every frame\n    overflow: 'hidden',\n  }))\n\n  return <Animated.View style={animatedStyle}>{children}</Animated.View>\n}\n```\n\n**Correct (animates scaleY, GPU-accelerated):**\n\n```tsx\nimport Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated'\n\nfunction CollapsiblePanel({ expanded }: { expanded: boolean }) {\n  const animatedStyle = useAnimatedStyle(() => ({\n    transform: [\n      { scaleY: withTiming(expanded ? 1 : 0) },\n    ],\n    opacity: withTiming(expanded ? 1 : 0),\n  }))\n\n  return (\n    <Animated.View style={[{ height: 200, transformOrigin: 'top' }, animatedStyle]}>\n      {children}\n    </Animated.View>\n  )\n}\n```\n\n**Correct (animates translateY for slide animations):**\n\n```tsx\nimport Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated'\n\nfunction SlideIn({ visible }: { visible: boolean }) {\n  const animatedStyle = useAnimatedStyle(() => ({\n    transform: [\n      { translateY: withTiming(visible ? 0 : 100) },\n    ],\n    opacity: withTiming(visible ? 1 : 0),\n  }))\n\n  return <Animated.View style={animatedStyle}>{children}</Animated.View>\n}\n```\n\nGPU-accelerated properties: `transform` (translate, scale, rotate), `opacity`. Everything else triggers layout.\n"
  },
  {
    "path": "skills/react-native-skills/rules/design-system-compound-components.md",
    "content": "---\ntitle: Use Compound Components Over Polymorphic Children\nimpact: MEDIUM\nimpactDescription: flexible composition, clearer API\ntags: design-system, components, composition\n---\n\n## Use Compound Components Over Polymorphic Children\n\nDon't create components that can accept a string if they aren't a text node. If\na component can receive a string child, it must be a dedicated `*Text`\ncomponent. For components like buttons, which can have both a View (or\nPressable) together with text, use compound components, such a `Button`,\n`ButtonText`, and `ButtonIcon`.\n\n**Incorrect (polymorphic children):**\n\n```tsx\nimport { Pressable, Text } from 'react-native'\n\ntype ButtonProps = {\n  children: string | React.ReactNode\n  icon?: React.ReactNode\n}\n\nfunction Button({ children, icon }: ButtonProps) {\n  return (\n    <Pressable>\n      {icon}\n      {typeof children === 'string' ? <Text>{children}</Text> : children}\n    </Pressable>\n  )\n}\n\n// Usage is ambiguous\n<Button icon={<Icon />}>Save</Button>\n<Button><CustomText>Save</CustomText></Button>\n```\n\n**Correct (compound components):**\n\n```tsx\nimport { Pressable, Text } from 'react-native'\n\nfunction Button({ children }: { children: React.ReactNode }) {\n  return <Pressable>{children}</Pressable>\n}\n\nfunction ButtonText({ children }: { children: React.ReactNode }) {\n  return <Text>{children}</Text>\n}\n\nfunction ButtonIcon({ children }: { children: React.ReactNode }) {\n  return <>{children}</>\n}\n\n// Usage is explicit and composable\n<Button>\n  <ButtonIcon><SaveIcon /></ButtonIcon>\n  <ButtonText>Save</ButtonText>\n</Button>\n\n<Button>\n  <ButtonText>Cancel</ButtonText>\n</Button>\n```\n"
  },
  {
    "path": "skills/react-native-skills/rules/fonts-config-plugin.md",
    "content": "---\ntitle: Load fonts natively at build time\nimpact: LOW\nimpactDescription: fonts available at launch, no async loading\ntags: fonts, expo, performance, config-plugin\n---\n\n## Use Expo Config Plugin for Font Loading\n\nUse the `expo-font` config plugin to embed fonts at build time instead of\n`useFonts` or `Font.loadAsync`. Embedded fonts are more efficient.\n\n**Incorrect (async font loading):**\n\n```tsx\nimport { useFonts } from 'expo-font'\nimport { Text, View } from 'react-native'\n\nfunction App() {\n  const [fontsLoaded] = useFonts({\n    'Geist-Bold': require('./assets/fonts/Geist-Bold.otf'),\n  })\n\n  if (!fontsLoaded) {\n    return null\n  }\n\n  return (\n    <View>\n      <Text style={{ fontFamily: 'Geist-Bold' }}>Hello</Text>\n    </View>\n  )\n}\n```\n\n**Correct (config plugin, fonts embedded at build):**\n\n```json\n// app.json\n{\n  \"expo\": {\n    \"plugins\": [\n      [\n        \"expo-font\",\n        {\n          \"fonts\": [\"./assets/fonts/Geist-Bold.otf\"]\n        }\n      ]\n    ]\n  }\n}\n```\n\n```tsx\nimport { Text, View } from 'react-native'\n\nfunction App() {\n  // No loading state needed—font is already available\n  return (\n    <View>\n      <Text style={{ fontFamily: 'Geist-Bold' }}>Hello</Text>\n    </View>\n  )\n}\n```\n\nAfter adding fonts to the config plugin, run `npx expo prebuild` and rebuild the\nnative app.\n\nReference:\n[Expo Font Documentation](https://docs.expo.dev/versions/latest/sdk/font/)\n"
  },
  {
    "path": "skills/react-native-skills/rules/imports-design-system-folder.md",
    "content": "---\ntitle: Import from Design System Folder\nimpact: LOW\nimpactDescription: enables global changes and easy refactoring\ntags: imports, architecture, design-system\n---\n\n## Import from Design System Folder\n\nRe-export dependencies from a design system folder. App code imports from there,\nnot directly from packages. This enables global changes and easy refactoring.\n\n**Incorrect (imports directly from package):**\n\n```tsx\nimport { View, Text } from 'react-native'\nimport { Button } from '@ui/button'\n\nfunction Profile() {\n  return (\n    <View>\n      <Text>Hello</Text>\n      <Button>Save</Button>\n    </View>\n  )\n}\n```\n\n**Correct (imports from design system):**\n\n```tsx\n// components/view.tsx\nimport { View as RNView } from 'react-native'\n\n// ideal: pick the props you will actually use to control implementation\nexport function View(\n  props: Pick<React.ComponentProps<typeof RNView>, 'style' | 'children'>\n) {\n  return <RNView {...props} />\n}\n```\n\n```tsx\n// components/text.tsx\nexport { Text } from 'react-native'\n```\n\n```tsx\n// components/button.tsx\nexport { Button } from '@ui/button'\n```\n\n```tsx\nimport { View } from '@/components/view'\nimport { Text } from '@/components/text'\nimport { Button } from '@/components/button'\n\nfunction Profile() {\n  return (\n    <View>\n      <Text>Hello</Text>\n      <Button>Save</Button>\n    </View>\n  )\n}\n```\n\nStart by simply re-exporting. Customize later without changing app code.\n"
  },
  {
    "path": "skills/react-native-skills/rules/js-hoist-intl.md",
    "content": "---\ntitle: Hoist Intl Formatter Creation\nimpact: LOW-MEDIUM\nimpactDescription: avoids expensive object recreation\ntags: javascript, intl, optimization, memoization\n---\n\n## Hoist Intl Formatter Creation\n\nDon't create `Intl.DateTimeFormat`, `Intl.NumberFormat`, or\n`Intl.RelativeTimeFormat` inside render or loops. These are expensive to\ninstantiate. Hoist to module scope when the locale/options are static.\n\n**Incorrect (new formatter every render):**\n\n```tsx\nfunction Price({ amount }: { amount: number }) {\n  const formatter = new Intl.NumberFormat('en-US', {\n    style: 'currency',\n    currency: 'USD',\n  })\n  return <Text>{formatter.format(amount)}</Text>\n}\n```\n\n**Correct (hoisted to module scope):**\n\n```tsx\nconst currencyFormatter = new Intl.NumberFormat('en-US', {\n  style: 'currency',\n  currency: 'USD',\n})\n\nfunction Price({ amount }: { amount: number }) {\n  return <Text>{currencyFormatter.format(amount)}</Text>\n}\n```\n\n**For dynamic locales, memoize:**\n\n```tsx\nconst dateFormatter = useMemo(\n  () => new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }),\n  [locale]\n)\n```\n\n**Common formatters to hoist:**\n\n```tsx\n// Module-level formatters\nconst dateFormatter = new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' })\nconst timeFormatter = new Intl.DateTimeFormat('en-US', { timeStyle: 'short' })\nconst percentFormatter = new Intl.NumberFormat('en-US', { style: 'percent' })\nconst relativeFormatter = new Intl.RelativeTimeFormat('en-US', {\n  numeric: 'auto',\n})\n```\n\nCreating `Intl` objects is significantly more expensive than `RegExp` or plain\nobjects—each instantiation parses locale data and builds internal lookup tables.\n"
  },
  {
    "path": "skills/react-native-skills/rules/list-performance-callbacks.md",
    "content": "---\ntitle: Hoist callbacks to the root of lists\nimpact: MEDIUM\nimpactDescription: Fewer re-renders and faster lists\ntags: tag1, tag2\n---\n\n## List performance callbacks\n\n**Impact: HIGH (Fewer re-renders and faster lists)**\n\nWhen passing callback functions to list items, create a single instance of the\ncallback at the root of the list. Items should then call it with a unique\nidentifier.\n\n**Incorrect (creates a new callback on each render):**\n\n```typescript\nreturn (\n  <LegendList\n    renderItem={({ item }) => {\n      // bad: creates a new callback on each render\n      const onPress = () => handlePress(item.id)\n      return <Item key={item.id} item={item} onPress={onPress} />\n    }}\n  />\n)\n```\n\n**Correct (a single function instance passed to each item):**\n\n```typescript\nconst onPress = useCallback(() => handlePress(item.id), [handlePress, item.id])\n\nreturn (\n  <LegendList\n    renderItem={({ item }) => (\n      <Item key={item.id} item={item} onPress={onPress} />\n    )}\n  />\n)\n```\n\nReference: [Link to documentation or resource](https://example.com)\n"
  },
  {
    "path": "skills/react-native-skills/rules/list-performance-function-references.md",
    "content": "---\ntitle: Optimize List Performance with Stable Object References\nimpact: CRITICAL\nimpactDescription: virtualization relies on reference stability\ntags: lists, performance, flatlist, virtualization\n---\n\n## Optimize List Performance with Stable Object References\n\nDon't map or filter data before passing to virtualized lists. Virtualization\nrelies on object reference stability to know what changed—new references cause\nfull re-renders of all visible items. Attempt to prevent frequent renders at the\nlist-parent level.\n\nWhere needed, use context selectors within list items.\n\n**Incorrect (creates new object references on every keystroke):**\n\n```tsx\nfunction DomainSearch() {\n  const { keyword, setKeyword } = useKeywordZustandState()\n  const { data: tlds } = useTlds()\n\n  // Bad: creates new objects on every render, reparenting the entire list on every keystroke\n  const domains = tlds.map((tld) => ({\n    domain: `${keyword}.${tld.name}`,\n    tld: tld.name,\n    price: tld.price,\n  }))\n\n  return (\n    <>\n      <TextInput value={keyword} onChangeText={setKeyword} />\n      <LegendList\n        data={domains}\n        renderItem={({ item }) => <DomainItem item={item} keyword={keyword} />}\n      />\n    </>\n  )\n}\n```\n\n**Correct (stable references, transform inside items):**\n\n```tsx\nconst renderItem = ({ item }) => <DomainItem tld={item} />\n\nfunction DomainSearch() {\n  const { data: tlds } = useTlds()\n\n  return (\n    <LegendList\n      // good: as long as the data is stable, LegendList will not re-render the entire list\n      data={tlds}\n      renderItem={renderItem}\n    />\n  )\n}\n\nfunction DomainItem({ tld }: { tld: Tld }) {\n  // good: transform within items, and don't pass the dynamic data as a prop\n  // good: use a selector function from zustand to receive a stable string back\n  const domain = useKeywordZustandState((s) => s.keyword + '.' + tld.name)\n  return <Text>{domain}</Text>\n}\n```\n\n**Updating parent array reference:**\n\nCreating a new array instance can be okay, as long as its inner object\nreferences are stable. For instance, if you sort a list of objects:\n\n```tsx\n// good: creates a new array instance without mutating the inner objects\n// good: parent array reference is unaffected by typing and updating \"keyword\"\nconst sortedTlds = tlds.toSorted((a, b) => a.name.localeCompare(b.name))\n\nreturn <LegendList data={sortedTlds} renderItem={renderItem} />\n```\n\nEven though this creates a new array instance `sortedTlds`, the inner object\nreferences are stable.\n\n**With zustand for dynamic data (avoids parent re-renders):**\n\n```tsx\nconst useSearchStore = create<{ keyword: string }>(() => ({ keyword: '' }))\n\nfunction DomainSearch() {\n  const { data: tlds } = useTlds()\n\n  return (\n    <>\n      <SearchInput />\n      <LegendList\n        data={tlds}\n        // if you aren't using React Compiler, wrap renderItem with useCallback\n        renderItem={({ item }) => <DomainItem tld={item} />}\n      />\n    </>\n  )\n}\n\nfunction DomainItem({ tld }: { tld: Tld }) {\n  // Select only what you need—component only re-renders when keyword changes\n  const keyword = useSearchStore((s) => s.keyword)\n  const domain = `${keyword}.${tld.name}`\n  return <Text>{domain}</Text>\n}\n```\n\nVirtualization can now skip items that haven't changed when typing. Only visible\nitems (~20) re-render on keystroke, rather than the parent.\n\n**Deriving state within list items based on parent data (avoids parent\nre-renders):**\n\nFor components where the data is conditional based on the parent state, this\npattern is even more important. For example, if you are checking if an item is\nfavorited, toggling favorites only re-renders one component if the item itself\nis in charge of accessing the state rather than the parent:\n\n```tsx\nfunction DomainItemFavoriteButton({ tld }: { tld: Tld }) {\n  const isFavorited = useFavoritesStore((s) => s.favorites.has(tld.id))\n  return <TldFavoriteButton isFavorited={isFavorited} />\n}\n```\n\nNote: if you're using the React Compiler, you can read React Context values\ndirectly within list items. Although this is slightly slower than using a\nZustand selector in most cases, the effect may be negligible.\n"
  },
  {
    "path": "skills/react-native-skills/rules/list-performance-images.md",
    "content": "---\ntitle: Use Compressed Images in Lists\nimpact: HIGH\nimpactDescription: faster load times, less memory\ntags: lists, images, performance, optimization\n---\n\n## Use Compressed Images in Lists\n\nAlways load compressed, appropriately-sized images in lists. Full-resolution\nimages consume excessive memory and cause scroll jank. Request thumbnails from\nyour server or use an image CDN with resize parameters.\n\n**Incorrect (full-resolution images):**\n\n```tsx\nfunction ProductItem({ product }: { product: Product }) {\n  return (\n    <View>\n      {/* 4000x3000 image loaded for a 100x100 thumbnail */}\n      <Image\n        source={{ uri: product.imageUrl }}\n        style={{ width: 100, height: 100 }}\n      />\n      <Text>{product.name}</Text>\n    </View>\n  )\n}\n```\n\n**Correct (request appropriately-sized image):**\n\n```tsx\nfunction ProductItem({ product }: { product: Product }) {\n  // Request a 200x200 image (2x for retina)\n  const thumbnailUrl = `${product.imageUrl}?w=200&h=200&fit=cover`\n\n  return (\n    <View>\n      <Image\n        source={{ uri: thumbnailUrl }}\n        style={{ width: 100, height: 100 }}\n        contentFit='cover'\n      />\n      <Text>{product.name}</Text>\n    </View>\n  )\n}\n```\n\nUse an optimized image component with built-in caching and placeholder support,\nsuch as `expo-image` or `SolitoImage` (which uses `expo-image` under the hood).\nRequest images at 2x the display size for retina screens.\n"
  },
  {
    "path": "skills/react-native-skills/rules/list-performance-inline-objects.md",
    "content": "---\ntitle: Avoid Inline Objects in renderItem\nimpact: HIGH\nimpactDescription: prevents unnecessary re-renders of memoized list items\ntags: lists, performance, flatlist, virtualization, memo\n---\n\n## Avoid Inline Objects in renderItem\n\nDon't create new objects inside `renderItem` to pass as props. Inline objects\ncreate new references on every render, breaking memoization. Pass primitive\nvalues directly from `item` instead.\n\n**Incorrect (inline object breaks memoization):**\n\n```tsx\nfunction UserList({ users }: { users: User[] }) {\n  return (\n    <LegendList\n      data={users}\n      renderItem={({ item }) => (\n        <UserRow\n          // Bad: new object on every render\n          user={{ id: item.id, name: item.name, avatar: item.avatar }}\n        />\n      )}\n    />\n  )\n}\n```\n\n**Incorrect (inline style object):**\n\n```tsx\nrenderItem={({ item }) => (\n  <UserRow\n    name={item.name}\n    // Bad: new style object on every render\n    style={{ backgroundColor: item.isActive ? 'green' : 'gray' }}\n  />\n)}\n```\n\n**Correct (pass item directly or primitives):**\n\n```tsx\nfunction UserList({ users }: { users: User[] }) {\n  return (\n    <LegendList\n      data={users}\n      renderItem={({ item }) => (\n        // Good: pass the item directly\n        <UserRow user={item} />\n      )}\n    />\n  )\n}\n```\n\n**Correct (pass primitives, derive inside child):**\n\n```tsx\nrenderItem={({ item }) => (\n  <UserRow\n    id={item.id}\n    name={item.name}\n    isActive={item.isActive}\n  />\n)}\n\nconst UserRow = memo(function UserRow({ id, name, isActive }: Props) {\n  // Good: derive style inside memoized component\n  const backgroundColor = isActive ? 'green' : 'gray'\n  return <View style={[styles.row, { backgroundColor }]}>{/* ... */}</View>\n})\n```\n\n**Correct (hoist static styles in module scope):**\n\n```tsx\nconst activeStyle = { backgroundColor: 'green' }\nconst inactiveStyle = { backgroundColor: 'gray' }\n\nrenderItem={({ item }) => (\n  <UserRow\n    name={item.name}\n    // Good: stable references\n    style={item.isActive ? activeStyle : inactiveStyle}\n  />\n)}\n```\n\nPassing primitives or stable references allows `memo()` to skip re-renders when\nthe actual values haven't changed.\n\n**Note:** If you have the React Compiler enabled, it handles memoization\nautomatically and these manual optimizations become less critical.\n"
  },
  {
    "path": "skills/react-native-skills/rules/list-performance-item-expensive.md",
    "content": "---\ntitle: Keep List Items Lightweight\nimpact: HIGH\nimpactDescription: reduces render time for visible items during scroll\ntags: lists, performance, virtualization, hooks\n---\n\n## Keep List Items Lightweight\n\nList items should be as inexpensive as possible to render. Minimize hooks, avoid\nqueries, and limit React Context access. Virtualized lists render many items\nduring scroll—expensive items cause jank.\n\n**Incorrect (heavy list item):**\n\n```tsx\nfunction ProductRow({ id }: { id: string }) {\n  // Bad: query inside list item\n  const { data: product } = useQuery(['product', id], () => fetchProduct(id))\n  // Bad: multiple context accesses\n  const theme = useContext(ThemeContext)\n  const user = useContext(UserContext)\n  const cart = useContext(CartContext)\n  // Bad: expensive computation\n  const recommendations = useMemo(\n    () => computeRecommendations(product),\n    [product]\n  )\n\n  return <View>{/* ... */}</View>\n}\n```\n\n**Correct (lightweight list item):**\n\n```tsx\nfunction ProductRow({ name, price, imageUrl }: Props) {\n  // Good: receives only primitives, minimal hooks\n  return (\n    <View>\n      <Image source={{ uri: imageUrl }} />\n      <Text>{name}</Text>\n      <Text>{price}</Text>\n    </View>\n  )\n}\n```\n\n**Move data fetching to parent:**\n\n```tsx\n// Parent fetches all data once\nfunction ProductList() {\n  const { data: products } = useQuery(['products'], fetchProducts)\n\n  return (\n    <LegendList\n      data={products}\n      renderItem={({ item }) => (\n        <ProductRow name={item.name} price={item.price} imageUrl={item.image} />\n      )}\n    />\n  )\n}\n```\n\n**For shared values, use Zustand selectors instead of Context:**\n\n```tsx\n// Incorrect: Context causes re-render when any cart value changes\nfunction ProductRow({ id, name }: Props) {\n  const { items } = useContext(CartContext)\n  const inCart = items.includes(id)\n  // ...\n}\n\n// Correct: Zustand selector only re-renders when this specific value changes\nfunction ProductRow({ id, name }: Props) {\n  // use Set.has (created once at the root) instead of Array.includes()\n  const inCart = useCartStore((s) => s.items.has(id))\n  // ...\n}\n```\n\n**Guidelines for list items:**\n\n- No queries or data fetching\n- No expensive computations (move to parent or memoize at parent level)\n- Prefer Zustand selectors over React Context\n- Minimize useState/useEffect hooks\n- Pass pre-computed values as props\n\nThe goal: list items should be simple rendering functions that take props and\nreturn JSX.\n"
  },
  {
    "path": "skills/react-native-skills/rules/list-performance-item-memo.md",
    "content": "---\ntitle: Pass Primitives to List Items for Memoization\nimpact: HIGH\nimpactDescription: enables effective memo() comparison\ntags: lists, performance, memo, primitives\n---\n\n## Pass Primitives to List Items for Memoization\n\nWhen possible, pass only primitive values (strings, numbers, booleans) as props\nto list item components. Primitives enable shallow comparison in `memo()` to\nwork correctly, skipping re-renders when values haven't changed.\n\n**Incorrect (object prop requires deep comparison):**\n\n```tsx\ntype User = { id: string; name: string; email: string; avatar: string }\n\nconst UserRow = memo(function UserRow({ user }: { user: User }) {\n  // memo() compares user by reference, not value\n  // If parent creates new user object, this re-renders even if data is same\n  return <Text>{user.name}</Text>\n})\n\nrenderItem={({ item }) => <UserRow user={item} />}\n```\n\nThis can still be optimized, but it is harder to memoize properly.\n\n**Correct (primitive props enable shallow comparison):**\n\n```tsx\nconst UserRow = memo(function UserRow({\n  id,\n  name,\n  email,\n}: {\n  id: string\n  name: string\n  email: string\n}) {\n  // memo() compares each primitive directly\n  // Re-renders only if id, name, or email actually changed\n  return <Text>{name}</Text>\n})\n\nrenderItem={({ item }) => (\n  <UserRow id={item.id} name={item.name} email={item.email} />\n)}\n```\n\n**Pass only what you need:**\n\n```tsx\n// Incorrect: passing entire item when you only need name\n<UserRow user={item} />\n\n// Correct: pass only the fields the component uses\n<UserRow name={item.name} avatarUrl={item.avatar} />\n```\n\n**For callbacks, hoist or use item ID:**\n\n```tsx\n// Incorrect: inline function creates new reference\n<UserRow name={item.name} onPress={() => handlePress(item.id)} />\n\n// Correct: pass ID, handle in child\n<UserRow id={item.id} name={item.name} />\n\nconst UserRow = memo(function UserRow({ id, name }: Props) {\n  const handlePress = useCallback(() => {\n    // use id here\n  }, [id])\n  return <Pressable onPress={handlePress}><Text>{name}</Text></Pressable>\n})\n```\n\nPrimitive props make memoization predictable and effective.\n\n**Note:** If you have the React Compiler enabled, you do not need to use\n`memo()` or `useCallback()`, but the object references still apply.\n"
  },
  {
    "path": "skills/react-native-skills/rules/list-performance-item-types.md",
    "content": "---\ntitle: Use Item Types for Heterogeneous Lists\nimpact: HIGH\nimpactDescription: efficient recycling, less layout thrashing\ntags: list, performance, recycling, heterogeneous, LegendList\n---\n\n## Use Item Types for Heterogeneous Lists\n\nWhen a list has different item layouts (messages, images, headers, etc.), use a\n`type` field on each item and provide `getItemType` to the list. This puts items\ninto separate recycling pools so a message component never gets recycled into an\nimage component.\n\n**Incorrect (single component with conditionals):**\n\n```tsx\ntype Item = { id: string; text?: string; imageUrl?: string; isHeader?: boolean }\n\nfunction ListItem({ item }: { item: Item }) {\n  if (item.isHeader) {\n    return <HeaderItem title={item.text} />\n  }\n  if (item.imageUrl) {\n    return <ImageItem url={item.imageUrl} />\n  }\n  return <MessageItem text={item.text} />\n}\n\nfunction Feed({ items }: { items: Item[] }) {\n  return (\n    <LegendList\n      data={items}\n      renderItem={({ item }) => <ListItem item={item} />}\n      recycleItems\n    />\n  )\n}\n```\n\n**Correct (typed items with separate components):**\n\n```tsx\ntype HeaderItem = { id: string; type: 'header'; title: string }\ntype MessageItem = { id: string; type: 'message'; text: string }\ntype ImageItem = { id: string; type: 'image'; url: string }\ntype FeedItem = HeaderItem | MessageItem | ImageItem\n\nfunction Feed({ items }: { items: FeedItem[] }) {\n  return (\n    <LegendList\n      data={items}\n      keyExtractor={(item) => item.id}\n      getItemType={(item) => item.type}\n      renderItem={({ item }) => {\n        switch (item.type) {\n          case 'header':\n            return <SectionHeader title={item.title} />\n          case 'message':\n            return <MessageRow text={item.text} />\n          case 'image':\n            return <ImageRow url={item.url} />\n        }\n      }}\n      recycleItems\n    />\n  )\n}\n```\n\n**Why this matters:**\n\n- **Recycling efficiency**: Items with the same type share a recycling pool\n- **No layout thrashing**: A header never recycles into an image cell\n- **Type safety**: TypeScript can narrow the item type in each branch\n- **Better size estimation**: Use `getEstimatedItemSize` with `itemType` for\n  accurate estimates per type\n\n```tsx\n<LegendList\n  data={items}\n  keyExtractor={(item) => item.id}\n  getItemType={(item) => item.type}\n  getEstimatedItemSize={(index, item, itemType) => {\n    switch (itemType) {\n      case 'header':\n        return 48\n      case 'message':\n        return 72\n      case 'image':\n        return 300\n      default:\n        return 72\n    }\n  }}\n  renderItem={({ item }) => {\n    /* ... */\n  }}\n  recycleItems\n/>\n```\n\nReference:\n[LegendList getItemType](https://legendapp.com/open-source/list/api/props/#getitemtype-v2)\n"
  },
  {
    "path": "skills/react-native-skills/rules/list-performance-virtualize.md",
    "content": "---\ntitle: Use a List Virtualizer for Any List\nimpact: HIGH\nimpactDescription: reduced memory, faster mounts\ntags: lists, performance, virtualization, scrollview\n---\n\n## Use a List Virtualizer for Any List\n\nUse a list virtualizer like LegendList or FlashList instead of ScrollView with\nmapped children—even for short lists. Virtualizers only render visible items,\nreducing memory usage and mount time. ScrollView renders all children upfront,\nwhich gets expensive quickly.\n\n**Incorrect (ScrollView renders all items at once):**\n\n```tsx\nfunction Feed({ items }: { items: Item[] }) {\n  return (\n    <ScrollView>\n      {items.map((item) => (\n        <ItemCard key={item.id} item={item} />\n      ))}\n    </ScrollView>\n  )\n}\n// 50 items = 50 components mounted, even if only 10 visible\n```\n\n**Correct (virtualizer renders only visible items):**\n\n```tsx\nimport { LegendList } from '@legendapp/list'\n\nfunction Feed({ items }: { items: Item[] }) {\n  return (\n    <LegendList\n      data={items}\n      // if you aren't using React Compiler, wrap these with useCallback\n      renderItem={({ item }) => <ItemCard item={item} />}\n      keyExtractor={(item) => item.id}\n      estimatedItemSize={80}\n    />\n  )\n}\n// Only ~10-15 visible items mounted at a time\n```\n\n**Alternative (FlashList):**\n\n```tsx\nimport { FlashList } from '@shopify/flash-list'\n\nfunction Feed({ items }: { items: Item[] }) {\n  return (\n    <FlashList\n      data={items}\n      // if you aren't using React Compiler, wrap these with useCallback\n      renderItem={({ item }) => <ItemCard item={item} />}\n      keyExtractor={(item) => item.id}\n    />\n  )\n}\n```\n\nBenefits apply to any screen with scrollable content—profiles, settings, feeds,\nsearch results. Default to virtualization.\n"
  },
  {
    "path": "skills/react-native-skills/rules/monorepo-native-deps-in-app.md",
    "content": "---\ntitle: Install Native Dependencies in App Directory\nimpact: CRITICAL\nimpactDescription: required for autolinking to work\ntags: monorepo, native, autolinking, installation\n---\n\n## Install Native Dependencies in App Directory\n\nIn a monorepo, packages with native code must be installed in the native app's\ndirectory directly. Autolinking only scans the app's `node_modules`—it won't\nfind native dependencies installed in other packages.\n\n**Incorrect (native dep in shared package only):**\n\n```\npackages/\n  ui/\n    package.json  # has react-native-reanimated\n  app/\n    package.json  # missing react-native-reanimated\n```\n\nAutolinking fails—native code not linked.\n\n**Correct (native dep in app directory):**\n\n```\npackages/\n  ui/\n    package.json  # has react-native-reanimated\n  app/\n    package.json  # also has react-native-reanimated\n```\n\n```json\n// packages/app/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"3.16.1\"\n  }\n}\n```\n\nEven if the shared package uses the native dependency, the app must also list it\nfor autolinking to detect and link the native code.\n"
  },
  {
    "path": "skills/react-native-skills/rules/monorepo-single-dependency-versions.md",
    "content": "---\ntitle: Use Single Dependency Versions Across Monorepo\nimpact: MEDIUM\nimpactDescription: avoids duplicate bundles, version conflicts\ntags: monorepo, dependencies, installation\n---\n\n## Use Single Dependency Versions Across Monorepo\n\nUse a single version of each dependency across all packages in your monorepo.\nPrefer exact versions over ranges. Multiple versions cause duplicate code in\nbundles, runtime conflicts, and inconsistent behavior across packages.\n\nUse a tool like syncpack to enforce this. As a last resort, use yarn resolutions\nor npm overrides.\n\n**Incorrect (version ranges, multiple versions):**\n\n```json\n// packages/app/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"^3.0.0\"\n  }\n}\n\n// packages/ui/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"^3.5.0\"\n  }\n}\n```\n\n**Correct (exact versions, single source of truth):**\n\n```json\n// package.json (root)\n{\n  \"pnpm\": {\n    \"overrides\": {\n      \"react-native-reanimated\": \"3.16.1\"\n    }\n  }\n}\n\n// packages/app/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"3.16.1\"\n  }\n}\n\n// packages/ui/package.json\n{\n  \"dependencies\": {\n    \"react-native-reanimated\": \"3.16.1\"\n  }\n}\n```\n\nUse your package manager's override/resolution feature to enforce versions at\nthe root. When adding dependencies, specify exact versions without `^` or `~`.\n"
  },
  {
    "path": "skills/react-native-skills/rules/navigation-native-navigators.md",
    "content": "---\ntitle: Use Native Navigators for Navigation\nimpact: HIGH\nimpactDescription: native performance, platform-appropriate UI\ntags: navigation, react-navigation, expo-router, native-stack, tabs\n---\n\n## Use Native Navigators for Navigation\n\nAlways use native navigators instead of JS-based ones. Native navigators use\nplatform APIs (UINavigationController on iOS, Fragment on Android) for better\nperformance and native behavior.\n\n**For stacks:** Use `@react-navigation/native-stack` or expo-router's default\nstack (which uses native-stack). Avoid `@react-navigation/stack`.\n\n**For tabs:** Use `react-native-bottom-tabs` (native) or expo-router's native\ntabs. Avoid `@react-navigation/bottom-tabs` when native feel matters.\n\n### Stack Navigation\n\n**Incorrect (JS stack navigator):**\n\n```tsx\nimport { createStackNavigator } from '@react-navigation/stack'\n\nconst Stack = createStackNavigator()\n\nfunction App() {\n  return (\n    <Stack.Navigator>\n      <Stack.Screen name='Home' component={HomeScreen} />\n      <Stack.Screen name='Details' component={DetailsScreen} />\n    </Stack.Navigator>\n  )\n}\n```\n\n**Correct (native stack with react-navigation):**\n\n```tsx\nimport { createNativeStackNavigator } from '@react-navigation/native-stack'\n\nconst Stack = createNativeStackNavigator()\n\nfunction App() {\n  return (\n    <Stack.Navigator>\n      <Stack.Screen name='Home' component={HomeScreen} />\n      <Stack.Screen name='Details' component={DetailsScreen} />\n    </Stack.Navigator>\n  )\n}\n```\n\n**Correct (expo-router uses native stack by default):**\n\n```tsx\n// app/_layout.tsx\nimport { Stack } from 'expo-router'\n\nexport default function Layout() {\n  return <Stack />\n}\n```\n\n### Tab Navigation\n\n**Incorrect (JS bottom tabs):**\n\n```tsx\nimport { createBottomTabNavigator } from '@react-navigation/bottom-tabs'\n\nconst Tab = createBottomTabNavigator()\n\nfunction App() {\n  return (\n    <Tab.Navigator>\n      <Tab.Screen name='Home' component={HomeScreen} />\n      <Tab.Screen name='Settings' component={SettingsScreen} />\n    </Tab.Navigator>\n  )\n}\n```\n\n**Correct (native bottom tabs with react-navigation):**\n\n```tsx\nimport { createNativeBottomTabNavigator } from '@bottom-tabs/react-navigation'\n\nconst Tab = createNativeBottomTabNavigator()\n\nfunction App() {\n  return (\n    <Tab.Navigator>\n      <Tab.Screen\n        name='Home'\n        component={HomeScreen}\n        options={{\n          tabBarIcon: () => ({ sfSymbol: 'house' }),\n        }}\n      />\n      <Tab.Screen\n        name='Settings'\n        component={SettingsScreen}\n        options={{\n          tabBarIcon: () => ({ sfSymbol: 'gear' }),\n        }}\n      />\n    </Tab.Navigator>\n  )\n}\n```\n\n**Correct (expo-router native tabs):**\n\n```tsx\n// app/(tabs)/_layout.tsx\nimport { NativeTabs } from 'expo-router/unstable-native-tabs'\n\nexport default function TabLayout() {\n  return (\n    <NativeTabs>\n      <NativeTabs.Trigger name='index'>\n        <NativeTabs.Trigger.Label>Home</NativeTabs.Trigger.Label>\n        <NativeTabs.Trigger.Icon sf='house.fill' md='home' />\n      </NativeTabs.Trigger>\n      <NativeTabs.Trigger name='settings'>\n        <NativeTabs.Trigger.Label>Settings</NativeTabs.Trigger.Label>\n        <NativeTabs.Trigger.Icon sf='gear' md='settings' />\n      </NativeTabs.Trigger>\n    </NativeTabs>\n  )\n}\n```\n\nOn iOS, native tabs automatically enable `contentInsetAdjustmentBehavior` on the\nfirst `ScrollView` at the root of each tab screen, so content scrolls correctly\nbehind the translucent tab bar. If you need to disable this, use\n`disableAutomaticContentInsets` on the trigger.\n\n### Prefer Native Header Options Over Custom Components\n\n**Incorrect (custom header component):**\n\n```tsx\n<Stack.Screen\n  name='Profile'\n  component={ProfileScreen}\n  options={{\n    header: () => <CustomHeader title='Profile' />,\n  }}\n/>\n```\n\n**Correct (native header options):**\n\n```tsx\n<Stack.Screen\n  name='Profile'\n  component={ProfileScreen}\n  options={{\n    title: 'Profile',\n    headerLargeTitleEnabled: true,\n    headerSearchBarOptions: {\n      placeholder: 'Search',\n    },\n  }}\n/>\n```\n\nNative headers support iOS large titles, search bars, blur effects, and proper\nsafe area handling automatically.\n\n### Why Native Navigators\n\n- **Performance**: Native transitions and gestures run on the UI thread\n- **Platform behavior**: Automatic iOS large titles, Android material design\n- **System integration**: Scroll-to-top on tab tap, PiP avoidance, proper safe\n  areas\n- **Accessibility**: Platform accessibility features work automatically\n\nReference:\n\n- [React Navigation Native Stack](https://reactnavigation.org/docs/native-stack-navigator)\n- [React Native Bottom Tabs with React Navigation](https://oss.callstack.com/react-native-bottom-tabs/docs/guides/usage-with-react-navigation)\n- [React Native Bottom Tabs with Expo Router](https://oss.callstack.com/react-native-bottom-tabs/docs/guides/usage-with-expo-router)\n- [Expo Router Native Tabs](https://docs.expo.dev/router/advanced/native-tabs)\n"
  },
  {
    "path": "skills/react-native-skills/rules/react-compiler-destructure-functions.md",
    "content": "---\ntitle: Destructure Functions Early in Render (React Compiler)\nimpact: HIGH\nimpactDescription: stable references, fewer re-renders\ntags: rerender, hooks, performance, react-compiler\n---\n\n## Destructure Functions Early in Render\n\nThis rule is only applicable if you are using the React Compiler.\n\nDestructure functions from hooks at the top of render scope. Never dot into\nobjects to call functions. Destructured functions are stable references; dotting\ncreates new references and breaks memoization.\n\n**Incorrect (dotting into object):**\n\n```tsx\nimport { useRouter } from 'expo-router'\n\nfunction SaveButton(props) {\n  const router = useRouter()\n\n  // bad: react-compiler will key the cache on \"props\" and \"router\", which are objects that change each render\n  const handlePress = () => {\n    props.onSave()\n    router.push('/success') // unstable reference\n  }\n\n  return <Button onPress={handlePress}>Save</Button>\n}\n```\n\n**Correct (destructure early):**\n\n```tsx\nimport { useRouter } from 'expo-router'\n\nfunction SaveButton({ onSave }) {\n  const { push } = useRouter()\n\n  // good: react-compiler will key on push and onSave\n  const handlePress = () => {\n    onSave()\n    push('/success') // stable reference\n  }\n\n  return <Button onPress={handlePress}>Save</Button>\n}\n```\n"
  },
  {
    "path": "skills/react-native-skills/rules/react-compiler-reanimated-shared-values.md",
    "content": "---\ntitle: Use .get() and .set() for Reanimated Shared Values (not .value)\nimpact: LOW\nimpactDescription: required for React Compiler compatibility\ntags: reanimated, react-compiler, shared-values\n---\n\n## Use .get() and .set() for Shared Values with React Compiler\n\nWith React Compiler enabled, use `.get()` and `.set()` instead of reading or\nwriting `.value` directly on Reanimated shared values. The compiler can't track\nproperty access—explicit methods ensure correct behavior.\n\n**Incorrect (breaks with React Compiler):**\n\n```tsx\nimport { useSharedValue } from 'react-native-reanimated'\n\nfunction Counter() {\n  const count = useSharedValue(0)\n\n  const increment = () => {\n    count.value = count.value + 1 // opts out of react compiler\n  }\n\n  return <Button onPress={increment} title={`Count: ${count.value}`} />\n}\n```\n\n**Correct (React Compiler compatible):**\n\n```tsx\nimport { useSharedValue } from 'react-native-reanimated'\n\nfunction Counter() {\n  const count = useSharedValue(0)\n\n  const increment = () => {\n    count.set(count.get() + 1)\n  }\n\n  return <Button onPress={increment} title={`Count: ${count.get()}`} />\n}\n```\n\nSee the\n[Reanimated docs](https://docs.swmansion.com/react-native-reanimated/docs/core/useSharedValue/#react-compiler-support)\nfor more.\n"
  },
  {
    "path": "skills/react-native-skills/rules/react-state-dispatcher.md",
    "content": "---\ntitle: useState Dispatch updaters for State That Depends on Current Value\nimpact: MEDIUM\nimpactDescription: avoids stale closures, prevents unnecessary re-renders\ntags: state, hooks, useState, callbacks\n---\n\n## Use Dispatch Updaters for State That Depends on Current Value\n\nWhen the next state depends on the current state, use a dispatch updater\n(`setState(prev => ...)`) instead of reading the state variable directly in a\ncallback. This avoids stale closures and ensures you're comparing against the\nlatest value.\n\n**Incorrect (reads state directly):**\n\n```tsx\nconst [size, setSize] = useState<Size | undefined>(undefined)\n\nconst onLayout = (e: LayoutChangeEvent) => {\n  const { width, height } = e.nativeEvent.layout\n  // size may be stale in this closure\n  if (size?.width !== width || size?.height !== height) {\n    setSize({ width, height })\n  }\n}\n```\n\n**Correct (dispatch updater):**\n\n```tsx\nconst [size, setSize] = useState<Size | undefined>(undefined)\n\nconst onLayout = (e: LayoutChangeEvent) => {\n  const { width, height } = e.nativeEvent.layout\n  setSize((prev) => {\n    if (prev?.width === width && prev?.height === height) return prev\n    return { width, height }\n  })\n}\n```\n\nReturning the previous value from the updater skips the re-render.\n\nFor primitive states, you don't need to compare values before firing a\nre-render.\n\n**Incorrect (unnecessary comparison for primitive state):**\n\n```tsx\nconst [size, setSize] = useState<Size | undefined>(undefined)\n\nconst onLayout = (e: LayoutChangeEvent) => {\n  const { width, height } = e.nativeEvent.layout\n  setSize((prev) => (prev === width ? prev : width))\n}\n```\n\n**Correct (sets primitive state directly):**\n\n```tsx\nconst [size, setSize] = useState<Size | undefined>(undefined)\n\nconst onLayout = (e: LayoutChangeEvent) => {\n  const { width, height } = e.nativeEvent.layout\n  setSize(width)\n}\n```\n\nHowever, if the next state depends on the current state, you should still use a\ndispatch updater.\n\n**Incorrect (reads state directly from the callback):**\n\n```tsx\nconst [count, setCount] = useState(0)\n\nconst onTap = () => {\n  setCount(count + 1)\n}\n```\n\n**Correct (dispatch updater):**\n\n```tsx\nconst [count, setCount] = useState(0)\n\nconst onTap = () => {\n  setCount((prev) => prev + 1)\n}\n```\n"
  },
  {
    "path": "skills/react-native-skills/rules/react-state-fallback.md",
    "content": "---\ntitle: Use fallback state instead of initialState\nimpact: MEDIUM\nimpactDescription: reactive fallbacks without syncing\ntags: state, hooks, derived-state, props, initialState\n---\n\n## Use fallback state instead of initialState\n\nUse `undefined` as initial state and nullish coalescing (`??`) to fall back to\nparent or server values. State represents user intent only—`undefined` means\n\"user hasn't chosen yet.\" This enables reactive fallbacks that update when the\nsource changes, not just on initial render.\n\n**Incorrect (syncs state, loses reactivity):**\n\n```tsx\ntype Props = { fallbackEnabled: boolean }\n\nfunction Toggle({ fallbackEnabled }: Props) {\n  const [enabled, setEnabled] = useState(defaultEnabled)\n  // If fallbackEnabled changes, state is stale\n  // State mixes user intent with default value\n\n  return <Switch value={enabled} onValueChange={setEnabled} />\n}\n```\n\n**Correct (state is user intent, reactive fallback):**\n\n```tsx\ntype Props = { fallbackEnabled: boolean }\n\nfunction Toggle({ fallbackEnabled }: Props) {\n  const [_enabled, setEnabled] = useState<boolean | undefined>(undefined)\n  const enabled = _enabled ?? defaultEnabled\n  // undefined = user hasn't touched it, falls back to prop\n  // If defaultEnabled changes, component reflects it\n  // Once user interacts, their choice persists\n\n  return <Switch value={enabled} onValueChange={setEnabled} />\n}\n```\n\n**With server data:**\n\n```tsx\nfunction ProfileForm({ data }: { data: User }) {\n  const [_theme, setTheme] = useState<string | undefined>(undefined)\n  const theme = _theme ?? data.theme\n  // Shows server value until user overrides\n  // Server refetch updates the fallback automatically\n\n  return <ThemePicker value={theme} onChange={setTheme} />\n}\n```\n"
  },
  {
    "path": "skills/react-native-skills/rules/react-state-minimize.md",
    "content": "---\ntitle: Minimize State Variables and Derive Values\nimpact: MEDIUM\nimpactDescription: fewer re-renders, less state drift\ntags: state, derived-state, hooks, optimization\n---\n\n## Minimize State Variables and Derive Values\n\nUse the fewest state variables possible. If a value can be computed from existing state or props, derive it during render instead of storing it in state. Redundant state causes unnecessary re-renders and can drift out of sync.\n\n**Incorrect (redundant state):**\n\n```tsx\nfunction Cart({ items }: { items: Item[] }) {\n  const [total, setTotal] = useState(0)\n  const [itemCount, setItemCount] = useState(0)\n\n  useEffect(() => {\n    setTotal(items.reduce((sum, item) => sum + item.price, 0))\n    setItemCount(items.length)\n  }, [items])\n\n  return (\n    <View>\n      <Text>{itemCount} items</Text>\n      <Text>Total: ${total}</Text>\n    </View>\n  )\n}\n```\n\n**Correct (derived values):**\n\n```tsx\nfunction Cart({ items }: { items: Item[] }) {\n  const total = items.reduce((sum, item) => sum + item.price, 0)\n  const itemCount = items.length\n\n  return (\n    <View>\n      <Text>{itemCount} items</Text>\n      <Text>Total: ${total}</Text>\n    </View>\n  )\n}\n```\n\n**Another example:**\n\n```tsx\n// Incorrect: storing both firstName, lastName, AND fullName\nconst [firstName, setFirstName] = useState('')\nconst [lastName, setLastName] = useState('')\nconst [fullName, setFullName] = useState('')\n\n// Correct: derive fullName\nconst [firstName, setFirstName] = useState('')\nconst [lastName, setLastName] = useState('')\nconst fullName = `${firstName} ${lastName}`\n```\n\nState should be the minimal source of truth. Everything else is derived.\n\nReference: [Choosing the State Structure](https://react.dev/learn/choosing-the-state-structure)\n"
  },
  {
    "path": "skills/react-native-skills/rules/rendering-no-falsy-and.md",
    "content": "---\ntitle: Never Use && with Potentially Falsy Values\nimpact: CRITICAL\nimpactDescription: prevents production crash\ntags: rendering, conditional, jsx, crash\n---\n\n## Never Use && with Potentially Falsy Values\n\nNever use `{value && <Component />}` when `value` could be an empty string or\n`0`. These are falsy but JSX-renderable—React Native will try to render them as\ntext outside a `<Text>` component, causing a hard crash in production.\n\n**Incorrect (crashes if count is 0 or name is \"\"):**\n\n```tsx\nfunction Profile({ name, count }: { name: string; count: number }) {\n  return (\n    <View>\n      {name && <Text>{name}</Text>}\n      {count && <Text>{count} items</Text>}\n    </View>\n  )\n}\n// If name=\"\" or count=0, renders the falsy value → crash\n```\n\n**Correct (ternary with null):**\n\n```tsx\nfunction Profile({ name, count }: { name: string; count: number }) {\n  return (\n    <View>\n      {name ? <Text>{name}</Text> : null}\n      {count ? <Text>{count} items</Text> : null}\n    </View>\n  )\n}\n```\n\n**Correct (explicit boolean coercion):**\n\n```tsx\nfunction Profile({ name, count }: { name: string; count: number }) {\n  return (\n    <View>\n      {!!name && <Text>{name}</Text>}\n      {!!count && <Text>{count} items</Text>}\n    </View>\n  )\n}\n```\n\n**Best (early return):**\n\n```tsx\nfunction Profile({ name, count }: { name: string; count: number }) {\n  if (!name) return null\n\n  return (\n    <View>\n      <Text>{name}</Text>\n      {count > 0 ? <Text>{count} items</Text> : null}\n    </View>\n  )\n}\n```\n\nEarly returns are clearest. When using conditionals inline, prefer ternary or\nexplicit boolean checks.\n\n**Lint rule:** Enable `react/jsx-no-leaked-render` from\n[eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/jsx-no-leaked-render.md)\nto catch this automatically.\n"
  },
  {
    "path": "skills/react-native-skills/rules/rendering-text-in-text-component.md",
    "content": "---\ntitle: Wrap Strings in Text Components\nimpact: CRITICAL\nimpactDescription: prevents runtime crash\ntags: rendering, text, core\n---\n\n## Wrap Strings in Text Components\n\nStrings must be rendered inside `<Text>`. React Native crashes if a string is a\ndirect child of `<View>`.\n\n**Incorrect (crashes):**\n\n```tsx\nimport { View } from 'react-native'\n\nfunction Greeting({ name }: { name: string }) {\n  return <View>Hello, {name}!</View>\n}\n// Error: Text strings must be rendered within a <Text> component.\n```\n\n**Correct:**\n\n```tsx\nimport { View, Text } from 'react-native'\n\nfunction Greeting({ name }: { name: string }) {\n  return (\n    <View>\n      <Text>Hello, {name}!</Text>\n    </View>\n  )\n}\n```\n"
  },
  {
    "path": "skills/react-native-skills/rules/scroll-position-no-state.md",
    "content": "---\ntitle: Never Track Scroll Position in useState\nimpact: HIGH\nimpactDescription: prevents render thrashing during scroll\ntags: scroll, performance, reanimated, useRef\n---\n\n## Never Track Scroll Position in useState\n\nNever store scroll position in `useState`. Scroll events fire rapidly—state\nupdates cause render thrashing and dropped frames. Use a Reanimated shared value\nfor animations or a ref for non-reactive tracking.\n\n**Incorrect (useState causes jank):**\n\n```tsx\nimport { useState } from 'react'\nimport {\n  ScrollView,\n  NativeSyntheticEvent,\n  NativeScrollEvent,\n} from 'react-native'\n\nfunction Feed() {\n  const [scrollY, setScrollY] = useState(0)\n\n  const onScroll = (e: NativeSyntheticEvent<NativeScrollEvent>) => {\n    setScrollY(e.nativeEvent.contentOffset.y) // re-renders on every frame\n  }\n\n  return <ScrollView onScroll={onScroll} scrollEventThrottle={16} />\n}\n```\n\n**Correct (Reanimated for animations):**\n\n```tsx\nimport Animated, {\n  useSharedValue,\n  useAnimatedScrollHandler,\n} from 'react-native-reanimated'\n\nfunction Feed() {\n  const scrollY = useSharedValue(0)\n\n  const onScroll = useAnimatedScrollHandler({\n    onScroll: (e) => {\n      scrollY.value = e.contentOffset.y // runs on UI thread, no re-render\n    },\n  })\n\n  return (\n    <Animated.ScrollView\n      onScroll={onScroll}\n      // higher number has better performance, but it fires less often.\n      // unset this if you need higher precision over performance.\n      scrollEventThrottle={16}\n    />\n  )\n}\n```\n\n**Correct (ref for non-reactive tracking):**\n\n```tsx\nimport { useRef } from 'react'\nimport {\n  ScrollView,\n  NativeSyntheticEvent,\n  NativeScrollEvent,\n} from 'react-native'\n\nfunction Feed() {\n  const scrollY = useRef(0)\n\n  const onScroll = (e: NativeSyntheticEvent<NativeScrollEvent>) => {\n    scrollY.current = e.nativeEvent.contentOffset.y // no re-render\n  }\n\n  return <ScrollView onScroll={onScroll} scrollEventThrottle={16} />\n}\n```\n"
  },
  {
    "path": "skills/react-native-skills/rules/state-ground-truth.md",
    "content": "---\ntitle: State Must Represent Ground Truth\nimpact: HIGH\nimpactDescription: cleaner logic, easier debugging, single source of truth\ntags: state, derived-state, reanimated, hooks\n---\n\n## State Must Represent Ground Truth\n\nState variables—both React `useState` and Reanimated shared values—should\nrepresent the actual state of something (e.g., `pressed`, `progress`, `isOpen`),\nnot derived visual values (e.g., `scale`, `opacity`, `translateY`). Derive\nvisual values from state using computation or interpolation.\n\n**Incorrect (storing the visual output):**\n\n```tsx\nconst scale = useSharedValue(1)\n\nconst tap = Gesture.Tap()\n  .onBegin(() => {\n    scale.set(withTiming(0.95))\n  })\n  .onFinalize(() => {\n    scale.set(withTiming(1))\n  })\n\nconst animatedStyle = useAnimatedStyle(() => ({\n  transform: [{ scale: scale.get() }],\n}))\n```\n\n**Correct (storing the state, deriving the visual):**\n\n```tsx\nconst pressed = useSharedValue(0) // 0 = not pressed, 1 = pressed\n\nconst tap = Gesture.Tap()\n  .onBegin(() => {\n    pressed.set(withTiming(1))\n  })\n  .onFinalize(() => {\n    pressed.set(withTiming(0))\n  })\n\nconst animatedStyle = useAnimatedStyle(() => ({\n  transform: [{ scale: interpolate(pressed.get(), [0, 1], [1, 0.95]) }],\n}))\n```\n\n**Why this matters:**\n\nState variables should represent real \"state\", not necessarily a desired end\nresult.\n\n1. **Single source of truth** — The state (`pressed`) describes what's\n   happening; visuals are derived\n2. **Easier to extend** — Adding opacity, rotation, or other effects just\n   requires more interpolations from the same state\n3. **Debugging** — Inspecting `pressed = 1` is clearer than `scale = 0.95`\n4. **Reusable logic** — The same `pressed` value can drive multiple visual\n   properties\n\n**Same principle for React state:**\n\n```tsx\n// Incorrect: storing derived values\nconst [isExpanded, setIsExpanded] = useState(false)\nconst [height, setHeight] = useState(0)\n\nuseEffect(() => {\n  setHeight(isExpanded ? 200 : 0)\n}, [isExpanded])\n\n// Correct: derive from state\nconst [isExpanded, setIsExpanded] = useState(false)\nconst height = isExpanded ? 200 : 0\n```\n\nState is the minimal truth. Everything else is derived.\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-expo-image.md",
    "content": "---\ntitle: Use expo-image for Optimized Images\nimpact: HIGH\nimpactDescription: memory efficiency, caching, blurhash placeholders, progressive loading\ntags: images, performance, expo-image, ui\n---\n\n## Use expo-image for Optimized Images\n\nUse `expo-image` instead of React Native's `Image`. It provides memory-efficient caching, blurhash placeholders, progressive loading, and better performance for lists.\n\n**Incorrect (React Native Image):**\n\n```tsx\nimport { Image } from 'react-native'\n\nfunction Avatar({ url }: { url: string }) {\n  return <Image source={{ uri: url }} style={styles.avatar} />\n}\n```\n\n**Correct (expo-image):**\n\n```tsx\nimport { Image } from 'expo-image'\n\nfunction Avatar({ url }: { url: string }) {\n  return <Image source={{ uri: url }} style={styles.avatar} />\n}\n```\n\n**With blurhash placeholder:**\n\n```tsx\n<Image\n  source={{ uri: url }}\n  placeholder={{ blurhash: 'LGF5]+Yk^6#M@-5c,1J5@[or[Q6.' }}\n  contentFit=\"cover\"\n  transition={200}\n  style={styles.image}\n/>\n```\n\n**With priority and caching:**\n\n```tsx\n<Image\n  source={{ uri: url }}\n  priority=\"high\"\n  cachePolicy=\"memory-disk\"\n  style={styles.hero}\n/>\n```\n\n**Key props:**\n\n- `placeholder` — Blurhash or thumbnail while loading\n- `contentFit` — `cover`, `contain`, `fill`, `scale-down`\n- `transition` — Fade-in duration (ms)\n- `priority` — `low`, `normal`, `high`\n- `cachePolicy` — `memory`, `disk`, `memory-disk`, `none`\n- `recyclingKey` — Unique key for list recycling\n\nFor cross-platform (web + native), use `SolitoImage` from `solito/image` which uses `expo-image` under the hood.\n\nReference: [expo-image](https://docs.expo.dev/versions/latest/sdk/image/)\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-image-gallery.md",
    "content": "---\ntitle: Use Galeria for Image Galleries and Lightbox\nimpact: MEDIUM\nimpactDescription:\n  native shared element transitions, pinch-to-zoom, pan-to-close\ntags: images, gallery, lightbox, expo-image, ui\n---\n\n## Use Galeria for Image Galleries and Lightbox\n\nFor image galleries with lightbox (tap to fullscreen), use `@nandorojo/galeria`.\nIt provides native shared element transitions with pinch-to-zoom, double-tap\nzoom, and pan-to-close. Works with any image component including `expo-image`.\n\n**Incorrect (custom modal implementation):**\n\n```tsx\nfunction ImageGallery({ urls }: { urls: string[] }) {\n  const [selected, setSelected] = useState<string | null>(null)\n\n  return (\n    <>\n      {urls.map((url) => (\n        <Pressable key={url} onPress={() => setSelected(url)}>\n          <Image source={{ uri: url }} style={styles.thumbnail} />\n        </Pressable>\n      ))}\n      <Modal visible={!!selected} onRequestClose={() => setSelected(null)}>\n        <Image source={{ uri: selected! }} style={styles.fullscreen} />\n      </Modal>\n    </>\n  )\n}\n```\n\n**Correct (Galeria with expo-image):**\n\n```tsx\nimport { Galeria } from '@nandorojo/galeria'\nimport { Image } from 'expo-image'\n\nfunction ImageGallery({ urls }: { urls: string[] }) {\n  return (\n    <Galeria urls={urls}>\n      {urls.map((url, index) => (\n        <Galeria.Image index={index} key={url}>\n          <Image source={{ uri: url }} style={styles.thumbnail} />\n        </Galeria.Image>\n      ))}\n    </Galeria>\n  )\n}\n```\n\n**Single image:**\n\n```tsx\nimport { Galeria } from '@nandorojo/galeria'\nimport { Image } from 'expo-image'\n\nfunction Avatar({ url }: { url: string }) {\n  return (\n    <Galeria urls={[url]}>\n      <Galeria.Image>\n        <Image source={{ uri: url }} style={styles.avatar} />\n      </Galeria.Image>\n    </Galeria>\n  )\n}\n```\n\n**With low-res thumbnails and high-res fullscreen:**\n\n```tsx\n<Galeria urls={highResUrls}>\n  {lowResUrls.map((url, index) => (\n    <Galeria.Image index={index} key={url}>\n      <Image source={{ uri: url }} style={styles.thumbnail} />\n    </Galeria.Image>\n  ))}\n</Galeria>\n```\n\n**With FlashList:**\n\n```tsx\n<Galeria urls={urls}>\n  <FlashList\n    data={urls}\n    renderItem={({ item, index }) => (\n      <Galeria.Image index={index}>\n        <Image source={{ uri: item }} style={styles.thumbnail} />\n      </Galeria.Image>\n    )}\n    numColumns={3}\n    estimatedItemSize={100}\n  />\n</Galeria>\n```\n\nWorks with `expo-image`, `SolitoImage`, `react-native` Image, or any image\ncomponent.\n\nReference: [Galeria](https://github.com/nandorojo/galeria)\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-measure-views.md",
    "content": "---\ntitle: Measuring View Dimensions\nimpact: MEDIUM\nimpactDescription: synchronous measurement, avoid unnecessary re-renders\ntags: layout, measurement, onLayout, useLayoutEffect\n---\n\n## Measuring View Dimensions\n\nUse both `useLayoutEffect` (synchronous) and `onLayout` (for updates). The sync\nmeasurement gives you the initial size immediately; `onLayout` keeps it current\nwhen the view changes. For non-primitive states, use a dispatch updater to\ncompare values and avoid unnecessary re-renders.\n\n**Height only:**\n\n```tsx\nimport { useLayoutEffect, useRef, useState } from 'react'\nimport { View, LayoutChangeEvent } from 'react-native'\n\nfunction MeasuredBox({ children }: { children: React.ReactNode }) {\n  const ref = useRef<View>(null)\n  const [height, setHeight] = useState<number | undefined>(undefined)\n\n  useLayoutEffect(() => {\n    // Sync measurement on mount (RN 0.82+)\n    const rect = ref.current?.getBoundingClientRect()\n    if (rect) setHeight(rect.height)\n    // Pre-0.82: ref.current?.measure((x, y, w, h) => setHeight(h))\n  }, [])\n\n  const onLayout = (e: LayoutChangeEvent) => {\n    setHeight(e.nativeEvent.layout.height)\n  }\n\n  return (\n    <View ref={ref} onLayout={onLayout}>\n      {children}\n    </View>\n  )\n}\n```\n\n**Both dimensions:**\n\n```tsx\nimport { useLayoutEffect, useRef, useState } from 'react'\nimport { View, LayoutChangeEvent } from 'react-native'\n\ntype Size = { width: number; height: number }\n\nfunction MeasuredBox({ children }: { children: React.ReactNode }) {\n  const ref = useRef<View>(null)\n  const [size, setSize] = useState<Size | undefined>(undefined)\n\n  useLayoutEffect(() => {\n    const rect = ref.current?.getBoundingClientRect()\n    if (rect) setSize({ width: rect.width, height: rect.height })\n  }, [])\n\n  const onLayout = (e: LayoutChangeEvent) => {\n    const { width, height } = e.nativeEvent.layout\n    setSize((prev) => {\n      // for non-primitive states, compare values before firing a re-render\n      if (prev?.width === width && prev?.height === height) return prev\n      return { width, height }\n    })\n  }\n\n  return (\n    <View ref={ref} onLayout={onLayout}>\n      {children}\n    </View>\n  )\n}\n```\n\nUse functional setState to compare—don't read state directly in the callback.\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-menus.md",
    "content": "---\ntitle: Use Native Menus for Dropdowns and Context Menus\nimpact: HIGH\nimpactDescription: native accessibility, platform-consistent UX\ntags: user-interface, menus, context-menus, zeego, accessibility\n---\n\n## Use Native Menus for Dropdowns and Context Menus\n\nUse native platform menus instead of custom JS implementations. Native menus\nprovide built-in accessibility, consistent platform UX, and better performance.\nUse [zeego](https://zeego.dev) for cross-platform native menus.\n\n**Incorrect (custom JS menu):**\n\n```tsx\nimport { useState } from 'react'\nimport { View, Pressable, Text } from 'react-native'\n\nfunction MyMenu() {\n  const [open, setOpen] = useState(false)\n\n  return (\n    <View>\n      <Pressable onPress={() => setOpen(!open)}>\n        <Text>Open Menu</Text>\n      </Pressable>\n      {open && (\n        <View style={{ position: 'absolute', top: 40 }}>\n          <Pressable onPress={() => console.log('edit')}>\n            <Text>Edit</Text>\n          </Pressable>\n          <Pressable onPress={() => console.log('delete')}>\n            <Text>Delete</Text>\n          </Pressable>\n        </View>\n      )}\n    </View>\n  )\n}\n```\n\n**Correct (native menu with zeego):**\n\n```tsx\nimport * as DropdownMenu from 'zeego/dropdown-menu'\n\nfunction MyMenu() {\n  return (\n    <DropdownMenu.Root>\n      <DropdownMenu.Trigger>\n        <Pressable>\n          <Text>Open Menu</Text>\n        </Pressable>\n      </DropdownMenu.Trigger>\n\n      <DropdownMenu.Content>\n        <DropdownMenu.Item key='edit' onSelect={() => console.log('edit')}>\n          <DropdownMenu.ItemTitle>Edit</DropdownMenu.ItemTitle>\n        </DropdownMenu.Item>\n\n        <DropdownMenu.Item\n          key='delete'\n          destructive\n          onSelect={() => console.log('delete')}\n        >\n          <DropdownMenu.ItemTitle>Delete</DropdownMenu.ItemTitle>\n        </DropdownMenu.Item>\n      </DropdownMenu.Content>\n    </DropdownMenu.Root>\n  )\n}\n```\n\n**Context menu (long-press):**\n\n```tsx\nimport * as ContextMenu from 'zeego/context-menu'\n\nfunction MyContextMenu() {\n  return (\n    <ContextMenu.Root>\n      <ContextMenu.Trigger>\n        <View style={{ padding: 20 }}>\n          <Text>Long press me</Text>\n        </View>\n      </ContextMenu.Trigger>\n\n      <ContextMenu.Content>\n        <ContextMenu.Item key='copy' onSelect={() => console.log('copy')}>\n          <ContextMenu.ItemTitle>Copy</ContextMenu.ItemTitle>\n        </ContextMenu.Item>\n\n        <ContextMenu.Item key='paste' onSelect={() => console.log('paste')}>\n          <ContextMenu.ItemTitle>Paste</ContextMenu.ItemTitle>\n        </ContextMenu.Item>\n      </ContextMenu.Content>\n    </ContextMenu.Root>\n  )\n}\n```\n\n**Checkbox items:**\n\n```tsx\nimport * as DropdownMenu from 'zeego/dropdown-menu'\n\nfunction SettingsMenu() {\n  const [notifications, setNotifications] = useState(true)\n\n  return (\n    <DropdownMenu.Root>\n      <DropdownMenu.Trigger>\n        <Pressable>\n          <Text>Settings</Text>\n        </Pressable>\n      </DropdownMenu.Trigger>\n\n      <DropdownMenu.Content>\n        <DropdownMenu.CheckboxItem\n          key='notifications'\n          value={notifications}\n          onValueChange={() => setNotifications((prev) => !prev)}\n        >\n          <DropdownMenu.ItemIndicator />\n          <DropdownMenu.ItemTitle>Notifications</DropdownMenu.ItemTitle>\n        </DropdownMenu.CheckboxItem>\n      </DropdownMenu.Content>\n    </DropdownMenu.Root>\n  )\n}\n```\n\n**Submenus:**\n\n```tsx\nimport * as DropdownMenu from 'zeego/dropdown-menu'\n\nfunction MenuWithSubmenu() {\n  return (\n    <DropdownMenu.Root>\n      <DropdownMenu.Trigger>\n        <Pressable>\n          <Text>Options</Text>\n        </Pressable>\n      </DropdownMenu.Trigger>\n\n      <DropdownMenu.Content>\n        <DropdownMenu.Item key='home' onSelect={() => console.log('home')}>\n          <DropdownMenu.ItemTitle>Home</DropdownMenu.ItemTitle>\n        </DropdownMenu.Item>\n\n        <DropdownMenu.Sub>\n          <DropdownMenu.SubTrigger key='more'>\n            <DropdownMenu.ItemTitle>More Options</DropdownMenu.ItemTitle>\n          </DropdownMenu.SubTrigger>\n\n          <DropdownMenu.SubContent>\n            <DropdownMenu.Item key='settings'>\n              <DropdownMenu.ItemTitle>Settings</DropdownMenu.ItemTitle>\n            </DropdownMenu.Item>\n\n            <DropdownMenu.Item key='help'>\n              <DropdownMenu.ItemTitle>Help</DropdownMenu.ItemTitle>\n            </DropdownMenu.Item>\n          </DropdownMenu.SubContent>\n        </DropdownMenu.Sub>\n      </DropdownMenu.Content>\n    </DropdownMenu.Root>\n  )\n}\n```\n\nReference: [Zeego Documentation](https://zeego.dev/components/dropdown-menu)\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-native-modals.md",
    "content": "---\ntitle: Use Native Modals Over JS-Based Bottom Sheets\nimpact: HIGH\nimpactDescription: native performance, gestures, accessibility\ntags: modals, bottom-sheet, native, react-navigation\n---\n\n## Use Native Modals Over JS-Based Bottom Sheets\n\nUse native `<Modal>` with `presentationStyle=\"formSheet\"` or React Navigation\nv7's native form sheet instead of JS-based bottom sheet libraries. Native modals\nhave built-in gestures, accessibility, and better performance. Rely on native UI\nfor low-level primitives.\n\n**Incorrect (JS-based bottom sheet):**\n\n```tsx\nimport BottomSheet from 'custom-js-bottom-sheet'\n\nfunction MyScreen() {\n  const sheetRef = useRef<BottomSheet>(null)\n\n  return (\n    <View style={{ flex: 1 }}>\n      <Button onPress={() => sheetRef.current?.expand()} title='Open' />\n      <BottomSheet ref={sheetRef} snapPoints={['50%', '90%']}>\n        <View>\n          <Text>Sheet content</Text>\n        </View>\n      </BottomSheet>\n    </View>\n  )\n}\n```\n\n**Correct (native Modal with formSheet):**\n\n```tsx\nimport { Modal, View, Text, Button } from 'react-native'\n\nfunction MyScreen() {\n  const [visible, setVisible] = useState(false)\n\n  return (\n    <View style={{ flex: 1 }}>\n      <Button onPress={() => setVisible(true)} title='Open' />\n      <Modal\n        visible={visible}\n        presentationStyle='formSheet'\n        animationType='slide'\n        onRequestClose={() => setVisible(false)}\n      >\n        <View>\n          <Text>Sheet content</Text>\n        </View>\n      </Modal>\n    </View>\n  )\n}\n```\n\n**Correct (React Navigation v7 native form sheet):**\n\n```tsx\n// In your navigator\n<Stack.Screen\n  name='Details'\n  component={DetailsScreen}\n  options={{\n    presentation: 'formSheet',\n    sheetAllowedDetents: 'fitToContents',\n  }}\n/>\n```\n\nNative modals provide swipe-to-dismiss, proper keyboard avoidance, and\naccessibility out of the box.\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-pressable.md",
    "content": "---\ntitle: Use Pressable Instead of Touchable Components\nimpact: LOW\nimpactDescription: modern API, more flexible\ntags: ui, pressable, touchable, gestures\n---\n\n## Use Pressable Instead of Touchable Components\n\nNever use `TouchableOpacity` or `TouchableHighlight`. Use `Pressable` from\n`react-native` or `react-native-gesture-handler` instead.\n\n**Incorrect (legacy Touchable components):**\n\n```tsx\nimport { TouchableOpacity } from 'react-native'\n\nfunction MyButton({ onPress }: { onPress: () => void }) {\n  return (\n    <TouchableOpacity onPress={onPress} activeOpacity={0.7}>\n      <Text>Press me</Text>\n    </TouchableOpacity>\n  )\n}\n```\n\n**Correct (Pressable):**\n\n```tsx\nimport { Pressable } from 'react-native'\n\nfunction MyButton({ onPress }: { onPress: () => void }) {\n  return (\n    <Pressable onPress={onPress}>\n      <Text>Press me</Text>\n    </Pressable>\n  )\n}\n```\n\n**Correct (Pressable from gesture handler for lists):**\n\n```tsx\nimport { Pressable } from 'react-native-gesture-handler'\n\nfunction ListItem({ onPress }: { onPress: () => void }) {\n  return (\n    <Pressable onPress={onPress}>\n      <Text>Item</Text>\n    </Pressable>\n  )\n}\n```\n\nUse `react-native-gesture-handler` Pressable inside scrollable lists for better\ngesture coordination, as long as you are using the ScrollView from\n`react-native-gesture-handler` as well.\n\n**For animated press states (scale, opacity changes):** Use `GestureDetector`\nwith Reanimated shared values instead of Pressable's style callback. See the\n`animation-gesture-detector-press` rule.\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-safe-area-scroll.md",
    "content": "---\ntitle: Use contentInsetAdjustmentBehavior for Safe Areas\nimpact: MEDIUM\nimpactDescription: native safe area handling, no layout shifts\ntags: safe-area, scrollview, layout\n---\n\n## Use contentInsetAdjustmentBehavior for Safe Areas\n\nUse `contentInsetAdjustmentBehavior=\"automatic\"` on the root ScrollView instead of wrapping content in SafeAreaView or manual padding. This lets iOS handle safe area insets natively with proper scroll behavior.\n\n**Incorrect (SafeAreaView wrapper):**\n\n```tsx\nimport { SafeAreaView, ScrollView, View, Text } from 'react-native'\n\nfunction MyScreen() {\n  return (\n    <SafeAreaView style={{ flex: 1 }}>\n      <ScrollView>\n        <View>\n          <Text>Content</Text>\n        </View>\n      </ScrollView>\n    </SafeAreaView>\n  )\n}\n```\n\n**Incorrect (manual safe area padding):**\n\n```tsx\nimport { ScrollView, View, Text } from 'react-native'\nimport { useSafeAreaInsets } from 'react-native-safe-area-context'\n\nfunction MyScreen() {\n  const insets = useSafeAreaInsets()\n\n  return (\n    <ScrollView contentContainerStyle={{ paddingTop: insets.top }}>\n      <View>\n        <Text>Content</Text>\n      </View>\n    </ScrollView>\n  )\n}\n```\n\n**Correct (native content inset adjustment):**\n\n```tsx\nimport { ScrollView, View, Text } from 'react-native'\n\nfunction MyScreen() {\n  return (\n    <ScrollView contentInsetAdjustmentBehavior='automatic'>\n      <View>\n        <Text>Content</Text>\n      </View>\n    </ScrollView>\n  )\n}\n```\n\nThe native approach handles dynamic safe areas (keyboard, toolbars) and allows content to scroll behind the status bar naturally.\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-scrollview-content-inset.md",
    "content": "---\ntitle: Use contentInset for Dynamic ScrollView Spacing\nimpact: LOW\nimpactDescription: smoother updates, no layout recalculation\ntags: scrollview, layout, contentInset, performance\n---\n\n## Use contentInset for Dynamic ScrollView Spacing\n\nWhen adding space to the top or bottom of a ScrollView that may change\n(keyboard, toolbars, dynamic content), use `contentInset` instead of padding.\nChanging `contentInset` doesn't trigger layout recalculation—it adjusts the\nscroll area without re-rendering content.\n\n**Incorrect (padding causes layout recalculation):**\n\n```tsx\nfunction Feed({ bottomOffset }: { bottomOffset: number }) {\n  return (\n    <ScrollView contentContainerStyle={{ paddingBottom: bottomOffset }}>\n      {children}\n    </ScrollView>\n  )\n}\n// Changing bottomOffset triggers full layout recalculation\n```\n\n**Correct (contentInset for dynamic spacing):**\n\n```tsx\nfunction Feed({ bottomOffset }: { bottomOffset: number }) {\n  return (\n    <ScrollView\n      contentInset={{ bottom: bottomOffset }}\n      scrollIndicatorInsets={{ bottom: bottomOffset }}\n    >\n      {children}\n    </ScrollView>\n  )\n}\n// Changing bottomOffset only adjusts scroll bounds\n```\n\nUse `scrollIndicatorInsets` alongside `contentInset` to keep the scroll\nindicator aligned. For static spacing that never changes, padding is fine.\n"
  },
  {
    "path": "skills/react-native-skills/rules/ui-styling.md",
    "content": "---\ntitle: Modern React Native Styling Patterns\nimpact: MEDIUM\nimpactDescription: consistent design, smoother borders, cleaner layouts\ntags: styling, css, layout, shadows, gradients\n---\n\n## Modern React Native Styling Patterns\n\nFollow these styling patterns for cleaner, more consistent React Native code.\n\n**Always use `borderCurve: 'continuous'` with `borderRadius`:**\n\n```tsx\n// Incorrect\n{ borderRadius: 12 }\n\n// Correct – smoother iOS-style corners\n{ borderRadius: 12, borderCurve: 'continuous' }\n```\n\n**Use `gap` instead of margin for spacing between elements:**\n\n```tsx\n// Incorrect – margin on children\n<View>\n  <Text style={{ marginBottom: 8 }}>Title</Text>\n  <Text style={{ marginBottom: 8 }}>Subtitle</Text>\n</View>\n\n// Correct – gap on parent\n<View style={{ gap: 8 }}>\n  <Text>Title</Text>\n  <Text>Subtitle</Text>\n</View>\n```\n\n**Use `padding` for space within, `gap` for space between:**\n\n```tsx\n<View style={{ padding: 16, gap: 12 }}>\n  <Text>First</Text>\n  <Text>Second</Text>\n</View>\n```\n\n**Use `experimental_backgroundImage` for linear gradients:**\n\n```tsx\n// Incorrect – third-party gradient library\n<LinearGradient colors={['#000', '#fff']} />\n\n// Correct – native CSS gradient syntax\n<View\n  style={{\n    experimental_backgroundImage: 'linear-gradient(to bottom, #000, #fff)',\n  }}\n/>\n```\n\n**Use CSS `boxShadow` string syntax for shadows:**\n\n```tsx\n// Incorrect – legacy shadow objects or elevation\n{ shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1 }\n{ elevation: 4 }\n\n// Correct – CSS box-shadow syntax\n{ boxShadow: '0 2px 8px rgba(0, 0, 0, 0.1)' }\n```\n\n**Avoid multiple font sizes – use weight and color for emphasis:**\n\n```tsx\n// Incorrect – varying font sizes for hierarchy\n<Text style={{ fontSize: 18 }}>Title</Text>\n<Text style={{ fontSize: 14 }}>Subtitle</Text>\n<Text style={{ fontSize: 12 }}>Caption</Text>\n\n// Correct – consistent size, vary weight and color\n<Text style={{ fontWeight: '600' }}>Title</Text>\n<Text style={{ color: '#666' }}>Subtitle</Text>\n<Text style={{ color: '#999' }}>Caption</Text>\n```\n\nLimiting font sizes creates visual consistency. Use `fontWeight` (bold/semibold)\nand grayscale colors for hierarchy instead.\n"
  },
  {
    "path": "skills/vercel-cli-with-tokens/SKILL.md",
    "content": "---\nname: vercel-cli-with-tokens\ndescription: Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g. \"deploy to vercel\", \"set up vercel\", \"add environment variables to vercel\".\nmetadata:\n  author: vercel\n  version: \"1.0.0\"\n---\n\n# Vercel CLI with Tokens\n\nDeploy and manage projects on Vercel using the CLI with token-based authentication, without relying on `vercel login`.\n\n## Step 1: Locate the Vercel Token\n\nBefore running any Vercel CLI commands, identify where the token is coming from. Work through these scenarios in order:\n\n### A) `VERCEL_TOKEN` is already set in the environment\n\n```bash\nprintenv VERCEL_TOKEN\n```\n\nIf this returns a value, you're ready. Skip to Step 2.\n\n### B) Token is in a `.env` file under `VERCEL_TOKEN`\n\n```bash\ngrep '^VERCEL_TOKEN=' .env 2>/dev/null\n```\n\nIf found, export it:\n\n```bash\nexport VERCEL_TOKEN=$(grep '^VERCEL_TOKEN=' .env | cut -d= -f2-)\n```\n\n### C) Token is in a `.env` file under a different name\n\nLook for any variable that looks like a Vercel token (Vercel tokens typically start with `vca_`):\n\n```bash\ngrep -i 'vercel' .env 2>/dev/null\n```\n\nInspect the output to identify which variable holds the token, then export it as `VERCEL_TOKEN`:\n\n```bash\nexport VERCEL_TOKEN=$(grep '^<VARIABLE_NAME>=' .env | cut -d= -f2-)\n```\n\n### D) No token found — ask the user\n\nIf none of the above yield a token, ask the user to provide one. They can create a Vercel access token at vercel.com/account/tokens.\n\n---\n\n**Important:** Once `VERCEL_TOKEN` is exported as an environment variable, the Vercel CLI reads it natively — **do not pass it as a `--token` flag**. Putting secrets in command-line arguments exposes them in shell history and process listings.\n\n```bash\n# Bad — token visible in shell history and process listings\nvercel deploy --token \"vca_abc123\"\n\n# Good — CLI reads VERCEL_TOKEN from the environment\nexport VERCEL_TOKEN=\"vca_abc123\"\nvercel deploy\n```\n\n## Step 2: Locate the Project and Team\n\nSimilarly, check for the project ID and team scope. These let the CLI target the right project without needing `vercel link`.\n\n```bash\n# Check environment\nprintenv VERCEL_PROJECT_ID\nprintenv VERCEL_ORG_ID\n\n# Or check .env\ngrep -i 'vercel' .env 2>/dev/null\n```\n\n**If you have a project URL** (e.g. `https://vercel.com/my-team/my-project`), extract the team slug:\n\n```bash\n# e.g. \"my-team\" from \"https://vercel.com/my-team/my-project\"\necho \"$PROJECT_URL\" | sed 's|https://vercel.com/||' | cut -d/ -f1\n```\n\n**If you have both `VERCEL_ORG_ID` and `VERCEL_PROJECT_ID` in your environment**, export them — the CLI will use these automatically and skip any `.vercel/` directory:\n\n```bash\nexport VERCEL_ORG_ID=\"<org-id>\"\nexport VERCEL_PROJECT_ID=\"<project-id>\"\n```\n\nNote: `VERCEL_ORG_ID` and `VERCEL_PROJECT_ID` must be set together — setting only one causes an error.\n\n## CLI Setup\n\nEnsure the Vercel CLI is installed:\n\n```bash\nnpm install -g vercel\nvercel --version\n```\n\n## Deploying a Project\n\nAlways deploy as **preview** unless the user explicitly requests production. Choose a method based on what you have available.\n\n### Quick Deploy (have project ID — no linking needed)\n\nWhen `VERCEL_TOKEN` and `VERCEL_PROJECT_ID` are set in the environment, deploy directly:\n\n```bash\nvercel deploy -y --no-wait\n```\n\nWith a team scope (either via `VERCEL_ORG_ID` or `--scope`):\n\n```bash\nvercel deploy --scope <team-slug> -y --no-wait\n```\n\nProduction (only when explicitly requested):\n\n```bash\nvercel deploy --prod --scope <team-slug> -y --no-wait\n```\n\nCheck status:\n\n```bash\nvercel inspect <deployment-url>\n```\n\n### Full Deploy Flow (no project ID — need to link)\n\nUse this when you have a token and team but no pre-existing project ID.\n\n#### Check project state first\n\n```bash\n# Does the project have a git remote?\ngit remote get-url origin 2>/dev/null\n\n# Is it already linked to a Vercel project?\ncat .vercel/project.json 2>/dev/null || cat .vercel/repo.json 2>/dev/null\n```\n\n#### Link the project\n\n**With git remote (preferred):**\n\n```bash\nvercel link --repo --scope <team-slug> -y\n```\n\nReads the git remote and connects to the matching Vercel project. Creates `.vercel/repo.json`. More reliable than plain `vercel link`, which matches by directory name.\n\n**Without git remote:**\n\n```bash\nvercel link --scope <team-slug> -y\n```\n\nCreates `.vercel/project.json`.\n\n**Link to a specific project by name:**\n\n```bash\nvercel link --project <project-name> --scope <team-slug> -y\n```\n\nIf the project is already linked, check `orgId` in `.vercel/project.json` or `.vercel/repo.json` to verify it matches the intended team.\n\n#### Deploy after linking\n\n**A) Git Push Deploy — has git remote (preferred)**\n\nGit pushes trigger automatic Vercel deployments.\n\n1. **Ask the user before pushing.** Never push without explicit approval.\n2. Commit and push:\n   ```bash\n   git add .\n   git commit -m \"deploy: <description of changes>\"\n   git push\n   ```\n3. Vercel builds automatically. Non-production branches get preview deployments.\n4. Retrieve the deployment URL:\n   ```bash\n   sleep 5\n   vercel ls --format json --scope <team-slug>\n   ```\n   Find the latest entry in the `deployments` array.\n\n**B) CLI Deploy — no git remote**\n\n```bash\nvercel deploy --scope <team-slug> -y --no-wait\n```\n\nCheck status:\n\n```bash\nvercel inspect <deployment-url>\n```\n\n### Deploying from a Remote Repository (code not cloned locally)\n\n1. Clone the repository:\n   ```bash\n   git clone <repo-url>\n   cd <repo-name>\n   ```\n2. Link to Vercel:\n   ```bash\n   vercel link --repo --scope <team-slug> -y\n   ```\n3. Deploy via git push (if you have push access) or CLI deploy.\n\n### About `.vercel/` Directory\n\nA linked project has either:\n- `.vercel/project.json` — from `vercel link`. Contains `projectId` and `orgId`.\n- `.vercel/repo.json` — from `vercel link --repo`. Contains `orgId`, `remoteName`, and a `projects` map.\n\nNot needed when `VERCEL_ORG_ID` + `VERCEL_PROJECT_ID` are both set in the environment.\n\n**Do NOT** run `vercel ls`, `vercel project inspect`, or `vercel link` in an unlinked directory to detect state — they will interactively prompt or silently link as a side-effect. Only `vercel whoami` is safe to run anywhere.\n\n## Managing Environment Variables\n\n```bash\n# Set for all environments\necho \"value\" | vercel env add VAR_NAME --scope <team-slug>\n\n# Set for a specific environment (production, preview, development)\necho \"value\" | vercel env add VAR_NAME production --scope <team-slug>\n\n# List environment variables\nvercel env ls --scope <team-slug>\n\n# Pull env vars to local .env file\nvercel env pull --scope <team-slug>\n\n# Remove a variable\nvercel env rm VAR_NAME --scope <team-slug> -y\n```\n\n## Inspecting Deployments\n\n```bash\n# List recent deployments\nvercel ls --format json --scope <team-slug>\n\n# Inspect a specific deployment\nvercel inspect <deployment-url>\n\n# View build logs\nvercel logs <deployment-url>\n```\n\n## Managing Domains\n\n```bash\n# List domains\nvercel domains ls --scope <team-slug>\n\n# Add a domain to the project\nvercel domains add <domain> --scope <team-slug>\n```\n\n## Working Agreement\n\n- **Never pass `VERCEL_TOKEN` as a `--token` flag.** Export it as an environment variable and let the CLI read it natively.\n- **Check the environment for tokens before asking the user.** Look in the current env and `.env` files first.\n- **Default to preview deployments.** Only deploy to production when explicitly asked.\n- **Ask before pushing to git.** Never push commits without the user's approval.\n- **Do not read or modify `.vercel/` files directly.** The CLI manages this directory.\n- **Do not curl/fetch deployed URLs to verify.** Just return the link to the user.\n- **Use `--format json`** when structured output will help with follow-up steps.\n- **Use `-y`** on commands that prompt for confirmation to avoid interactive blocking.\n\n## Troubleshooting\n\n### Token not found\n\nCheck the environment and any `.env` files present:\n\n```bash\nprintenv | grep -i vercel\ngrep -i vercel .env 2>/dev/null\n```\n\n### Authentication error\n\nIf the CLI fails with `Authentication required`:\n- The token may be expired or invalid.\n- Verify: `vercel whoami` (uses `VERCEL_TOKEN` from environment).\n- Ask the user for a fresh token.\n\n### Wrong team\n\nVerify the scope is correct:\n\n```bash\nvercel whoami --scope <team-slug>\n```\n\n### Build failure\n\nCheck the build logs:\n\n```bash\nvercel logs <deployment-url>\n```\n\nCommon causes:\n- Missing dependencies — ensure `package.json` is complete and committed.\n- Missing environment variables — add with `vercel env add`.\n- Framework misconfiguration — check `vercel.json`. Vercel auto-detects frameworks (Next.js, Remix, Vite, etc.) from `package.json`; override with `vercel.json` if detection is wrong.\n\n### CLI not installed\n\n```bash\nnpm install -g vercel\n```\n"
  },
  {
    "path": "skills/web-design-guidelines/SKILL.md",
    "content": "---\nname: web-design-guidelines\ndescription: Review UI code for Web Interface Guidelines compliance. Use when asked to \"review my UI\", \"check accessibility\", \"audit design\", \"review UX\", or \"check my site against best practices\".\nmetadata:\n  author: vercel\n  version: \"1.0.0\"\n  argument-hint: <file-or-pattern>\n---\n\n# Web Interface Guidelines\n\nReview files for compliance with Web Interface Guidelines.\n\n## How It Works\n\n1. Fetch the latest guidelines from the source URL below\n2. Read the specified files (or prompt user for files/pattern)\n3. Check against all rules in the fetched guidelines\n4. Output findings in the terse `file:line` format\n\n## Guidelines Source\n\nFetch fresh guidelines before each review:\n\n```\nhttps://raw.githubusercontent.com/vercel-labs/web-interface-guidelines/main/command.md\n```\n\nUse WebFetch to retrieve the latest rules. The fetched content contains all the rules and output format instructions.\n\n## Usage\n\nWhen a user provides a file or pattern argument:\n1. Fetch guidelines from the source URL above\n2. Read the specified files\n3. Apply all rules from the fetched guidelines\n4. Output findings using the format specified in the guidelines\n\nIf no files specified, ask the user which files to review.\n"
  }
]