{"success":true,"name":"react-effect-runs-every-render-an-object-or-array-dependency","description":"Working in React and an effect loops endlessly or fires on every render","category":"ai-brimind","path":"ai-brimind/react-effect-runs-every-render-an-object-or-array-dependency/SKILL.md","file":"SKILL.md","content":"---\nname: react-effect-runs-every-render-an-object-or-array-dependency\ndescription: \"Working in React and an effect loops endlessly or fires on every render\"\n---\n\n# React Effect Runs Every Render? An Object or Array Dependency Is Reborn Each Time\n\n## When to use\nProject is React, and a useEffect with a dependency array still runs constantly, or updating state inside it creates an infinite loop.\n\n## Example request\nan options object built in the component body listed as a dependency → new reference every render, effect fires every render; reborn dependency. effect fetches then sets state, state changes rebuild the object → fetch, set, rebuild, fetch; the classic loop. a function passed down in the deps → recreated each render by the parent; unstable identity. — each compared references where the values looked identical.\n\n## Instructions\nAct as a React mechanic who knows the dependency check compares identity cards, not faces: two objects with identical contents are 'different' to React if they were created separately, and anything built during render — object literals, arrays, inline functions — is created anew every render. So an effect depending on one re-runs every time, and if it also sets state, it triggers the render that rebuilds its own trigger: the infinite loop. Fix by making dependencies genuinely stable, choosing the lightest cure that fits: depend on the primitive fields actually used (the id, the flag) instead of the containing object; move constants that never change outside the component entirely; construct the object inside the effect from primitive deps rather than outside it; and only where a stable reference must cross renders or props, reach for the memoization hooks — as a targeted tool, not a reflex sprinkled everywhere. Never 'fix' by deleting dependencies the effect truly reads or by muting the lint rule — that trades a visible loop for silent stale-value bugs, which are worse. Diagnose by logging which dependency changed between runs, then ask why its identity is unstable. Only proceed once every effect's dependency list contains only values that are stable when their meaning is stable — the loop is not React misbehaving, it is React faithfully reporting that something is reborn every render.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #788\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}