renderProp.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.renderCar = void 0;
  4. var tslib_1 = require("tslib");
  5. var React = tslib_1.__importStar(require("react"));
  6. var react_1 = require("react");
  7. function renderCar(WrappedComponent, defaults) {
  8. function State(_a) {
  9. var stateRef = _a.stateRef, props = _a.props;
  10. var renderTarget = (0, react_1.useCallback)(function SideTarget() {
  11. var args = [];
  12. for (var _i = 0; _i < arguments.length; _i++) {
  13. args[_i] = arguments[_i];
  14. }
  15. (0, react_1.useLayoutEffect)(function () {
  16. stateRef.current(args);
  17. });
  18. return null;
  19. }, []);
  20. // @ts-ignore
  21. return React.createElement(WrappedComponent, tslib_1.__assign({}, props, { children: renderTarget }));
  22. }
  23. var Children = React.memo(function (_a) {
  24. var stateRef = _a.stateRef, defaultState = _a.defaultState, children = _a.children;
  25. var _b = (0, react_1.useState)(defaultState.current), state = _b[0], setState = _b[1];
  26. (0, react_1.useEffect)(function () {
  27. stateRef.current = setState;
  28. }, []);
  29. return children.apply(void 0, state);
  30. }, function () { return true; });
  31. return function Combiner(props) {
  32. var defaultState = React.useRef(defaults(props));
  33. var ref = React.useRef(function (state) { return (defaultState.current = state); });
  34. return (React.createElement(React.Fragment, null,
  35. React.createElement(State, { stateRef: ref, props: props }),
  36. React.createElement(Children, { stateRef: ref, defaultState: defaultState, children: props.children })));
  37. };
  38. }
  39. exports.renderCar = renderCar;