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-defer-reads.md
1---2title: Defer State Reads to Usage Point3impact: MEDIUM4impactDescription: avoids unnecessary subscriptions5tags: rerender, searchParams, localStorage, optimization6---78## Defer State Reads to Usage Point910Don't subscribe to dynamic state (searchParams, localStorage) if you only read it inside callbacks.1112**Incorrect (subscribes to all searchParams changes):**1314```tsx15function ShareButton({ chatId }: { chatId: string }) {16const searchParams = useSearchParams()1718const handleShare = () => {19const ref = searchParams.get('ref')20shareChat(chatId, { ref })21}2223return <button onClick={handleShare}>Share</button>24}25```2627**Correct (reads on demand, no subscription):**2829```tsx30function ShareButton({ chatId }: { chatId: string }) {31const handleShare = () => {32const params = new URLSearchParams(window.location.search)33const ref = params.get('ref')34shareChat(chatId, { ref })35}3637return <button onClick={handleShare}>Share</button>38}39```40