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-memo-with-default-value.md
1---23title: Extract Default Non-primitive Parameter Value from Memoized Component to Constant4impact: MEDIUM5impactDescription: restores memoization by using a constant for default value6tags: rerender, memo, optimization78---910## Extract Default Non-primitive Parameter Value from Memoized Component to Constant1112When memoized component has a default value for some non-primitive optional parameter, such as an array, function, or object, calling the component without that parameter results in broken memoization. This is because new value instances are created on every rerender, and they do not pass strict equality comparison in `memo()`.1314To address this issue, extract the default value into a constant.1516**Incorrect (`onClick` has different values on every rerender):**1718```tsx19const UserAvatar = memo(function UserAvatar({ onClick = () => {} }: { onClick?: () => void }) {20// ...21})2223// Used without optional onClick24<UserAvatar />25```2627**Correct (stable default value):**2829```tsx30const NOOP = () => {};3132const UserAvatar = memo(function UserAvatar({ onClick = NOOP }: { onClick?: () => void }) {33// ...34})3536// Used without optional onClick37<UserAvatar />38```39