| 1 | --- |
| 2 | title: Extract to Memoized Components |
| 3 | impact: MEDIUM |
| 4 | impactDescription: enables early returns |
| 5 | tags: rerender, memo, useMemo, optimization |
| 6 | --- |
| 7 | |
| 8 | ## Extract to Memoized Components |
| 9 | |
| 10 | Extract expensive work into memoized components to enable early returns before computation. |
| 11 | |
| 12 | **Incorrect (computes avatar even when loading):** |
| 13 | |
| 14 | ```tsx |
| 15 | function Profile({ user |
| 16 | const avatar = |
| 17 | const id = |
| 18 | return <Avatar |
| 19 | }, [user]); |
| 20 | |
| 21 | if (loading) return |
| 22 | return <div>{ |
| 23 | } |
| 24 | ``` |
| 25 | |
| 26 | **Correct (skips computation when loading):** |
| 27 | |
| 28 | ```tsx |
| 29 | const UserAvatar = memo |
| 30 | const id = |
| 31 | return <Avatar |
| 32 | }); |
| 33 | |
| 34 | function Profile({ user |
| 35 | if (loading) return |
| 36 | return ( |
| 37 | <div> |
| 38 | <UserAvatar user= |
| 39 | </div> |
| 40 | ); |
| 41 | } |
| 42 | ``` |
| 43 | |
| 44 | **Note:** If your project has [React Compiler |