[ PROMPT_NODE_27974 ]
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
}