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/rerender-no-inline-components.md
1---2title: Don't Define Components Inside Components3impact: HIGH4impactDescription: prevents remount on every render5tags: rerender, components, remount, performance6---78## Don't Define Components Inside Components910**Impact: HIGH (prevents remount on every render)**1112Defining 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.1314A common reason developers do this is to access parent variables without passing props. Always pass props instead.1516**Incorrect (remounts on every render):**1718```tsx19function UserProfile({ user, theme }) {20// Defined inside to access `theme` - BAD21const Avatar = () => (22<img23src={user.avatarUrl}24className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}25/>26)2728// Defined inside to access `user` - BAD29const Stats = () => (30<div>31<span>{user.followers} followers</span>32<span>{user.posts} posts</span>33</div>34)3536return (37<div>38<Avatar />39<Stats />40</div>41)42}43```4445Every 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.4647**Correct (pass props instead):**4849```tsx50function Avatar({ src, theme }: { src: string; theme: string }) {51return (52<img53src={src}54className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}55/>56)57}5859function Stats({ followers, posts }: { followers: number; posts: number }) {60return (61<div>62<span>{followers} followers</span>63<span>{posts} posts</span>64</div>65)66}6768function UserProfile({ user, theme }) {69return (70<div>71<Avatar src={user.avatarUrl} theme={theme} />72<Stats followers={user.followers} posts={user.posts} />73</div>74)75}76```7778**Symptoms of this bug:**79- Input fields lose focus on every keystroke80- Animations restart unexpectedly81- `useEffect` cleanup/setup runs on every parent render82- Scroll position resets inside the component83