AgentSkillsCN

initialize-app-once-not-per-mount

仅在应用首次挂载时初始化,而非每次挂载都重新初始化

SKILL.md
--- frontmatter
name: initialize-app-once-not-per-mount
description: Initialize App Once, Not Per Mount

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