import { useState , useCallback } from 'react' ; export default function useControlled < V > ( controlled , defaultValue ? ) { const isControlled = controlled !== undefined ; const [ uncontrolledValue , setUncontrolledValue ] = useState ( defaultValue ) ; const setValueIfUncontrolled = useCallback ( ( v ) => { if ( ! isControlled ) { setUncontrolledValue ( v ) ; } } , [ ] ) ; const value = isControlled ? controlled : uncontrolledValue ; return [ value , setValueIfUncontrolled ] as const ; }