Rooks
Utilities & Refs

useFreshCallback

Returns a stable callback reference that always calls the latest version of the function.

About

Avoid stale closures and keep your callback fresh.

Examples

Call the latest callback after state changes

import { useState } from "react";
import { useFreshCallback } from "rooks";

export default function App() {
  const [count, setCount] = useState(0);
  const logLatestCount = useFreshCallback(() => {
    console.log(`Latest count: ${count}`);
  });

  return (
    <div>
      <button onClick={() => setCount((current) => current + 1)}>
        Increment
      </button>
      <button onClick={logLatestCount}>Log latest count</button>
    </div>
  );
}

Keep an event handler in sync with changing props

import { useEffect } from "react";
import { useFreshCallback } from "rooks";

function ResizeLogger({ label }: { label: string }) {
  const handleResize = useFreshCallback(() => {
    console.log(`${label}: ${window.innerWidth}`);
  });

  useEffect(() => {
    window.addEventListener("resize", handleResize);
    return () => window.removeEventListener("resize", handleResize);
  }, [handleResize]);

  return null;
}

On this page