react-best-practices

Guides React component design with correct hooks, effects, and refs usage patterns.

Updated Jul 9, 2025
One-click install
npx skills add https://github.com/renpereiradx/erp-webapp --skill react-best-practices-renpereiradx
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: react-best-practices
Source: https://github.com/renpereiradx/erp-webapp/tree/main/.opencode/skills/react-best-practices
Command: npx skills add https://github.com/renpereiradx/erp-webapp --skill react-best-practices-renpereiradx

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? React developers frequently misuse useEffect for derived state, event handling, and state resets, causing bugs like infinite loops, stale closures, and unnecessary re-renders. This Skill provides decision rules and code patterns to write correct React components. ## Core Features & Use Cases - Effect Discipline: Distinguishes when Effects are required (external system synchronization) versus when to use render-time calculations, useMemo, key props, or event handlers instead. - Dependency & Cleanup Patterns: Covers exhaustive-deps compliance, updater functions, useEffectEvent for non-reactive logic, and mandatory cleanup for subscriptions and fetches. - Refs and Custom Hooks: Explains correct ref usage (no render-time reads/writes, ref callbacks for lists, useImperativeHandle) and conventions for focused custom hooks. - Use Case: When refactoring a chat component that reconnects on every render, apply the patterns to move objects inside Effects and wrap callbacks with useEffectEvent to stabilize dependencies. ## Quick Start Review my React component and fix any incorrect useEffect usage following the react-best-practices guidelines.

Frequently Asked Questions about react-best-practices

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

FAQPage Schema
When should I use useEffect in React?▼

Use useEffect only to synchronize with external systems like browser APIs, WebSockets, third-party libraries, or non-React DOM elements. Derived state, event responses, and state resets should use render-time calculations, event handlers, or the key prop instead.

How do I fix useEffect dependency warnings?▼

Fix exhaustive-deps warnings by restructuring code rather than suppressing the linter. Use updater functions like setCount(c => c + 1) to remove state dependencies, or move objects and functions inside the Effect so they are not recreated each render.

What is useEffectEvent used for in React?▼

useEffectEvent wraps non-reactive logic inside Effects so values like theme or callback props can be read without adding them to the dependency array. This prevents reconnections or re-subscriptions when those values change.

Why does my useEffect run twice in development?▼

React intentionally remounts components in development mode to verify your cleanup functions work correctly. Do not block the second run with a ref flag; instead fix the cleanup by disconnecting connections or removing event listeners.

When should I use refs instead of state in React?▼

Use refs for mutable values that do not affect rendering, such as timeout IDs or DOM node references. Never read or write ref.current during render; only access refs inside event handlers and Effects.