Skip to content

CanvasAnvil is an open source AI diagram maker and flowchart generator - an infinite-canvas diagram editor and self-hosted drawio alternative that writes editable .drawio files. Text to diagram, then fix nodes, layout and labels in place. Also AI presentations, interior design CAD/BOM, posters and infographics.

721 B
Raw Blame
import { useEffect, useRef, type MutableRefObject } from "react";

/**
 * Keeps a ref pointing at the newest value.
 *
 * For long-lived effects — an event listener registered once on mount, a
 * timer, a subscription — that need the current value of something without
 * re-registering when it changes. Adding the value to the dependency array
 * would tear the subscription down and rebuild it on every change, which for
 * these is worse than the staleness it fixes.
 *
 * Read it as `ref.current` inside the callback, never during render.
 */
export function useLatestRef<T>(value: T): MutableRefObject<T> {
  const ref = useRef(value);
  useEffect(() => {
    ref.current = value;
  }, [value]);
  return ref;
}