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.md
1---2title: Extract to Memoized Components3impact: MEDIUM4impactDescription: enables early returns5tags: rerender, memo, useMemo, optimization6---78## Extract to Memoized Components910Extract expensive work into memoized components to enable early returns before computation.1112**Incorrect (computes avatar even when loading):**1314```tsx15function Profile({ user, loading }: Props) {16const avatar = useMemo(() => {17const id = computeAvatarId(user)18return <Avatar id={id} />19}, [user])2021if (loading) return <Skeleton />22return <div>{avatar}</div>23}24```2526**Correct (skips computation when loading):**2728```tsx29const UserAvatar = memo(function UserAvatar({ user }: { user: User }) {30const id = useMemo(() => computeAvatarId(user), [user])31return <Avatar id={id} />32})3334function Profile({ user, loading }: Props) {35if (loading) return <Skeleton />36return (37<div>38<UserAvatar user={user} />39</div>40)41}42```4344**Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, manual memoization with `memo()` and `useMemo()` is not necessary. The compiler automatically optimizes re-renders.45