New to Claude Skills? Learn how to install them →

davila7 on GitHub

React UseEffect Best Practices

Free

Master the useEffect hook with best practices and alternatives.

Get this skill

Free · Opens the source repo

What React UseEffect Best Practices does

The React UseEffect Best Practices skill provides developers with essential guidelines for using the useEffect hook effectively in their applications. It emphasizes when to use effects and, more importantly, when to avoid them. By following these best practices, developers can ensure that their components remain performant and maintainable. The skill offers a decision tree that simplifies the process of determining whether an effect is necessary based on the specific situation, such as user interactions or prop changes.

This skill is particularly useful for both new and experienced React developers who want to deepen their understanding of state management and side effects. It outlines common anti-patterns associated with useEffect, such as using it for derived state or handling user events, and provides better alternatives like using event handlers or calculating values during render. The guidance is rooted in the official React documentation, ensuring that users are aligned with the framework's best practices.

In addition to the decision tree, the skill includes detailed guidance on handling specific scenarios, such as data fetching and external subscriptions. It also highlights the importance of cleanup in effects to prevent memory leaks and other issues. By implementing the recommendations from this skill, developers can enhance the reliability and clarity of their React applications, ultimately leading to a better user experience.

When to use it

Use this skill when writing or reviewing components that utilize the useEffect and useState hooks, especially for data fetching or state synchronization.

When not to use it

This skill may not be necessary for simple components that do not involve side effects or derived state, where basic React principles suffice.

What you can build with it

Reviewing Component Code

Use this skill to evaluate existing components for proper use of useEffect and identify any potential anti-patterns.

Teaching React Best Practices

Incorporate this skill into your teaching materials to help students learn the correct usage of hooks in React.

Optimizing Data Fetching

Refer to this skill when implementing data fetching in your components to ensure proper cleanup and performance.

How to install React UseEffect Best Practices

View source

1. Install with the skills CLI

npx skills add davila7/claude-code-templates/react-useeffect --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by davila7

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

Frequently asked questions about React UseEffect Best Practices

Similar skills