UNPKG

@liveblocks/react-ui

Version:

A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.

33 lines (30 loc) 1.12 kB
import { console } from '@liveblocks/core'; import { useState, useRef, useEffect, useCallback } from 'react'; function useControllableState(value, onChange, defaultValue) { const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue); const isControlled = value !== void 0; const wasControlled = useRef(isControlled); useEffect(() => { if (process.env.NODE_ENV !== "production" && wasControlled.current !== isControlled) { console.warn( `A component is changing from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.` ); } wasControlled.current = isControlled; }, [isControlled]); const currentValue = isControlled ? value : uncontrolledValue; const setValue = useCallback( (value2) => { if (isControlled) { return onChange?.(value2); } else { setUncontrolledValue(value2); return onChange?.(value2); } }, [isControlled, onChange] ); return [currentValue, setValue]; } export { useControllableState }; //# sourceMappingURL=use-controllable-state.js.map