mobx-react-lite
Version:
Lightweight React bindings for MobX based on React 16.8+ and Hooks
77 lines • 3.33 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__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;
};
return __assign.apply(this, arguments);
};
import { forwardRef, memo } from "react";
import { isUsingStaticRendering } from "./staticRendering";
import { useObserver } from "./useObserver";
// n.b. base case is not used for actual typings or exported in the typing files
export function observer(baseComponent, options) {
// The working of observer is explained step by step in this talk: https://www.youtube.com/watch?v=cPF4iBedoF0&feature=youtu.be&t=1307
if (isUsingStaticRendering()) {
return baseComponent;
}
var realOptions = __assign({ forwardRef: false }, options);
var baseComponentName = baseComponent.displayName || baseComponent.name;
var wrappedComponent = function (props, ref) {
return useObserver(function () { return baseComponent(props, ref); }, baseComponentName);
};
// Don't set `displayName` for anonymous components,
// so the `displayName` can be customized by user, see #3192.
if (baseComponentName !== "") {
wrappedComponent.displayName = baseComponentName;
}
// Support legacy context: `contextTypes` must be applied before `memo`
if (baseComponent.contextTypes) {
wrappedComponent.contextTypes = baseComponent.contextTypes;
}
// memo; we are not interested in deep updates
// in props; we assume that if deep objects are changed,
// this is in observables, which would have been tracked anyway
var memoComponent;
if (realOptions.forwardRef) {
// we have to use forwardRef here because:
// 1. it cannot go before memo, only after it
// 2. forwardRef converts the function into an actual component, so we can't let the baseComponent do it
// since it wouldn't be a callable function anymore
memoComponent = memo(forwardRef(wrappedComponent));
}
else {
memoComponent = memo(wrappedComponent);
}
copyStaticProperties(baseComponent, memoComponent);
if ("production" !== process.env.NODE_ENV) {
Object.defineProperty(memoComponent, "contextTypes", {
set: function () {
var _a;
throw new Error("[mobx-react-lite] `".concat(this.displayName || ((_a = this.type) === null || _a === void 0 ? void 0 : _a.displayName) || "Component", ".contextTypes` must be set before applying `observer`."));
}
});
}
return memoComponent;
}
// based on https://github.com/mridgway/hoist-non-react-statics/blob/master/src/index.js
var hoistBlackList = {
$$typeof: true,
render: true,
compare: true,
type: true,
// Don't redefine `displayName`,
// it's defined as getter-setter pair on `memo` (see #3192).
displayName: true
};
function copyStaticProperties(base, target) {
Object.keys(base).forEach(function (key) {
if (!hoistBlackList[key]) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(base, key));
}
});
}
//# sourceMappingURL=observer.js.map