UNPKG

refun

Version:

A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:

32 lines (24 loc) 859 B
Object.defineProperty(exports, "__esModule", { value: true }); exports.mapState = void 0; var _react = require("react"); var _tsfn = require("tsfn"); var _utils = require("./utils"); const mapState = (stateName, stateSetterName, getValue, watchKeys) => props => { const prevProps = (0, _react.useRef)(_tsfn.EMPTY_OBJECT); const [state, setState] = (0, _react.useState)(prevProps.current === _tsfn.EMPTY_OBJECT ? getValue(props) : _tsfn.EMPTY_OBJECT); let nextState = state; if (prevProps.current !== _tsfn.EMPTY_OBJECT && !(0, _utils.shallowEqualByKeys)(prevProps.current, props, watchKeys)) { nextState = getValue(props); if (nextState !== state) { setState(nextState); } } prevProps.current = props; return { ...props, [stateName]: nextState, [stateSetterName]: setState }; }; exports.mapState = mapState;