reshow-flux
Version:
The smallest react flux and Fast hook alternative
37 lines (35 loc) • 1.03 kB
JavaScript
;
exports.__esModule = true;
exports["default"] = void 0;
var _react = require("react");
var _reshowFluxBase = require("reshow-flux-base");
// @ts-check
/**
* useReducer alternative for External store.
* https://reactjs.org/docs/hooks-reference.html#usereducer
*
*
* ## Base usage
*
* const [store, dispatch] = useReduceStore(reduceCallback, initial[Map|Function]);
*
* Call dispatch will not trigger re-render.
*
*
* @template StateType
* @template ActionType
* @param {import('reshow-flux-base').ReducerType<StateType, ActionType>} reducer
* @param {import('reshow-flux-base').InitStateType<StateType>} [initialState]
*/
var useReduceStore = function useReduceStore(reducer, initialState) {
/**
* @type any
*/
var lastReducer = (0, _react.useRef)();
if (!lastReducer.current) {
lastReducer.current = (0, _reshowFluxBase.createReducer)(reducer, initialState);
}
return lastReducer.current;
};
var _default = exports["default"] = useReduceStore;
module.exports = exports.default;