use-store-hooks
Version:
Create a redux-like store using hooks. Supports middleware
47 lines (35 loc) • 2.94 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _State = _interopRequireDefault(require("./State"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
/**
*
* @param {Function} mapState takes the state and own props
* @param {Function} mapDispatch takes dispatch and current props
* @return {Node} A react component with additional props
*/
var connect = function connect() {
var mapState = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
var mapDispatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
return function (ConnectedComponent) {
return function (_ref) {
var props = _extends({}, _ref);
var _useContext = (0, _react.useContext)(_State["default"]),
state = _useContext.state,
dispatch = _useContext.dispatch;
return /*#__PURE__*/_react["default"].createElement(ConnectedComponent, _extends({}, props, mapState(state, props), mapDispatch(dispatch, props), {
dispatch: dispatch
}));
};
};
};
var _default = connect;
exports["default"] = _default;