Created
February 21, 2019 12:51
-
-
Save eps1lon/47109f9734892b1abf80711b396910c8 to your computer and use it in GitHub Desktop.
useControlled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useIsControlled(value) { | |
const { current: isControlled } = React.useRef(value != null); | |
return isControlled; | |
} | |
function useControlled(config) { | |
const { value, defaultValue, setControlledValue } = config; | |
const isControlled = useIsControlled(value); | |
const [state, setState] = React.useState(defaultValue); | |
return isControlled ? [value, setControlledValue] : [state, setState]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment