advanced-init-once — Claude Code rule: what it does, how to install
advanced-init-once prevents running app-wide startup logic inside component useEffect([]) hooks and suggests module-level guards or top-level initialization instead.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/advanced-init-once.mdWhat advanced-init-once does
This rule stops the agent from placing app-wide startup logic inside a component's useEffect([]). React components remount and re-run effects, causing startup routines like token checks or storage reads to fire twice in development.
The instruction directs Claude Code to either place one-time initialization at the entry module level or guard it with a module-level variable like didInit. This ensures functions such as checkAuthToken() execute strictly once per application lifecycle.
Without a module-level guard, React's development mode will trigger the effect multiple times on mount.
What it helps with
- prevent duplicate startup logic when components remount
- stop double authorization checks during development
- move application initialization to the entry module level
- guard component
useEffecthooks with a module-level boolean
Example prompts for the agent
- Check the app initialization in the auth component and prevent duplicate runs
- Move startup data loading out of
useEffectto the entry module - Add a module-level guard for
checkAuthToken()on component remount
How to install advanced-init-once
Manually: put the files in place
.claude/rules/advanced-init-once.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/advanced-init-once.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/advanced-init-once.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/advanced-init-once.mdManually: put the files in place
.clinerules/advanced-init-once.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about advanced-init-once
How to install advanced-init-once in Claude Code?
Save the rule into .claude/rules/advanced-init-once.md in your project root.
What does the advanced-init-once rule do?
It prevents Claude Code from placing app-wide startup code inside component useEffect hooks with empty dependency arrays. This avoids redundant network calls and token checks during remounts.
How does the rule handle initialization instead of useEffect?
It advises placing the setup code at the entry module top level or setting a module-level guard like didInit = true inside the effect.
Source text
advanced-init-once.md · MIT
---
title: Initialize App Once, Not Per Mount
impact: LOW-MEDIUM
impactDescription: avoids duplicate init in development
tags: initialization, useEffect, app-startup, side-effects
---
## Initialize App Once, Not Per Mount
Do not put app-wide initialization that must run once per app load inside `useEffect([])` of a component. Components can remount and effects will re-run. Use a module-level guard or top-level init in the entry module instead.
**Incorrect (runs twice in dev, re-runs on remount):**
```tsx
function Comp() {
useEffect(() => {
loadFromStorage()
checkAuthToken()
}, [])
// ...
}
```
**Correct (once per app load):**
```tsx
let didInit = false
function Comp() {
useEffect(() => {
if (didInit) return
didInit = true
loadFromStorage()
checkAuthToken()
}, [])
// ...
}
```
Reference: [Initializing the application](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)
© tech-leads-club · MIT · tech-leads-club/agent-skills