UNPKG

@appbuckets/react-ui-core

Version:

Core utilities built for AppBuckets React UI Framework

52 lines (49 loc) 1.43 kB
import { __read } from 'tslib'; import * as React from 'react'; function useAutoControlledValue(initialState, config) { var _a = config !== null && config !== void 0 ? config : {}, prop = _a.prop, defaultProp = _a.defaultProp; var _b = __read( React.useState( prop === undefined ? defaultProp === undefined ? initialState : defaultProp : prop ), 2 ), state = _b[0], setState = _b[1]; // Counterpart to the `static getDerivedStateFromProps` method, but for one key only. // When `prop` has changed since last render, update `state` with the `prop`'s value. // https://reactjs.org/docs/hooks-faq.html#how-do-i-implement-getderivedstatefromprops var getDerivedStateFromProps = React.useCallback( function () { if (prop === undefined || prop === state) { return; } setState(prop); }, [state, prop, setState] ); // Attempt to modify the `state` value internally. // When `prop` has already been provided, defer to it and don't update `state`. var trySetState = React.useCallback( function (newState) { if (prop !== undefined) { return; } setState(newState); }, [prop, setState] ); return [ prop === undefined ? state : prop, trySetState, setState, getDerivedStateFromProps, ]; } export { useAutoControlledValue as default };