UNPKG

refun

Version:

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

28 lines (24 loc) 1.1 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import { useState, useRef } from 'react'; import { EMPTY_OBJECT, NOOP } from 'tsfn'; import { shallowEqualByKeys } from './utils'; export var mapStateRef = function mapStateRef(stateName, stateFlusherName, getValue, watchKeys) { return function (props) { var _Object$assign; var useStateResult = useState(EMPTY_OBJECT); var prevProps = useRef(EMPTY_OBJECT); var stateRef = useRef(EMPTY_OBJECT); var stateFlushRef = useRef(NOOP); if (stateFlushRef.current === NOOP) { stateFlushRef.current = function () { useStateResult[1]({}); }; } if (prevProps.current === EMPTY_OBJECT || !shallowEqualByKeys(prevProps.current, props, watchKeys)) { stateRef.current = getValue(props); } prevProps.current = props; return Object.assign({}, props, (_Object$assign = {}, _defineProperty(_Object$assign, stateName, stateRef), _defineProperty(_Object$assign, stateFlusherName, stateFlushRef.current), _Object$assign)); }; }; //# sourceMappingURL=map-state-ref.js.map