Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

2.2 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */
import { debounce } from "lodash-es";
import { useEffect, useMemo, useState } from "react";
import useEvent from "react-use-event-hook";
import { useInternalStateWithSync } from "./useInternalStateWithSync";

export function useDebounce<T>(value: T, delay: number): T {
  const [debouncedValue, setDebouncedValue] = useState<T>(value);

  useEffect(() => {
    const timer = setTimeout(() => setDebouncedValue(value), delay);

    return () => {
      clearTimeout(timer);
    };
  }, [value, delay]);

  return debouncedValue;
}

/**
 * A hook that allows you to control a state value, but also get a debounced version of that value.
 */
export function useDebounceControlledState<T>(opts: {
  /**
   * Initial value of the state.
   */
  initialValue: T;
  /**
   * Callback to run when the state changes.
   * This will be debounced.
   */
  onChange: (value: T) => void;
  /**
   * The delay to debounce the state change by.
   * @default 200
   */
  delay?: number;
  /**
   * Whether the state is disabled.
   */
  disabled?: boolean;
}) {
  const { initialValue, onChange, delay, disabled } = opts;
  const [internalValue, setInternalValue] = useInternalStateWithSync(
    initialValue,
    Object.is,
  );
  const debouncedValue = useDebounce(internalValue, delay || 200);

  const onUpdate = useEvent(onChange);

  // Handle debounced updates for user input
  useEffect(() => {
    if (disabled) {
      return;
    }
    // Only trigger debounced update if the value is different from initialValue
    if (debouncedValue !== initialValue) {
      onUpdate(debouncedValue);
    }
  }, [debouncedValue, disabled, onUpdate]);

  // If disabled, just pass through the initialValue and onChange
  if (disabled) {
    return {
      value: internalValue,
      debouncedValue: internalValue,
      onChange: onChange,
    };
  }

  return {
    value: internalValue,
    debouncedValue,
    onChange: setInternalValue,
  };
}

export function useDebouncedCallback<T extends (...args: never[]) => unknown>(
  callback: T,
  delay: number,
) {
  const internalCallback = useEvent(callback);
  return useMemo(() => {
    return debounce(internalCallback, delay);
  }, [internalCallback, delay]);
}