mirror of
https://github.com/NicholaiVogel/dashore-incubator.git
synced 2026-04-24 16:54:26 +00:00
add web-perf and vercel-react-best-practices skills from skills.sh to improve ai assistance for this next.js + cloudflare workers project. - web-perf: core web vitals analysis and performance auditing - vercel-react-best-practices: react/next.js optimization patterns - wrangler: cloudflare workers cli guidance (already existed)
40 lines
973 B
Markdown
40 lines
973 B
Markdown
---
|
|
title: Defer State Reads to Usage Point
|
|
impact: MEDIUM
|
|
impactDescription: avoids unnecessary subscriptions
|
|
tags: rerender, searchParams, localStorage, optimization
|
|
---
|
|
|
|
## Defer State Reads to Usage Point
|
|
|
|
Don't subscribe to dynamic state (searchParams, localStorage) if you only read it inside callbacks.
|
|
|
|
**Incorrect (subscribes to all searchParams changes):**
|
|
|
|
```tsx
|
|
function ShareButton({ chatId }: { chatId: string }) {
|
|
const searchParams = useSearchParams()
|
|
|
|
const handleShare = () => {
|
|
const ref = searchParams.get('ref')
|
|
shareChat(chatId, { ref })
|
|
}
|
|
|
|
return <button onClick={handleShare}>Share</button>
|
|
}
|
|
```
|
|
|
|
**Correct (reads on demand, no subscription):**
|
|
|
|
```tsx
|
|
function ShareButton({ chatId }: { chatId: string }) {
|
|
const handleShare = () => {
|
|
const params = new URLSearchParams(window.location.search)
|
|
const ref = params.get('ref')
|
|
shareChat(chatId, { ref })
|
|
}
|
|
|
|
return <button onClick={handleShare}>Share</button>
|
|
}
|
|
```
|