[ PROMPT_NODE_25242 ]
rerender-defer-reads
[ SKILL_DOCUMENTATION ]
## 将状态读取延迟到使用点
如果你只在回调函数内部读取动态状态(如 searchParams、localStorage),则不要订阅它。
**错误做法(订阅了所有 searchParams 的变更):**
tsx
function ShareButton({ chatId }: { chatId: string }) {
const searchParams = useSearchParams()
const handleShare = () => {
const ref = searchParams.get('ref')
shareChat(chatId, { ref })
}
return
}
**正确做法(按需读取,无订阅):**
tsx
function ShareButton({ chatId }: { chatId: string }) {
const handleShare = () => {
const params = new URLSearchParams(window.location.search)
const ref = params.get('ref')
shareChat(chatId, { ref })
}
return
}