reactHooks.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. if (typeof b !== "function" && b !== null)
  10. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  11. extendStatics(d, b);
  12. function __() { this.constructor = d; }
  13. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  14. };
  15. })();
  16. import * as React from 'react';
  17. import { RediError, } from '@wendellhu/redi';
  18. import { RediContext } from './reactContext';
  19. var HooksNotInRediContextError = /** @class */ (function (_super) {
  20. __extends(HooksNotInRediContextError, _super);
  21. function HooksNotInRediContextError() {
  22. return _super.call(this, 'Using dependency injection outside of a RediContext.') || this;
  23. }
  24. return HooksNotInRediContextError;
  25. }(RediError));
  26. export function useInjector() {
  27. var injectionContext = React.useContext(RediContext);
  28. if (!injectionContext.injector) {
  29. throw new HooksNotInRediContextError();
  30. }
  31. return injectionContext.injector;
  32. }
  33. export function useDependency(id, quantityOrLookUp, lookUp) {
  34. var injector = useInjector();
  35. return injector.get(id, quantityOrLookUp, lookUp);
  36. }
  37. //# sourceMappingURL=reactHooks.js.map