Get the FREE Ultimate OpenClaw Setup Guide →

react-useeffect

npx machina-cli add skill softaworks/agent-toolkit/react-useeffect --openclaw
Files (1)
SKILL.md
2.3 KB

You Might Not Need an Effect

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

SituationDON'TDO
Derived state from props/stateuseState + useEffectCalculate during render
Expensive calculationsuseEffect to cacheuseMemo
Reset state on prop changeuseEffect with setStatekey prop
User event responsesuseEffect watching stateEvent handler directly
Notify parent of changesuseEffect calling onChangeCall in event handler
Fetch datauseEffect without cleanupuseEffect 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 + ' ' + lastName
  4. 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

Source

git clone https://github.com/softaworks/agent-toolkit/blob/main/skills/react-useeffect/SKILL.mdView on GitHub

Overview

This skill explains when to use useEffect, when to avoid it, and which patterns to prefer for data fetching, external synchronization, and analytics. It emphasizes computing derived values during render, using proper cleanup, and leveraging alternatives like key props and useSyncExternalStore.

How This Skill Works

The guide walks you through a decision process: if you need to synchronize with external systems, useEffect; otherwise compute derived values during render or via useMemo. It also highlights anti-patterns and points to better alternatives such as useSyncExternalStore and key props to reset state.

When to Use It

  • Synchronizing with external systems or browser APIs
  • Subscriptions to external stores (use useSyncExternalStore when possible)
  • Analytics or logging that runs when the component is displayed
  • Data fetching with proper cleanup (or framework-supported patterns)
  • Component mounting or prop/state changes that require external synchronization

Quick Start

  1. Step 1: Decide if an effect is actually needed using the decision guidance (external sync, analytics, data fetch) or if a render-time calculation is sufficient
  2. Step 2: If an effect is needed, implement it with proper cleanup and consider alternatives like useSyncExternalStore or a key prop where appropriate
  3. Step 3: Avoid using effects for derived state or event handling; compute values in render or with useMemo, and attach explicit event handlers

Best Practices

  • Derive state during render instead of using useEffect for derived values
  • Use useMemo for expensive calculations rather than deferring them to an effect
  • Reset state on prop changes with a key prop, not with useEffect
  • Handle user events with explicit event handlers rather than relying on effects
  • Always clean up effects to prevent memory leaks; align data-fetching patterns with framework guidance

Example Use Cases

  • Data fetch with cleanup: fetch user data on mount and cancel on unmount
  • Subscribing to an external store using useSyncExternalStore to mirror external data
  • Analytics: log a page impression in an effect when the component renders
  • Derived values: compute fullName from firstName and lastName inside render
  • Resetting form state by changing the key prop when a parent passes a different userId

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers