react-useeffect
Install this skill
npx skills add softaworks/agent-toolkitWorks across Claude Code, Cursor, Codex, Copilot & Antigravity
The useEffect hook serves as the primary bridge between React components and non-React environments. It acts as an escape hatch, providing a mechanism to synchronize component state with external systems like browser APIs, third-party libraries, or network data streams. Because it executes after the render cycle, it manages side effects that should not interfere with the rendering process itself. Developers often misuse this hook to manage internal state transitions or data transformations that React handles automatically. When used correctly, it ensures that your application stays aligned with external changes without triggering redundant renders or memory leaks. Understanding the dependency array is critical for controlling exactly when the effect logic runs, preventing infinite loops and ensuring that resources are cleaned up appropriately when the component unmounts.
When to Use This Skill
- β’Integrating a third-party non-React charting library
- β’Setting up a WebSocket connection for real-time updates
- β’Adding and removing DOM event listeners manually
- β’Performing data fetching with race condition protection
How to Invoke This Skill
Example prompts that trigger this skill in Claude Code, Cursor, or Antigravity:
- βhow to run code after component mounts
- βconnect react to a websocket
- βproper way to fetch data in a hook
- βclean up event listeners in react
- βsynchronize with external library
Pro Tips
- π‘Always question if an Effect is truly necessary; often, derived state or event handlers are simpler, more direct solutions.
- π‘When data fetching in `useEffect`, ensure you implement proper cleanup functions to prevent memory leaks and race conditions.
- π‘Combine this skill with `useMemo` and `useCallback` to further optimize performance by memoizing expensive computations and stable functions.
What this skill does
- β’Synchronizes React state with browser-specific APIs
- β’Manages cleanup functions to prevent memory leaks on unmount
- β’Observes dependency changes to trigger conditional logic
- β’Connects components to external data subscriptions
- β’Logs analytics events upon component lifecycle milestones
When not to use it
- βTransforming data or props for rendering purposes
- βUpdating state in response to user-triggered events
Example workflow
- Define the synchronization logic within the callback
- Specify the dependency array to target relevant variables
- Return a cleanup function to handle resource disposal
- Attach the component to the DOM
- Trigger the side effect post-render
- Execute cleanup when dependencies change or component unmounts
Prerequisites
- βBasic understanding of React component lifecycle
- βFamiliarity with functional components
Pitfalls & limitations
- !Creating infinite loops by omitting or misconfiguring the dependency array
- !Forgetting to return a cleanup function leading to memory leaks
- !Over-using effects for tasks that simple variable calculation handles
FAQ
How it compares
Unlike manual event handling which responds to user input, useEffect is specifically for syncing state with external systems that exist outside the React component lifecycle.
π Full skill instructions β original source: softaworks/agent-toolkit
Effects are an **escape hatch** from React. They let you synchronize with external systems. If there is no external system involved, you shouldn't need an Effect.
## Quick Reference
| Situation | DON'T | DO |
|-----------|-------|-----|
| Derived state from props/state |
useState + useEffect | Calculate during render || Expensive calculations |
useEffect to cache | useMemo || Reset state on prop change |
useEffect with setState | key prop || User event responses |
useEffect watching state | Event handler directly || Notify parent of changes |
useEffect calling onChange | Call in event handler || Fetch data |
useEffect without cleanup | useEffect with cleanup OR framework |## When You DO Need Effects
- Synchronizing with **external systems** (non-React widgets, browser APIs)
- **Subscriptions** to external stores (use
useSyncExternalStore when possible)- **Analytics/logging** that runs because component displayed
- **Data fetching** with proper cleanup (or use framework's built-in mechanism)
## When You DON'T Need Effects
1. **Transforming data for rendering** - Calculate at top level, re-runs automatically
2. **Handling user events** - Use event handlers, you know exactly what happened
3. **Deriving state** - Just compute it:
const fullName = firstName + ' ' + lastName4. **Chaining state updates** - Calculate all next state in the event handler
## Decision Tree
Need to respond to something?
βββ User interaction (click, submit, drag)?
β βββ Use EVENT HANDLER
βββ Component appeared on screen?
β βββ Use EFFECT (external sync, analytics)
βββ Props/state changed and need derived value?
β βββ CALCULATE DURING RENDER
β βββ Expensive? Use useMemo
βββ Need to reset state when prop changes?
βββ Use KEY PROP on component## Detailed Guidance
- [Anti-Patterns](./anti-patterns.md) - Common mistakes with fixes
- [Better Alternatives](./alternatives.md) - useMemo, key prop, lifting state, useSyncExternalStore
How to Use This Skill Unit
Option A: Project-Specific (Recommended)
- Click "Download" above
- In your project, create the directory:
.agent/skills/react-useeffect/ - Save the file as
SKILL.md - The agent will automatically discover the skill based on its description.
Option B: Global Installation (All Agents)
Save the file to these locations to make it available across all projects:
- Claude Code:
~/.claude/skills/softaworks/agent-toolkit/react-useeffect/SKILL.md - Cursor:
~/.cursor/skills/softaworks/agent-toolkit/react-useeffect/SKILL.md - Antigravity:
~/.gemini/antigravity/skills/softaworks/agent-toolkit/react-useeffect/SKILL.md
π Install with CLI:npx skills add softaworks/agent-toolkit
