UNPKG

refun

Version:

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

35 lines (27 loc) 976 B
Object.defineProperty(exports, "__esModule", { value: true }); exports.mapStateRef = void 0; var _react = require("react"); var _tsfn = require("tsfn"); var _utils = require("./utils"); const mapStateRef = (stateName, stateFlusherName, getValue, watchKeys) => props => { const useStateResult = (0, _react.useState)(_tsfn.EMPTY_OBJECT); const prevProps = (0, _react.useRef)(_tsfn.EMPTY_OBJECT); const stateRef = (0, _react.useRef)(_tsfn.EMPTY_OBJECT); const stateFlushRef = (0, _react.useRef)(_tsfn.NOOP); if (stateFlushRef.current === _tsfn.NOOP) { stateFlushRef.current = () => { useStateResult[1]({}); }; } if (prevProps.current === _tsfn.EMPTY_OBJECT || !(0, _utils.shallowEqualByKeys)(prevProps.current, props, watchKeys)) { stateRef.current = getValue(props); } prevProps.current = props; return { ...props, [stateName]: stateRef, [stateFlusherName]: stateFlushRef.current }; }; exports.mapStateRef = mapStateRef;