Zum Inhalt springen
Skillpedia
Websites & UI (Frontend)13k · #11389

no-use-effect

Was er macht

Dieser Skill bringt dem KI-Agenten bei, den React-Hook useEffect zu meiden und sicherere Alternativen zu nutzen. Er richtet sich an React-Entwickler, die sauberere und leichter zu debuggende Komponenten wollen.

Wann du ihn nutzt

  • ✓Wenn der Agent gerade eine neue React-Komponente schreiben will.
  • ✓Wenn du Code mit useEffect prĂŒfst und ihn vereinfachen willst.
  • ✓Wenn ein Agent useEffect vorsorglich hinzufĂŒgt und du es entfernt haben willst.

Link zum Skill

https://github.com/humanlayer/humanlayer/blob/main/.claude/skills/no-use-effect/SKILL.md

Kopiere ihn und fĂŒge ihn in deine KI ein (Claude, ChatGPT oder Cursor) mit der Bitte, den Anweisungen zu folgen.

So nutzt du ihn

Am einfachsten: Kopiere die Nachricht und fĂŒge sie in deine KI ein. Sie liest den Skill und folgt den Anweisungen.

Fertige Nachricht zum EinfĂŒgen

Nutze diesen Skill, um mir zu helfen: https://github.com/humanlayer/humanlayer/blob/main/.claude/skills/no-use-effect/SKILL.md Lies die SKILL.md unter diesem Link und folge ihren Anweisungen fĂŒr meine Aufgabe.
Soll deine KI Skills wie diesen selbst finden?Installiere den kostenlosen Skillpedia-MCP: eine Zeile, und sie durchsucht vor jeder Aufgabe 15.000+ Skills.In 1 Minute installieren →

Beispielanfrage an deine KI

“Refaktoriere meine React-Komponente so, dass sie kein useEffect mehr nutzt, und erklĂ€re jede Ersetzung.”

5 neue Skills pro Woche. 1 Minute Lesezeit.

So funktioniert es

Der Agent findet heraus, was jeder Effekt tut, und wĂ€hlt dann eine von fĂŒnf Ersetzungen, etwa das direkte Berechnen eines Wertes oder einen Ereignishandler. FĂŒr seltene FĂ€lle gibt es einen Hook, der nur beim ersten EinhĂ€ngen lĂ€uft.

Technischer Inhalt (originale SKILL.md)

Originaltext des Autors, auf Englisch, direkt von GitHub.

No useEffect

Never call useEffect directly. Use derived state, event handlers, data-fetching libraries, or useMountEffect instead.

Quick Reference

This skill is dervied from the React docs: You Might Not Need an Effect. IMPORTANT: when this skill is invoked you MUST read this post in entirety.

Instead of useEffect for... Use
Deriving state from other state/props Inline computation (Rule 1)
Fetching data useQuery / data-fetching library (Rule 2)
Responding to user actions Event handlers (Rule 3)
One-time external sync on mount useMountEffect (Rule 4)
Resetting state when a prop changes key prop on parent (Rule 5)

When to Use This Skill

  • Writing new React components
  • Refactoring existing useEffect calls
  • Reviewing PRs that introduce useEffect
  • An agent adds useEffect "just in case"

Workflow

1. Identify the useEffect

Determine what the effect is doing -- deriving state, fetching data, responding to an event, syncing with an external system, or resetting state.

2. Apply the Correct Replacement Pattern

Use the five rules below to pick the right replacement.

3. Verify

bun --bun run typecheck -- --filter=<package>
bun --bun run test -- --filter=<package>

The Escape Hatch: useMountEffect

For the rare case where you need to sync with an external system on mount:

The implementation wraps useEffect with an empty dependency array to make intent explicit:

export function useMountEffect(effect: () => void | (() => void)) {
  /* eslint-disable no-restricted-syntax */
  useEffect(effect, []);
}

Replacement Patterns

Rule 1: Derive state, do not sync it

Most effects that set state from other state are unnecessary and add extra renders.

// BAD: Two render cycles - first stale, then filtered
function ProductList() {
  const [products, setProducts] = useState([]);
  const [filteredProducts, setFilteredProducts] = useState([]);

  useEffect(() => {
    setFilteredProducts(products.filter((p) => p.inStock));
  }, [products]);
}

// GOOD: Compute inline in one render
function ProductList() {
  const [products, setProducts] = useState([]);
  const filteredProducts = products.filter((p) => p.inStock);
}

Smell test: You are about to write useEffect(() => setX(deriveFromY(y)), [y]), or you have state that only mirrors other state or props.

Rule 2: Use data-fetching libraries

Effect-based fetching creates race conditions and duplicated caching logic.

// BAD: Race condition risk
function ProductPage({ productId }) {
  const [product, setProduct] = useState(null);

  useEffect(() => {
    fetchProduct(productId).then(setProduct);
  }, [productId]);
}

// GOOD: Query library handles cancellation/caching/staleness
function ProductPage({ productId }) {
  const { data: product } = useQuery(['product', productId], () =>
    fetchProduct(productId)
  );
}

Smell test: Your effect does fetch(...) and then setState(...), or you are re-implementing caching, retries, cancellation, or stale handling.

Rule 3: Event handlers, not effects

If a user clicks a button, do the work in the handler.

// BAD: Effect as an action relay
function LikeButton() {
  const [liked, setLiked] = useState(false);

  useEffect(() => {
    if (liked) {
      postLike();
      setLiked(false);
    }
  }, [liked]);

  return <button onClick={() => setLiked(true)}>Like</button>;
}

// GOOD: Direct event-driven action
function LikeButton() {
  return <button onClick={() => postLike()}>Like</button>;
}

Smell test: State is used as a flag so an effect can do the real action, or you are building "set flag -> effect runs -> reset flag" mechanics.

Rule 4: useMountEffect for one-time external sync

Good uses: DOM integration (focus, scroll), third-party widget lifecycles, browser API subscriptions.

// BAD: Guard inside effect
function VideoPlayer({ isLoading }) {
  useEffect(() => {
    if (!isLoading) playVideo();
  }, [isLoading]);
}

// GOOD: Mount only when preconditions are met
function VideoPlayerWrapper({ isLoading }) {
  if (isLoading) return <LoadingScreen />;
  return <VideoPlayer />;
}

function VideoPlayer() {
  useMountEffect(() => playVideo());
}

Use useMountEffect for stable dependencies (singletons, refs, context values that never c

HĂ€ufige Fragen

Was ersetzt useEffect?

Direkte Berechnung, eine Bibliothek zum Laden von Daten, Ereignishandler, ein Hook fĂŒr einmaliges EinhĂ€ngen und eine key-Prop zum ZurĂŒcksetzen von Zustand.

Verbietet er useEffect ganz?

Er sagt, dass useEffect nie direkt aufgerufen werden soll und stattdessen die genannten Alternativen zu nutzen sind.

Woher stammt der Rat?

Er basiert auf der React-Dokumentation dazu, wann ein Effekt nicht nötig ist.

Details

Originalbeschreibung des Autors

Enforce the no-useEffect rule when writing or reviewing React code. ACTIVATE when writing React components, refactoring existing useEffect calls, reviewing PRs with useEffect, or when an agent adds useEffect "just in case." Provides the five replacement patterns and the useMountEffect escape hatch.

repo: humanlayer/humanlayer

Beliebtheit

GitHub-Sterne
13k
Platz im Ranking
#11389
Lizenz
Apache-2.0
Aktualisiert
31.10.2026

Ist dieser Skill nĂŒtzlich?

0

Anmelden um abzustimmen.

Kommentare

  • 


Weitere Skills aus diesem Repository

Ähnliche Skills