@tremendous/help
    Preparing search index...

    Function useLatestRef

    • 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.

      Type Parameters

      • T

      Parameters

      • value: T

        The value to track.

      Returns RefObject<T>

      A stable ref whose .current follows the latest committed value.

      const onSuccessRef = useLatestRef(onSuccess);

      useEffect(() => {
      const id = setInterval(() => onSuccessRef.current(), 1000);
      return () => clearInterval(id);
      }, [onSuccessRef]);