- docs/agent/external: Vercel react-best-practices and composition-patterns (MIT), Addy Osmani accessibility (MIT), Anthropic frontend-design (Apache-2.0), official Payload skill at v3.92.1 (MIT) — text only, each with its licence, SOURCE.md (repo, commit) and an override note in front of the SKILL.md (ipal-kit first, Payload fetching over swr, DESIGN.md tokens, Payload 3 not 4); manifest.json with sha256 per file - scripts/vendor-external-skills.mjs (maintainer, not shipped): fetches by full commit SHA only, rejects non-text or executable files, regenerates the manifest; --check verifies it - ipal-skills --external [names]: copies ext-* after checking every file against the manifest, downloads nothing, never touches ext-* folders without ipal-kit's SOURCE.md, removes only retired ones it installed; an unknown name stops the run before anything is copied - AGENT.md §14, ipal-setup stage 2 (optional step), README: usage, overrides, how to update the pins - test/externalSkills.test.ts: pins are SHAs, no branch fetch, checksums, text only, licences, override note, installer leaves other folders alone Co-Authored-By: Claude Opus 5.5 <[email protected]>
83 lines
2.1 KiB
Markdown
83 lines
2.1 KiB
Markdown
---
|
|
title: Don't Define Components Inside Components
|
|
impact: HIGH
|
|
impactDescription: prevents remount on every render
|
|
tags: rerender, components, remount, performance
|
|
---
|
|
|
|
## Don't Define Components Inside Components
|
|
|
|
**Impact: HIGH (prevents remount on every render)**
|
|
|
|
Defining a component inside another component creates a new component type on every render. React sees a different component each time and fully remounts it, destroying all state and DOM.
|
|
|
|
A common reason developers do this is to access parent variables without passing props. Always pass props instead.
|
|
|
|
**Incorrect (remounts on every render):**
|
|
|
|
```tsx
|
|
function UserProfile({ user, theme }) {
|
|
// Defined inside to access `theme` - BAD
|
|
const Avatar = () => (
|
|
<img
|
|
src={user.avatarUrl}
|
|
className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}
|
|
/>
|
|
)
|
|
|
|
// Defined inside to access `user` - BAD
|
|
const Stats = () => (
|
|
<div>
|
|
<span>{user.followers} followers</span>
|
|
<span>{user.posts} posts</span>
|
|
</div>
|
|
)
|
|
|
|
return (
|
|
<div>
|
|
<Avatar />
|
|
<Stats />
|
|
</div>
|
|
)
|
|
}
|
|
```
|
|
|
|
Every time `UserProfile` renders, `Avatar` and `Stats` are new component types. React unmounts the old instances and mounts new ones, losing any internal state, running effects again, and recreating DOM nodes.
|
|
|
|
**Correct (pass props instead):**
|
|
|
|
```tsx
|
|
function Avatar({ src, theme }: { src: string; theme: string }) {
|
|
return (
|
|
<img
|
|
src={src}
|
|
className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function Stats({ followers, posts }: { followers: number; posts: number }) {
|
|
return (
|
|
<div>
|
|
<span>{followers} followers</span>
|
|
<span>{posts} posts</span>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function UserProfile({ user, theme }) {
|
|
return (
|
|
<div>
|
|
<Avatar src={user.avatarUrl} theme={theme} />
|
|
<Stats followers={user.followers} posts={user.posts} />
|
|
</div>
|
|
)
|
|
}
|
|
```
|
|
|
|
**Symptoms of this bug:**
|
|
- Input fields lose focus on every keystroke
|
|
- Animations restart unexpectedly
|
|
- `useEffect` cleanup/setup runs on every parent render
|
|
- Scroll position resets inside the component
|