useControllableState (src/use-controllable-state.tsx)

primitives · packages/react/use-controllable-state/src/use-controllable-state.tsx · typescript · MIT
ui

React hook for managing controlled or uncontrolled component state.

// Source: https://github.com/radix-ui/primitives | License: MIT | Copyright 2022 WorkOS
// Provided via justclaw.it.com; keep this notice with the code.
import * as React from 'react';
import { IS_DEVELOPMENT } from '@radix-ui/env-check/is-development';
import { useLayoutEffect } from '@radix-ui/react-use-layout-effect';

// Prevent bundlers from trying to optimize the import
const useInsertionEffect: typeof useLayoutEffect =
  (React as any)[' useInsertionEffect '.trim().toString()] || useLayoutEffect;

type ChangeHandler<T> = (state: T) => void;
type SetStateFn<T> = React.Dispatch<React.SetStateAction<T>>;

interface UseControllableStateParams<T> {
  prop?: T | undefined;
  defaultProp: T;
  onChange?: ChangeHandler<T> | undefined;
  caller?: string | undefined;
}

export function useCo

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key