UNPKG

@modern-kit/react

Version:
23 lines (20 loc) 557 B
import { useState, useCallback } from 'react'; function useControllableState({ value, defaultValue }) { const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue); const isControlled = value !== void 0; const currentValue = isControlled ? value : uncontrolledValue; const setValue = useCallback( (nextValue) => { if (!isControlled) { setUncontrolledValue(nextValue); } }, [isControlled] ); return [currentValue, setValue]; } export { useControllableState }; //# sourceMappingURL=index.mjs.map