UNPKG

react95-native

Version:

Refreshed Windows 95 style UI components for your React Native app

24 lines (19 loc) 594 B
import { useState, useCallback } from 'react'; import type { AnyValue } from '../types'; type Props = { value: AnyValue; defaultValue: AnyValue; }; export default ({ value, defaultValue, }: Props): [AnyValue, (newValue: AnyValue) => void] => { const isControlled = value !== undefined; const [controlledValue, setControlledValue] = useState(defaultValue); const handleChangeIfUncontrolled = useCallback(newValue => { if (!isControlled) { setControlledValue(newValue); } }, []); return [isControlled ? value : controlledValue, handleChangeIfUncontrolled]; };