Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/advanced-init-once.md
1---2title: Initialize App Once, Not Per Mount3impact: LOW-MEDIUM4impactDescription: avoids duplicate init in development5tags: initialization, useEffect, app-startup, side-effects6---78## Initialize App Once, Not Per Mount910Do 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.1112**Incorrect (runs twice in dev, re-runs on remount):**1314```tsx15function Comp() {16useEffect(() => {17loadFromStorage()18checkAuthToken()19}, [])2021// ...22}23```2425**Correct (once per app load):**2627```tsx28let didInit = false2930function Comp() {31useEffect(() => {32if (didInit) return33didInit = true34loadFromStorage()35checkAuthToken()36}, [])3738// ...39}40```4142Reference: [Initializing the application](https://react.dev/learn/you-might-not-need-an-effect#initializing-the-application)43