UNPKG

4.87 kBJavaScriptView Raw
1var _a, _b;
2import { forwardRef, memo } from "react";
3import { isUsingStaticRendering } from "./staticRendering";
4import { useObserver } from "./useObserver";
5var warnObserverOptionsDeprecated = true;
6var hasSymbol = typeof Symbol === "function" && Symbol.for;
7var isFunctionNameConfigurable = (_b = (_a = Object.getOwnPropertyDescriptor(function () { }, "name")) === null || _a === void 0 ? void 0 : _a.configurable) !== null && _b !== void 0 ? _b : false;
8// Using react-is had some issues (and operates on elements, not on types), see #608 / #609
9var ReactForwardRefSymbol = hasSymbol
10 ? Symbol.for("react.forward_ref")
11 : typeof forwardRef === "function" && forwardRef(function (props) { return null; })["$$typeof"];
12var ReactMemoSymbol = hasSymbol
13 ? Symbol.for("react.memo")
14 : typeof memo === "function" && memo(function (props) { return null; })["$$typeof"];
15// n.b. base case is not used for actual typings or exported in the typing files
16export function observer(baseComponent,
17// TODO remove in next major
18options) {
19 var _a;
20 if (process.env.NODE_ENV !== "production" && warnObserverOptionsDeprecated && options) {
21 warnObserverOptionsDeprecated = false;
22 console.warn("[mobx-react-lite] `observer(fn, { forwardRef: true })` is deprecated, use `observer(React.forwardRef(fn))`");
23 }
24 if (ReactMemoSymbol && baseComponent["$$typeof"] === ReactMemoSymbol) {
25 throw new Error("[mobx-react-lite] You are trying to use `observer` on a function component wrapped in either another `observer` or `React.memo`. The observer already applies 'React.memo' for you.");
26 }
27 // The working of observer is explained step by step in this talk: https://www.youtube.com/watch?v=cPF4iBedoF0&feature=youtu.be&t=1307
28 if (isUsingStaticRendering()) {
29 return baseComponent;
30 }
31 var useForwardRef = (_a = options === null || options === void 0 ? void 0 : options.forwardRef) !== null && _a !== void 0 ? _a : false;
32 var render = baseComponent;
33 var baseComponentName = baseComponent.displayName || baseComponent.name;
34 // If already wrapped with forwardRef, unwrap,
35 // so we can patch render and apply memo
36 if (ReactForwardRefSymbol && baseComponent["$$typeof"] === ReactForwardRefSymbol) {
37 useForwardRef = true;
38 render = baseComponent["render"];
39 if (typeof render !== "function") {
40 throw new Error("[mobx-react-lite] `render` property of ForwardRef was not a function");
41 }
42 }
43 var observerComponent = function (props, ref) {
44 return useObserver(function () { return render(props, ref); }, baseComponentName);
45 };
46 observerComponent.displayName = baseComponent.displayName;
47 if (isFunctionNameConfigurable) {
48 Object.defineProperty(observerComponent, "name", {
49 value: baseComponent.name,
50 writable: true,
51 configurable: true
52 });
53 }
54 // Support legacy context: `contextTypes` must be applied before `memo`
55 if (baseComponent.contextTypes) {
56 ;
57 observerComponent.contextTypes = baseComponent.contextTypes;
58 }
59 if (useForwardRef) {
60 // `forwardRef` must be applied prior `memo`
61 // `forwardRef(observer(cmp))` throws:
62 // "forwardRef requires a render function but received a `memo` component. Instead of forwardRef(memo(...)), use memo(forwardRef(...))"
63 observerComponent = forwardRef(observerComponent);
64 }
65 // memo; we are not interested in deep updates
66 // in props; we assume that if deep objects are changed,
67 // this is in observables, which would have been tracked anyway
68 observerComponent = memo(observerComponent);
69 copyStaticProperties(baseComponent, observerComponent);
70 if ("production" !== process.env.NODE_ENV) {
71 Object.defineProperty(observerComponent, "contextTypes", {
72 set: function () {
73 var _a, _b;
74 throw new Error("[mobx-react-lite] `".concat(this.displayName || ((_a = this.type) === null || _a === void 0 ? void 0 : _a.displayName) || ((_b = this.type) === null || _b === void 0 ? void 0 : _b.name) || "Component", ".contextTypes` must be set before applying `observer`."));
75 }
76 });
77 }
78 return observerComponent;
79}
80// based on https://github.com/mridgway/hoist-non-react-statics/blob/master/src/index.js
81var hoistBlackList = {
82 $$typeof: true,
83 render: true,
84 compare: true,
85 type: true,
86 // Don't redefine `displayName`,
87 // it's defined as getter-setter pair on `memo` (see #3192).
88 displayName: true
89};
90function copyStaticProperties(base, target) {
91 Object.keys(base).forEach(function (key) {
92 if (!hoistBlackList[key]) {
93 Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(base, key));
94 }
95 });
96}
97//# sourceMappingURL=observer.js.map
\No newline at end of file