react-patterns

Guide React component logic placement across render, effects, and handlers.

1.5k|875|Updated Aug 20, 2021
One-click install
npx skills add https://github.com/saleor/storefront --skill react-patterns-saleor
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: react-patterns
Source: https://github.com/saleor/storefront/tree/main/.claude/skills/react-patterns
Command: npx skills add https://github.com/saleor/storefront --skill react-patterns-saleor

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill helps developers write idiomatic React by enforcing proper lifecycle usage, rendering purity, and correct hook choices to avoid common anti-patterns.

Core Features & Use Cases

  • Renders must be pure: no side effects during render; derived values should be computed in render or in handlers.
  • Effects are for external synchronization: useEffect and useRef for external APIs, subscriptions, and DOM interactions; avoid deriving state inside effects.
  • Lifecycle discipline: mount, update, unmount with cleanup functions to avoid leaks.
  • Hook guidance: choose the right hook (useState, useEffect, useRef, useMemo, useCallback) and apply them to stabilize performance and readability.
  • Use case: apply to writing or reviewing components, fixing hook-related lint errors, and deciding where logic should live (render vs effect vs handler).

Quick Start

Create a small React component that uses useEffect to fetch data and useMemo to compute a derived value.

Frequently Asked Questions about react-patterns

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

FAQPage Schema
How do I decide between putting logic in render, useEffect, or event handlers in React?

Use useEffect for external synchronization like API calls and subscriptions, compute derived values directly in render, and handle user actions in event handlers to maintain React render purity and avoid anti-patterns.

Why does my React component have side effects during render and how do I fix it?

React renders must be pure, meaning no side effects should occur during render. Move external interactions into useEffect, compute derived values directly, and handle user actions in event handlers to fix impure renders.

When should I use useMemo and useCallback to optimize React component performance?

Use useMemo and useCallback to stabilize values and functions across React renders, which helps avoid unnecessary re-renders and improves performance. Apply these hooks when passing stable references to child components is necessary for optimization.

How do I properly clean up external subscriptions and DOM interactions in React useEffect?

Properly clean up external subscriptions in React useEffect by returning a cleanup function from the effect. This ensures that when the component unmounts or updates, listeners are removed and resources are released, preventing memory leaks.

How do I avoid deriving state inside React effects?

Avoid deriving state inside React effects by computing values directly during render. If the derived value is expensive, use useMemo. Reserve useEffect for external synchronization, not for calculating state from other state variables.

What is the right React hook to use for managing a mutable value that doesn't trigger re-renders?

The right React hook for managing a mutable value that doesn't trigger re-renders is useRef. It allows you to persist values across renders without causing the component to update when the value changes, ideal for DOM interactions.