UNPKG

refun

Version:

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

44 lines (34 loc) 1.34 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.ReduxStateFactory = void 0; var _react = require("react"); var _tsfn = require("tsfn"); var _utils = require("./utils"); const ReduxStateFactory = context => (mapStateToProps, stateKeysToWatch) => props => { const [, rerender] = (0, _react.useState)(_tsfn.EMPTY_OBJECT); const prevStateRef = (0, _react.useRef)(_tsfn.EMPTY_OBJECT); const prevStatePropsRef = (0, _react.useRef)(_tsfn.EMPTY_OBJECT); const store = (0, _react.useContext)(context); const onMountRef = (0, _react.useRef)(_tsfn.NOOP); if (prevStatePropsRef.current === _tsfn.EMPTY_OBJECT) { const newState = store.getState(); prevStateRef.current = newState; prevStatePropsRef.current = mapStateToProps(newState); } if (onMountRef.current === _tsfn.NOOP) { onMountRef.current = () => store.subscribe(() => { const newState = store.getState(); if (!(0, _utils.shallowEqualByKeys)(prevStateRef.current, newState, stateKeysToWatch)) { prevStatePropsRef.current = mapStateToProps(newState); rerender({}); } prevStateRef.current = newState; }); } (0, _react.useEffect)(onMountRef.current, _tsfn.EMPTY_ARRAY); return { ...props, ...prevStatePropsRef.current }; }; exports.ReduxStateFactory = ReduxStateFactory;