Notes
← All notes
·frontend·20 min

React Hooks 邊角:closure trap、strict mode 雙呼叫、hydration mismatch

資深前端面試問 hooks 不會問 useState 怎麼用,會問「為什麼 setInterval 裡的 state 永遠不更新」、「為什麼 useEffect 跑了兩次」、「為什麼 server 跟 client render 不一樣」。三個邊角 bug 串成一條 React 心智模型;附 useMemo / useCallback 何時用、custom hook 抽法、useLayoutEffect 取捨。內部使用。

#react#hooks#closure#strict-mode#hydration#useeffect#usememo#interview-prep#面試系列#private

問「為什麼 setInterval 裡的 state 永遠不更新」、「strict mode 為什麼 useEffect 跑兩次」、「Date.now() 為什麼造成 hydration mismatch」——這三題在問同一件事:你懂不懂 hooks 的閉包模型、純度要求、SSR/CSR 一致性契約。基礎 hooks 教學不會教這些,但這些是真正 bug 的來源。