Custom hook that keeps a stable ref pointing at the latest value.
Replaces the const ref = useRef(value); ref.current = value; pattern, whose
render-phase write can leak values from renders React discards (flagged by
react-doctor's no-ref-current-in-render and rejected by the React Compiler).
The write happens in a layout effect, so the ref is up to date before any
event handler, timer, or passive effect reads it. Don't read it during render
or inside another layout effect — use the value itself there instead.
Custom hook that keeps a stable ref pointing at the latest value.
Replaces the
const ref = useRef(value); ref.current = value;pattern, whose render-phase write can leak values from renders React discards (flagged by react-doctor's no-ref-current-in-render and rejected by the React Compiler). The write happens in a layout effect, so the ref is up to date before any event handler, timer, or passive effect reads it. Don't read it during render or inside another layout effect — use the value itself there instead.