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.

786 B
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */
import { useState } from "react";

type EqualityFn<T> = (a: T, b: T) => boolean;

const defaultEqualityFn = <T>(a: T, b: T) => a === b;

/**
 * A hook that holds state that is synced with outside state.
 * If the outside state changes, the internal state is updated.
 */
export function useInternalStateWithSync<T>(
  initial: T,
  equalityFn: EqualityFn<T> = defaultEqualityFn,
) {
  const [state, setState] = useState(() => initial);
  const [previousInitial, setPreviousInitial] = useState(() => initial);

  // Whenever the outside state changes, update the internal state
  if (!equalityFn(previousInitial, initial)) {
    setState(() => initial);
    setPreviousInitial(() => initial);
  }

  return [state, setState] as const;
}