UNPKG

react-render-order-fixer

Version:

This library allows your react component to be re-rendered where it is needed.

69 lines (68 loc) 3.04 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var debug_1 = require("debug"); var decorate_component_with_props_1 = require("decorate-component-with-props"); var React = require("react"); var re_render_trigger_1 = require("./component/re-render-trigger"); var action_emitter_creator_1 = require("./util/action-emitter-creator"); var d = debug_1.default('react-render-order-fixer'); function createRenderOrderFixer(options) { if (options === void 0) { options = {}; } var alwaysUpdate = options.alwaysUpdate; var emitter = action_emitter_creator_1.default(); function withOrderFixer(Comp) { return /** @class */ (function (_super) { __extends(ComponentWithOrderFixer, _super); function ComponentWithOrderFixer() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.handleAction = function () { d('Force update component: %s', Comp.displayName); _this.forceUpdate(); }; return _this; } ComponentWithOrderFixer.prototype.componentWillUnmount = function () { emitter.removeListener(this.handleAction); }; ComponentWithOrderFixer.prototype.componentDidMount = function () { emitter.addListener(this.handleAction); }; // Only update <Comp/> when `this.forceUpdate()` called ComponentWithOrderFixer.prototype.shouldComponentUpdate = function () { return !!alwaysUpdate; }; ComponentWithOrderFixer.prototype.render = function () { d('Component %s rendered', Comp.displayName); return (React.createElement(Comp, __assign({}, this.props))); }; return ComponentWithOrderFixer; }(React.Component)); } return { withOrderFixer: withOrderFixer, ReRenderTrigger: decorate_component_with_props_1.default(re_render_trigger_1.default, { emitter: emitter }), triggerAction: function () { d('Triggered by triggerAction()'); emitter.fireAction(); } }; } exports.default = createRenderOrderFixer;