UNPKG

@hitachivantara/uikit-react-core

Version:

Core React components for the NEXT Design System.

19 lines (18 loc) 534 B
import { useRef, useState, useCallback } from "react"; const useControlled = (controlledProp, initialState) => { const { current: isControlled } = useRef(controlledProp !== void 0); const [valueState, setValue] = useState(initialState); const value = isControlled ? controlledProp : valueState; const setValueIfUncontrolled = useCallback( (newValue) => { if (!isControlled) { setValue(newValue); } }, [isControlled] ); return [value, setValueIfUncontrolled]; }; export { useControlled };