refun
Version:
A collection of React Hook-enabled functions that compose harmoniously with each other. Similar to `recompose`, but:
43 lines (37 loc) • 1.55 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { useContext, useRef, useEffect, useState } from 'react';
import { EMPTY_OBJECT, EMPTY_ARRAY, NOOP } from 'tsfn';
import { shallowEqualByKeys } from './utils';
export var ReduxStateFactory = function ReduxStateFactory(context) {
return function (mapStateToProps, stateKeysToWatch) {
return function (props) {
var _useState = useState(EMPTY_OBJECT),
_useState2 = _slicedToArray(_useState, 2),
rerender = _useState2[1];
var prevStateRef = useRef(EMPTY_OBJECT);
var prevStatePropsRef = useRef(EMPTY_OBJECT);
var store = useContext(context);
var onMountRef = useRef(NOOP);
if (prevStatePropsRef.current === EMPTY_OBJECT) {
var newState = store.getState();
prevStateRef.current = newState;
prevStatePropsRef.current = mapStateToProps(newState);
}
if (onMountRef.current === NOOP) {
onMountRef.current = function () {
return store.subscribe(function () {
var newState = store.getState();
if (!shallowEqualByKeys(prevStateRef.current, newState, stateKeysToWatch)) {
prevStatePropsRef.current = mapStateToProps(newState);
rerender({});
}
prevStateRef.current = newState;
});
};
}
useEffect(onMountRef.current, EMPTY_ARRAY);
return Object.assign({}, props, prevStatePropsRef.current);
};
};
};
//# sourceMappingURL=redux-state-factory.js.map