mobx-react 7.6.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,52 +6,26 @@
6
6
 
7
7
  var React__default = 'default' in React ? React['default'] : React;
8
8
 
9
- var symbolId = 0;
10
-
11
- function createSymbol(name) {
12
- if (typeof Symbol === "function") {
13
- return Symbol(name);
14
- }
15
-
16
- var symbol = "__$mobx-react " + name + " (" + symbolId + ")";
17
- symbolId++;
18
- return symbol;
19
- }
20
-
21
- var createdSymbols = {};
22
- function newSymbol(name) {
23
- if (!createdSymbols[name]) {
24
- createdSymbols[name] = createSymbol(name);
25
- }
26
-
27
- return createdSymbols[name];
28
- }
29
9
  function shallowEqual(objA, objB) {
30
10
  //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
31
11
  if (is(objA, objB)) {
32
12
  return true;
33
13
  }
34
-
35
14
  if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
36
15
  return false;
37
16
  }
38
-
39
17
  var keysA = Object.keys(objA);
40
18
  var keysB = Object.keys(objB);
41
-
42
19
  if (keysA.length !== keysB.length) {
43
20
  return false;
44
21
  }
45
-
46
22
  for (var i = 0; i < keysA.length; i++) {
47
23
  if (!Object.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
48
24
  return false;
49
25
  }
50
26
  }
51
-
52
27
  return true;
53
28
  }
54
-
55
29
  function is(x, y) {
56
30
  // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
57
31
  if (x === y) {
@@ -59,9 +33,8 @@
59
33
  } else {
60
34
  return x !== x && y !== y;
61
35
  }
62
- } // based on https://github.com/mridgway/hoist-non-react-statics/blob/master/src/index.js
63
-
64
-
36
+ }
37
+ // based on https://github.com/mridgway/hoist-non-react-statics/blob/master/src/index.js
65
38
  var hoistBlackList = {
66
39
  $$typeof: 1,
67
40
  render: 1,
@@ -86,33 +59,12 @@
86
59
  }
87
60
  });
88
61
  }
89
- /**
90
- * Helper to set `prop` to `this` as non-enumerable (hidden prop)
91
- * @param target
92
- * @param prop
93
- * @param value
94
- */
95
-
96
- function setHiddenProp(target, prop, value) {
97
- if (!Object.hasOwnProperty.call(target, prop)) {
98
- Object.defineProperty(target, prop, {
99
- enumerable: false,
100
- configurable: true,
101
- writable: true,
102
- value: value
103
- });
104
- } else {
105
- target[prop] = value;
106
- }
107
- }
108
62
  /**
109
63
  * Utilities for patching componentWillUnmount, to make sure @disposeOnUnmount works correctly icm with user defined hooks
110
64
  * and the handler provided by mobx-react
111
65
  */
112
-
113
- var mobxMixins = /*#__PURE__*/newSymbol("patchMixins");
114
- var mobxPatchedDefinition = /*#__PURE__*/newSymbol("patchedDefinition");
115
-
66
+ var mobxMixins = /*#__PURE__*/Symbol("patchMixins");
67
+ var mobxPatchedDefinition = /*#__PURE__*/Symbol("patchedDefinition");
116
68
  function getMixins(target, methodName) {
117
69
  var mixins = target[mobxMixins] = target[mobxMixins] || {};
118
70
  var methodMixins = mixins[methodName] = mixins[methodName] || {};
@@ -120,28 +72,21 @@
120
72
  methodMixins.methods = methodMixins.methods || [];
121
73
  return methodMixins;
122
74
  }
123
-
124
75
  function wrapper(realMethod, mixins) {
125
76
  var _this = this;
126
-
127
77
  for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
128
78
  args[_key - 2] = arguments[_key];
129
79
  }
130
-
131
80
  // locks are used to ensure that mixins are invoked only once per invocation, even on recursive calls
132
81
  mixins.locks++;
133
-
134
82
  try {
135
83
  var retVal;
136
-
137
84
  if (realMethod !== undefined && realMethod !== null) {
138
85
  retVal = realMethod.apply(this, args);
139
86
  }
140
-
141
87
  return retVal;
142
88
  } finally {
143
89
  mixins.locks--;
144
-
145
90
  if (mixins.locks === 0) {
146
91
  mixins.methods.forEach(function (mx) {
147
92
  mx.apply(_this, args);
@@ -149,41 +94,31 @@
149
94
  }
150
95
  }
151
96
  }
152
-
153
97
  function wrapFunction(realMethod, mixins) {
154
98
  var fn = function fn() {
155
99
  for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
156
100
  args[_key2] = arguments[_key2];
157
101
  }
158
-
159
102
  wrapper.call.apply(wrapper, [this, realMethod, mixins].concat(args));
160
103
  };
161
-
162
104
  return fn;
163
105
  }
164
-
165
106
  function patch(target, methodName, mixinMethod) {
166
107
  var mixins = getMixins(target, methodName);
167
-
168
108
  if (mixins.methods.indexOf(mixinMethod) < 0) {
169
109
  mixins.methods.push(mixinMethod);
170
110
  }
171
-
172
111
  var oldDefinition = Object.getOwnPropertyDescriptor(target, methodName);
173
-
174
112
  if (oldDefinition && oldDefinition[mobxPatchedDefinition]) {
175
113
  // already patched definition, do not repatch
176
114
  return;
177
115
  }
178
-
179
116
  var originalMethod = target[methodName];
180
117
  var newDefinition = createDefinition(target, methodName, oldDefinition ? oldDefinition.enumerable : undefined, mixins, originalMethod);
181
118
  Object.defineProperty(target, methodName, newDefinition);
182
119
  }
183
-
184
120
  function createDefinition(target, methodName, enumerable, mixins, originalMethod) {
185
121
  var _ref;
186
-
187
122
  var wrappedFunc = wrapFunction(originalMethod, mixins);
188
123
  return _ref = {}, _ref[mobxPatchedDefinition] = true, _ref.get = function get() {
189
124
  return wrappedFunc;
@@ -201,247 +136,211 @@
201
136
  }, _ref.configurable = true, _ref.enumerable = enumerable, _ref;
202
137
  }
203
138
 
204
- var mobxAdminProperty = mobx.$mobx || "$mobx"; // BC
205
-
206
- var mobxObserverProperty = /*#__PURE__*/newSymbol("isMobXReactObserver");
207
- var mobxIsUnmounted = /*#__PURE__*/newSymbol("isUnmounted");
208
- var skipRenderKey = /*#__PURE__*/newSymbol("skipRender");
209
- var isForcingUpdateKey = /*#__PURE__*/newSymbol("isForcingUpdate");
139
+ var administrationSymbol = /*#__PURE__*/Symbol("ObserverAdministration");
140
+ var isMobXReactObserverSymbol = /*#__PURE__*/Symbol("isMobXReactObserver");
141
+ var observablePropDescriptors;
142
+ {
143
+ observablePropDescriptors = {
144
+ props: /*#__PURE__*/createObservablePropDescriptor("props"),
145
+ state: /*#__PURE__*/createObservablePropDescriptor("state"),
146
+ context: /*#__PURE__*/createObservablePropDescriptor("context")
147
+ };
148
+ }
149
+ function getAdministration(component) {
150
+ var _component$administra;
151
+ // We create administration lazily, because we can't patch constructor
152
+ // and the exact moment of initialization partially depends on React internals.
153
+ // At the time of writing this, the first thing invoked is one of the observable getter/setter (state/props/context).
154
+ return (_component$administra = component[administrationSymbol]) != null ? _component$administra : component[administrationSymbol] = {
155
+ reaction: null,
156
+ mounted: false,
157
+ forceUpdate: null,
158
+ name: getDisplayName(component.constructor),
159
+ state: undefined,
160
+ props: undefined,
161
+ context: undefined
162
+ };
163
+ }
210
164
  function makeClassComponentObserver(componentClass) {
211
- var target = componentClass.prototype;
212
-
213
- if (componentClass[mobxObserverProperty]) {
214
- var displayName = getDisplayName(target);
215
- console.warn("The provided component class (" + displayName + ")\n has already been declared as an observer component.");
165
+ var prototype = componentClass.prototype;
166
+ if (componentClass[isMobXReactObserverSymbol]) {
167
+ var displayName = getDisplayName(componentClass);
168
+ throw new Error("The provided component class (" + displayName + ") has already been declared as an observer component.");
216
169
  } else {
217
- componentClass[mobxObserverProperty] = true;
170
+ componentClass[isMobXReactObserverSymbol] = true;
218
171
  }
219
-
220
- if (target.componentWillReact) {
172
+ if (prototype.componentWillReact) {
221
173
  throw new Error("The componentWillReact life-cycle event is no longer supported");
222
174
  }
223
-
224
175
  if (componentClass["__proto__"] !== React.PureComponent) {
225
- if (!target.shouldComponentUpdate) {
226
- target.shouldComponentUpdate = observerSCU;
227
- } else if (target.shouldComponentUpdate !== observerSCU) {
176
+ if (!prototype.shouldComponentUpdate) {
177
+ prototype.shouldComponentUpdate = observerSCU;
178
+ } else if (prototype.shouldComponentUpdate !== observerSCU) {
228
179
  // n.b. unequal check, instead of existence check, as @observer might be on superclass as well
229
180
  throw new Error("It is not allowed to use shouldComponentUpdate in observer based components.");
230
181
  }
231
- } // this.props and this.state are made observable, just to make sure @computed fields that
232
- // are defined inside the component, and which rely on state or props, re-compute if state or props change
233
- // (otherwise the computed wouldn't update and become stale on props change, since props are not observable)
234
- // However, this solution is not without it's own problems: https://github.com/mobxjs/mobx-react/issues?utf8=%E2%9C%93&q=is%3Aissue+label%3Aobservable-props-or-not+
235
-
236
-
237
- makeObservableProp(target, "props");
238
- makeObservableProp(target, "state");
239
-
240
- if (componentClass.contextType) {
241
- makeObservableProp(target, "context");
242
182
  }
243
-
244
- var originalRender = target.render;
245
-
183
+ {
184
+ Object.defineProperties(prototype, observablePropDescriptors);
185
+ }
186
+ var originalRender = prototype.render;
246
187
  if (typeof originalRender !== "function") {
247
- var _displayName = getDisplayName(target);
248
-
188
+ var _displayName = getDisplayName(componentClass);
249
189
  throw new Error("[mobx-react] class component (" + _displayName + ") is missing `render` method." + "\n`observer` requires `render` being a function defined on prototype." + "\n`render = () => {}` or `render = function() {}` is not supported.");
250
190
  }
251
-
252
- target.render = function () {
253
- this.render = mobxReactLite.isUsingStaticRendering() ? originalRender : createReactiveRender.call(this, originalRender);
191
+ prototype.render = function () {
192
+ Object.defineProperty(this, "render", {
193
+ // There is no safe way to replace render, therefore it's forbidden.
194
+ configurable: false,
195
+ writable: false,
196
+ value: mobxReactLite.isUsingStaticRendering() ? originalRender : createReactiveRender.call(this, originalRender)
197
+ });
254
198
  return this.render();
255
199
  };
256
-
257
- patch(target, "componentDidMount", function () {
258
- this[mobxIsUnmounted] = false;
259
-
260
- if (!this.render[mobxAdminProperty]) {
261
- // Reaction is re-created automatically during render, but a component can re-mount and skip render #3395.
262
- // To re-create the reaction and re-subscribe to relevant observables we have to force an update.
263
- React.Component.prototype.forceUpdate.call(this);
200
+ var originalComponentDidMount = prototype.componentDidMount;
201
+ prototype.componentDidMount = function () {
202
+ var _this = this;
203
+ if ( this.componentDidMount !== Object.getPrototypeOf(this).componentDidMount) {
204
+ var _displayName2 = getDisplayName(componentClass);
205
+ throw new Error("[mobx-react] `observer(" + _displayName2 + ").componentDidMount` must be defined on prototype." + "\n`componentDidMount = () => {}` or `componentDidMount = function() {}` is not supported.");
264
206
  }
265
- });
266
- patch(target, "componentWillUnmount", function () {
207
+ // `componentDidMount` may not be called at all. React can abandon the instance after `render`.
208
+ // That's why we use finalization registry to dispose reaction created during render.
209
+ // Happens with `<Suspend>` see #3492
210
+ //
211
+ // `componentDidMount` can be called immediately after `componentWillUnmount` without calling `render` in between.
212
+ // Happens with `<StrictMode>`see #3395.
213
+ //
214
+ // If `componentDidMount` is called, it's guaranteed to run synchronously with render (similary to `useLayoutEffect`).
215
+ // Therefore we don't have to worry about external (observable) state being updated before mount (no state version checking).
216
+ //
217
+ // Things may change: "In the future, React will provide a feature that lets components preserve state between unmounts"
218
+ var admin = getAdministration(this);
219
+ admin.mounted = true;
220
+ // Component instance committed, prevent reaction disposal.
221
+ mobxReactLite._observerFinalizationRegistry.unregister(admin);
222
+ // We don't set forceUpdate before mount because it requires a reference to `this`,
223
+ // therefore `this` could NOT be garbage collected before mount,
224
+ // preventing reaction disposal by FinalizationRegistry and leading to memory leak.
225
+ // As an alternative we could have `admin.instanceRef = new WeakRef(this)`, but lets avoid it if possible.
226
+ admin.forceUpdate = function () {
227
+ return _this.forceUpdate();
228
+ };
229
+ if (!admin.reaction) {
230
+ // 1. Instance was unmounted (reaction disposed) and immediately remounted without running render #3395.
231
+ // 2. Reaction was disposed by finalization registry before mount. Shouldn't ever happen for class components:
232
+ // `componentDidMount` runs synchronously after render, but our registry are deferred (can't run in between).
233
+ // In any case we lost subscriptions to observables, so we have to create new reaction and re-render to resubscribe.
234
+ // The reaction will be created lazily by following render.
235
+ admin.forceUpdate();
236
+ }
237
+ return originalComponentDidMount == null ? void 0 : originalComponentDidMount.apply(this, arguments);
238
+ };
239
+ // TODO@major Overly complicated "patch" is only needed to support the deprecated @disposeOnUnmount
240
+ patch(prototype, "componentWillUnmount", function () {
241
+ var _admin$reaction;
267
242
  if (mobxReactLite.isUsingStaticRendering()) {
268
243
  return;
269
244
  }
270
-
271
- var reaction = this.render[mobxAdminProperty];
272
-
273
- if (reaction) {
274
- reaction.dispose(); // Forces reaction to be re-created on next render
275
-
276
- this.render[mobxAdminProperty] = null;
277
- } else {
278
- // Render may have been hot-swapped and/or overridden by a subclass.
279
- var _displayName2 = getDisplayName(this);
280
-
281
- console.warn("The reactive render of an observer class component (" + _displayName2 + ")\n was overridden after MobX attached. This may result in a memory leak if the\n overridden reactive render was not properly disposed.");
282
- }
283
-
284
- this[mobxIsUnmounted] = true;
245
+ var admin = getAdministration(this);
246
+ (_admin$reaction = admin.reaction) == null ? void 0 : _admin$reaction.dispose();
247
+ admin.reaction = null;
248
+ admin.forceUpdate = null;
249
+ admin.mounted = false;
285
250
  });
286
251
  return componentClass;
287
- } // Generates a friendly name for debugging
288
-
289
- function getDisplayName(comp) {
290
- return comp.displayName || comp.name || comp.constructor && (comp.constructor.displayName || comp.constructor.name) || "<component>";
291
252
  }
292
-
253
+ // Generates a friendly name for debugging
254
+ function getDisplayName(componentClass) {
255
+ return componentClass.displayName || componentClass.name || "<component>";
256
+ }
293
257
  function createReactiveRender(originalRender) {
294
- var _this = this;
295
-
296
- /**
297
- * If props are shallowly modified, react will render anyway,
298
- * so atom.reportChanged() should not result in yet another re-render
299
- */
300
- setHiddenProp(this, skipRenderKey, false);
301
- /**
302
- * forceUpdate will re-assign this.props. We don't want that to cause a loop,
303
- * so detect these changes
304
- */
305
-
306
- setHiddenProp(this, isForcingUpdateKey, false);
307
- var initialName = getDisplayName(this);
308
258
  var boundOriginalRender = originalRender.bind(this);
309
- var isRenderingPending = false;
310
-
311
- var createReaction = function createReaction() {
312
- var reaction = new mobx.Reaction(initialName + ".render()", function () {
313
- if (!isRenderingPending) {
314
- // N.B. Getting here *before mounting* means that a component constructor has side effects (see the relevant test in misc.test.tsx)
315
- // This unidiomatic React usage but React will correctly warn about this so we continue as usual
316
- // See #85 / Pull #44
317
- isRenderingPending = true;
318
-
319
- if (_this[mobxIsUnmounted] !== true) {
320
- var hasError = true;
321
-
322
- try {
323
- setHiddenProp(_this, isForcingUpdateKey, true);
324
-
325
- if (!_this[skipRenderKey]) {
326
- React.Component.prototype.forceUpdate.call(_this);
327
- }
328
-
329
- hasError = false;
330
- } finally {
331
- setHiddenProp(_this, isForcingUpdateKey, false);
332
-
333
- if (hasError) {
334
- reaction.dispose(); // Forces reaction to be re-created on next render
335
-
336
- _this.render[mobxAdminProperty] = null;
337
- }
338
- }
339
- }
340
- }
341
- });
342
- reaction["reactComponent"] = _this;
343
- return reaction;
344
- };
345
-
259
+ var admin = getAdministration(this);
346
260
  function reactiveRender() {
347
- var _reactiveRender$mobxA;
348
-
349
- isRenderingPending = false; // Create reaction lazily to support re-mounting #3395
350
-
351
- var reaction = (_reactiveRender$mobxA = reactiveRender[mobxAdminProperty]) != null ? _reactiveRender$mobxA : reactiveRender[mobxAdminProperty] = createReaction();
352
- var exception = undefined;
353
- var rendering = undefined;
354
- reaction.track(function () {
261
+ if (!admin.reaction) {
262
+ // Create reaction lazily to support re-mounting #3395
263
+ admin.reaction = createReaction(admin);
264
+ if (!admin.mounted) {
265
+ // React can abandon this instance and never call `componentDidMount`/`componentWillUnmount`,
266
+ // we have to make sure reaction will be disposed.
267
+ mobxReactLite._observerFinalizationRegistry.register(this, admin, this);
268
+ }
269
+ }
270
+ var error = undefined;
271
+ var renderResult = undefined;
272
+ admin.reaction.track(function () {
355
273
  try {
356
274
  // TODO@major
357
275
  // Optimization: replace with _allowStateChangesStart/End (not available in mobx@6.0.0)
358
- rendering = mobx._allowStateChanges(false, boundOriginalRender);
276
+ renderResult = mobx._allowStateChanges(false, boundOriginalRender);
359
277
  } catch (e) {
360
- exception = e;
278
+ error = e;
361
279
  }
362
280
  });
363
-
364
- if (exception) {
365
- throw exception;
281
+ if (error) {
282
+ throw error;
366
283
  }
367
-
368
- return rendering;
284
+ return renderResult;
369
285
  }
370
-
371
286
  return reactiveRender;
372
287
  }
373
-
288
+ function createReaction(admin) {
289
+ return new mobx.Reaction(admin.name + ".render()", function () {
290
+ if (!admin.mounted) {
291
+ // This is neccessary to avoid react warning about calling forceUpdate on component that isn't mounted yet.
292
+ // This happens when component is abandoned after render - our reaction is already created and reacts to changes.
293
+ // Due to the synchronous nature of `componenDidMount`, we don't have to worry that component could eventually mount and require update.
294
+ return;
295
+ }
296
+ try {
297
+ admin.forceUpdate == null ? void 0 : admin.forceUpdate();
298
+ } catch (error) {
299
+ var _admin$reaction2;
300
+ (_admin$reaction2 = admin.reaction) == null ? void 0 : _admin$reaction2.dispose();
301
+ admin.reaction = null;
302
+ }
303
+ });
304
+ }
374
305
  function observerSCU(nextProps, nextState) {
375
306
  if (mobxReactLite.isUsingStaticRendering()) {
376
307
  console.warn("[mobx-react] It seems that a re-rendering of a React component is triggered while in static (server-side) mode. Please make sure components are rendered only once server-side.");
377
- } // update on any state changes (as is the default)
378
-
379
-
308
+ }
309
+ // update on any state changes (as is the default)
380
310
  if (this.state !== nextState) {
381
311
  return true;
382
- } // update if props are shallowly not equal, inspired by PureRenderMixin
312
+ }
313
+ // update if props are shallowly not equal, inspired by PureRenderMixin
383
314
  // we could return just 'false' here, and avoid the `skipRender` checks etc
384
315
  // however, it is nicer if lifecycle events are triggered like usually,
385
316
  // so we return true here if props are shallowly modified.
386
-
387
-
388
317
  return !shallowEqual(this.props, nextProps);
389
318
  }
390
-
391
- function makeObservableProp(target, propName) {
392
- var valueHolderKey = newSymbol("reactProp_" + propName + "_valueHolder");
393
- var atomHolderKey = newSymbol("reactProp_" + propName + "_atomHolder");
394
-
395
- function getAtom() {
396
- if (!this[atomHolderKey]) {
397
- setHiddenProp(this, atomHolderKey, mobx.createAtom("reactive " + propName));
398
- }
399
-
400
- return this[atomHolderKey];
401
- }
402
-
403
- Object.defineProperty(target, propName, {
319
+ function createObservablePropDescriptor(key) {
320
+ return {
404
321
  configurable: true,
405
322
  enumerable: true,
406
323
  get: function get() {
407
- var prevReadState = false; // Why this check? BC?
408
- // @ts-expect-error
409
-
410
- if (mobx._allowStateReadsStart && mobx._allowStateReadsEnd) {
411
- prevReadState = mobx._allowStateReadsStart(true);
324
+ var admin = getAdministration(this);
325
+ var derivation = mobx._getGlobalState().trackingDerivation;
326
+ if (derivation && derivation !== admin.reaction) {
327
+ throw new Error("[mobx-react] Cannot read \"" + admin.name + "." + key + "\" in a reactive context, as it isn't observable.\n Please use component lifecycle method to copy the value into a local observable first.\n See https://github.com/mobxjs/mobx/blob/main/packages/mobx-react/README.md#note-on-using-props-and-state-in-derivations");
412
328
  }
413
-
414
- getAtom.call(this).reportObserved(); // Why this check? BC?
415
- // @ts-expect-error
416
-
417
- if (mobx._allowStateReadsStart && mobx._allowStateReadsEnd) {
418
- mobx._allowStateReadsEnd(prevReadState);
419
- }
420
-
421
- return this[valueHolderKey];
329
+ return admin[key];
422
330
  },
423
- set: function set(v) {
424
- if (!this[isForcingUpdateKey] && !shallowEqual(this[valueHolderKey], v)) {
425
- setHiddenProp(this, valueHolderKey, v);
426
- setHiddenProp(this, skipRenderKey, true);
427
- getAtom.call(this).reportChanged();
428
- setHiddenProp(this, skipRenderKey, false);
429
- } else {
430
- setHiddenProp(this, valueHolderKey, v);
431
- }
331
+ set: function set(value) {
332
+ getAdministration(this)[key] = value;
432
333
  }
433
- });
334
+ };
434
335
  }
435
336
 
436
337
  /**
437
338
  * Observer function / decorator
438
339
  */
439
-
440
340
  function observer(component) {
441
341
  if (component["isMobxInjector"] === true) {
442
342
  console.warn("Mobx observer: You are trying to use `observer` on a component that already has `inject`. Please apply `observer` before applying `inject`");
443
343
  }
444
-
445
344
  if (Object.prototype.isPrototypeOf.call(React.Component, component) || Object.prototype.isPrototypeOf.call(React.PureComponent, component)) {
446
345
  // Class component
447
346
  return makeClassComponentObserver(component);
@@ -452,35 +351,29 @@
452
351
  }
453
352
 
454
353
  function _extends() {
455
- _extends = Object.assign || function (target) {
354
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
456
355
  for (var i = 1; i < arguments.length; i++) {
457
356
  var source = arguments[i];
458
-
459
357
  for (var key in source) {
460
358
  if (Object.prototype.hasOwnProperty.call(source, key)) {
461
359
  target[key] = source[key];
462
360
  }
463
361
  }
464
362
  }
465
-
466
363
  return target;
467
364
  };
468
-
469
365
  return _extends.apply(this, arguments);
470
366
  }
471
-
472
367
  function _objectWithoutPropertiesLoose(source, excluded) {
473
368
  if (source == null) return {};
474
369
  var target = {};
475
370
  var sourceKeys = Object.keys(source);
476
371
  var key, i;
477
-
478
372
  for (i = 0; i < sourceKeys.length; i++) {
479
373
  key = sourceKeys[i];
480
374
  if (excluded.indexOf(key) >= 0) continue;
481
375
  target[key] = source[key];
482
376
  }
483
-
484
377
  return target;
485
378
  }
486
379
 
@@ -488,21 +381,16 @@
488
381
  var MobXProviderContext = /*#__PURE__*/React__default.createContext({});
489
382
  function Provider(props) {
490
383
  var children = props.children,
491
- stores = _objectWithoutPropertiesLoose(props, _excluded);
492
-
384
+ stores = _objectWithoutPropertiesLoose(props, _excluded);
493
385
  var parentValue = React__default.useContext(MobXProviderContext);
494
386
  var mutableProviderRef = React__default.useRef(_extends({}, parentValue, stores));
495
387
  var value = mutableProviderRef.current;
496
-
497
388
  {
498
389
  var newValue = _extends({}, value, stores); // spread in previous state for the context based stores
499
-
500
-
501
390
  if (!shallowEqual(value, newValue)) {
502
391
  throw new Error("MobX Provider: The set of provided stores has changed. See: https://github.com/mobxjs/mobx-react#the-set-of-provided-stores-has-changed-error.");
503
392
  }
504
393
  }
505
-
506
394
  return React__default.createElement(MobXProviderContext.Provider, {
507
395
  value: value
508
396
  }, children);
@@ -512,38 +400,31 @@
512
400
  /**
513
401
  * Store Injection
514
402
  */
515
-
516
403
  function createStoreInjector(grabStoresFn, component, injectNames, makeReactive) {
517
404
  // Support forward refs
518
405
  var Injector = React__default.forwardRef(function (props, ref) {
519
406
  var newProps = _extends({}, props);
520
-
521
407
  var context = React__default.useContext(MobXProviderContext);
522
408
  Object.assign(newProps, grabStoresFn(context || {}, newProps) || {});
523
-
524
409
  if (ref) {
525
410
  newProps.ref = ref;
526
411
  }
527
-
528
412
  return React__default.createElement(component, newProps);
529
413
  });
530
414
  if (makeReactive) Injector = observer(Injector);
531
415
  Injector["isMobxInjector"] = true; // assigned late to suppress observer warning
532
416
  // Static fields from component should be visible on the generated Injector
533
-
534
417
  copyStaticProperties(component, Injector);
535
418
  Injector["wrappedComponent"] = component;
536
419
  Injector.displayName = getInjectName(component, injectNames);
537
420
  return Injector;
538
421
  }
539
-
540
422
  function getInjectName(component, injectNames) {
541
423
  var displayName;
542
424
  var componentName = component.displayName || component.name || component.constructor && component.constructor.name || "Component";
543
425
  if (injectNames) displayName = "inject-with-" + injectNames + "(" + componentName + ")";else displayName = "inject(" + componentName + ")";
544
426
  return displayName;
545
427
  }
546
-
547
428
  function grabStoresByName(storeNames) {
548
429
  return function (baseStores, nextProps) {
549
430
  storeNames.forEach(function (storeName) {
@@ -561,13 +442,10 @@
561
442
  * or a function that manually maps the available stores from the context to props:
562
443
  * storesToProps(mobxStores, props, context) => newProps
563
444
  */
564
-
565
-
566
445
  function inject() {
567
446
  for (var _len = arguments.length, storeNames = new Array(_len), _key = 0; _key < _len; _key++) {
568
447
  storeNames[_key] = arguments[_key];
569
448
  }
570
-
571
449
  if (typeof arguments[0] === "function") {
572
450
  var grabStoresFn = arguments[0];
573
451
  return function (componentClass) {
@@ -580,14 +458,14 @@
580
458
  }
581
459
  }
582
460
 
583
- var protoStoreKey = /*#__PURE__*/newSymbol("disposeOnUnmountProto");
584
- var instStoreKey = /*#__PURE__*/newSymbol("disposeOnUnmountInst");
585
-
461
+ var reactMajorVersion = /*#__PURE__*/Number.parseInt( /*#__PURE__*/React__default.version.split(".")[0]);
462
+ var warnedAboutDisposeOnUnmountDeprecated = false;
463
+ var protoStoreKey = /*#__PURE__*/Symbol("disposeOnUnmountProto");
464
+ var instStoreKey = /*#__PURE__*/Symbol("disposeOnUnmountInst");
586
465
  function runDisposersOnWillUnmount() {
587
466
  var _this = this;
588
467
  [].concat(this[protoStoreKey] || [], this[instStoreKey] || []).forEach(function (propKeyOrFunction) {
589
468
  var prop = typeof propKeyOrFunction === "string" ? _this[propKeyOrFunction] : propKeyOrFunction;
590
-
591
469
  if (prop !== undefined && prop !== null) {
592
470
  if (Array.isArray(prop)) prop.map(function (f) {
593
471
  return f();
@@ -595,62 +473,67 @@
595
473
  }
596
474
  });
597
475
  }
598
-
476
+ /**
477
+ * @deprecated `disposeOnUnmount` is not compatible with React 18 and higher.
478
+ */
599
479
  function disposeOnUnmount(target, propertyKeyOrFunction) {
600
480
  if (Array.isArray(propertyKeyOrFunction)) {
601
481
  return propertyKeyOrFunction.map(function (fn) {
602
482
  return disposeOnUnmount(target, fn);
603
483
  });
604
484
  }
605
-
485
+ if (!warnedAboutDisposeOnUnmountDeprecated) {
486
+ if (reactMajorVersion >= 18) {
487
+ console.error("[mobx-react] disposeOnUnmount is not compatible with React 18 and higher. Don't use it.");
488
+ } else {
489
+ console.warn("[mobx-react] disposeOnUnmount is deprecated. It won't work correctly with React 18 and higher.");
490
+ }
491
+ warnedAboutDisposeOnUnmountDeprecated = true;
492
+ }
606
493
  var c = Object.getPrototypeOf(target).constructor;
607
- var c2 = Object.getPrototypeOf(target.constructor); // Special case for react-hot-loader
608
-
494
+ var c2 = Object.getPrototypeOf(target.constructor);
495
+ // Special case for react-hot-loader
609
496
  var c3 = Object.getPrototypeOf(Object.getPrototypeOf(target));
610
-
611
497
  if (!(c === React__default.Component || c === React__default.PureComponent || c2 === React__default.Component || c2 === React__default.PureComponent || c3 === React__default.Component || c3 === React__default.PureComponent)) {
612
498
  throw new Error("[mobx-react] disposeOnUnmount only supports direct subclasses of React.Component or React.PureComponent.");
613
499
  }
614
-
615
500
  if (typeof propertyKeyOrFunction !== "string" && typeof propertyKeyOrFunction !== "function" && !Array.isArray(propertyKeyOrFunction)) {
616
501
  throw new Error("[mobx-react] disposeOnUnmount only works if the parameter is either a property key or a function.");
617
- } // decorator's target is the prototype, so it doesn't have any instance properties like props
618
-
619
-
620
- var isDecorator = typeof propertyKeyOrFunction === "string"; // add property key / function we want run (disposed) to the store
621
-
502
+ }
503
+ // decorator's target is the prototype, so it doesn't have any instance properties like props
504
+ var isDecorator = typeof propertyKeyOrFunction === "string";
505
+ // add property key / function we want run (disposed) to the store
622
506
  var componentWasAlreadyModified = !!target[protoStoreKey] || !!target[instStoreKey];
623
- var store = isDecorator ? // decorators are added to the prototype store
624
- target[protoStoreKey] || (target[protoStoreKey] = []) : // functions are added to the instance store
507
+ var store = isDecorator ?
508
+ // decorators are added to the prototype store
509
+ target[protoStoreKey] || (target[protoStoreKey] = []) :
510
+ // functions are added to the instance store
625
511
  target[instStoreKey] || (target[instStoreKey] = []);
626
- store.push(propertyKeyOrFunction); // tweak the component class componentWillUnmount if not done already
627
-
512
+ store.push(propertyKeyOrFunction);
513
+ // tweak the component class componentWillUnmount if not done already
628
514
  if (!componentWasAlreadyModified) {
629
515
  patch(target, "componentWillUnmount", runDisposersOnWillUnmount);
630
- } // return the disposer as is if invoked as a non decorator
631
-
632
-
516
+ }
517
+ // return the disposer as is if invoked as a non decorator
633
518
  if (typeof propertyKeyOrFunction !== "string") {
634
519
  return propertyKeyOrFunction;
635
520
  }
636
521
  }
637
522
 
523
+ // Copied from React.PropTypes
638
524
  function createChainableTypeChecker(validator) {
639
525
  function checkType(isRequired, props, propName, componentName, location, propFullName) {
640
526
  for (var _len = arguments.length, rest = new Array(_len > 6 ? _len - 6 : 0), _key = 6; _key < _len; _key++) {
641
527
  rest[_key - 6] = arguments[_key];
642
528
  }
643
-
644
529
  return mobx.untracked(function () {
645
530
  componentName = componentName || "<<anonymous>>";
646
531
  propFullName = propFullName || propName;
647
-
648
532
  if (props[propName] == null) {
649
533
  if (isRequired) {
650
534
  var actual = props[propName] === null ? "null" : "undefined";
651
535
  return new Error("The " + location + " `" + propFullName + "` is marked as required " + "in `" + componentName + "`, but its value is `" + actual + "`.");
652
536
  }
653
-
654
537
  return null;
655
538
  } else {
656
539
  // @ts-ignore rest arg is necessary for some React internals - fails tests otherwise
@@ -658,60 +541,48 @@
658
541
  }
659
542
  });
660
543
  }
661
-
662
- var chainedCheckType = checkType.bind(null, false); // Add isRequired to satisfy Requirable
663
-
544
+ var chainedCheckType = checkType.bind(null, false);
545
+ // Add isRequired to satisfy Requirable
664
546
  chainedCheckType.isRequired = checkType.bind(null, true);
665
547
  return chainedCheckType;
666
- } // Copied from React.PropTypes
667
-
668
-
548
+ }
549
+ // Copied from React.PropTypes
669
550
  function isSymbol(propType, propValue) {
670
551
  // Native Symbol.
671
552
  if (propType === "symbol") {
672
553
  return true;
673
- } // 19.4.3.5 Symbol.prototype[@@toStringTag] === 'Symbol'
674
-
675
-
554
+ }
555
+ // 19.4.3.5 Symbol.prototype[@@toStringTag] === 'Symbol'
676
556
  if (propValue["@@toStringTag"] === "Symbol") {
677
557
  return true;
678
- } // Fallback for non-spec compliant Symbols which are polyfilled.
679
-
680
-
558
+ }
559
+ // Fallback for non-spec compliant Symbols which are polyfilled.
681
560
  if (typeof Symbol === "function" && propValue instanceof Symbol) {
682
561
  return true;
683
562
  }
684
-
685
563
  return false;
686
- } // Copied from React.PropTypes
687
-
688
-
564
+ }
565
+ // Copied from React.PropTypes
689
566
  function getPropType(propValue) {
690
567
  var propType = typeof propValue;
691
-
692
568
  if (Array.isArray(propValue)) {
693
569
  return "array";
694
570
  }
695
-
696
571
  if (propValue instanceof RegExp) {
697
572
  // Old webkits (at least until Android 4.0) return 'function' rather than
698
573
  // 'object' for typeof a RegExp. We'll normalize this here so that /bla/
699
574
  // passes PropTypes.object.
700
575
  return "object";
701
576
  }
702
-
703
577
  if (isSymbol(propType, propValue)) {
704
578
  return "symbol";
705
579
  }
706
-
707
580
  return propType;
708
- } // This handles more types than `getPropType`. Only used for error messages.
581
+ }
582
+ // This handles more types than `getPropType`. Only used for error messages.
709
583
  // Copied from React.PropTypes
710
-
711
-
712
584
  function getPreciseType(propValue) {
713
585
  var propType = getPropType(propValue);
714
-
715
586
  if (propType === "object") {
716
587
  if (propValue instanceof Date) {
717
588
  return "date";
@@ -719,55 +590,43 @@
719
590
  return "regexp";
720
591
  }
721
592
  }
722
-
723
593
  return propType;
724
594
  }
725
-
726
595
  function createObservableTypeCheckerCreator(allowNativeType, mobxType) {
727
596
  return createChainableTypeChecker(function (props, propName, componentName, location, propFullName) {
728
597
  return mobx.untracked(function () {
729
598
  if (allowNativeType) {
730
599
  if (getPropType(props[propName]) === mobxType.toLowerCase()) return null;
731
600
  }
732
-
733
601
  var mobxChecker;
734
-
735
602
  switch (mobxType) {
736
603
  case "Array":
737
604
  mobxChecker = mobx.isObservableArray;
738
605
  break;
739
-
740
606
  case "Object":
741
607
  mobxChecker = mobx.isObservableObject;
742
608
  break;
743
-
744
609
  case "Map":
745
610
  mobxChecker = mobx.isObservableMap;
746
611
  break;
747
-
748
612
  default:
749
613
  throw new Error("Unexpected mobxType: " + mobxType);
750
614
  }
751
-
752
615
  var propValue = props[propName];
753
-
754
616
  if (!mobxChecker(propValue)) {
755
617
  var preciseType = getPreciseType(propValue);
756
618
  var nativeTypeExpectationMessage = allowNativeType ? " or javascript `" + mobxType.toLowerCase() + "`" : "";
757
619
  return new Error("Invalid prop `" + propFullName + "` of type `" + preciseType + "` supplied to" + " `" + componentName + "`, expected `mobx.Observable" + mobxType + "`" + nativeTypeExpectationMessage + ".");
758
620
  }
759
-
760
621
  return null;
761
622
  });
762
623
  });
763
624
  }
764
-
765
625
  function createObservableArrayOfTypeChecker(allowNativeType, typeChecker) {
766
626
  return createChainableTypeChecker(function (props, propName, componentName, location, propFullName) {
767
627
  for (var _len2 = arguments.length, rest = new Array(_len2 > 5 ? _len2 - 5 : 0), _key2 = 5; _key2 < _len2; _key2++) {
768
628
  rest[_key2 - 5] = arguments[_key2];
769
629
  }
770
-
771
630
  return mobx.untracked(function () {
772
631
  if (typeof typeChecker !== "function") {
773
632
  return new Error("Property `" + propFullName + "` of component `" + componentName + "` has " + "invalid PropType notation.");
@@ -775,18 +634,15 @@
775
634
  var error = createObservableTypeCheckerCreator(allowNativeType, "Array")(props, propName, componentName, location, propFullName);
776
635
  if (error instanceof Error) return error;
777
636
  var propValue = props[propName];
778
-
779
637
  for (var i = 0; i < propValue.length; i++) {
780
638
  error = typeChecker.apply(void 0, [propValue, i, componentName, location, propFullName + "[" + i + "]"].concat(rest));
781
639
  if (error instanceof Error) return error;
782
640
  }
783
-
784
641
  return null;
785
642
  }
786
643
  });
787
644
  });
788
645
  }
789
-
790
646
  var observableArray = /*#__PURE__*/createObservableTypeCheckerCreator(false, "Array");
791
647
  var observableArrayOf = /*#__PURE__*/createObservableArrayOfTypeChecker.bind(null, false);
792
648
  var observableMap = /*#__PURE__*/createObservableTypeCheckerCreator(false, "Map");
@@ -804,8 +660,12 @@
804
660
  objectOrObservableObject: objectOrObservableObject
805
661
  };
806
662
 
807
- if (!React.Component) throw new Error("mobx-react requires React to be available");
808
- if (!mobx.observable) throw new Error("mobx-react requires mobx to be available");
663
+ if (!React.Component) {
664
+ throw new Error("mobx-react requires React to be available");
665
+ }
666
+ if (!mobx.observable) {
667
+ throw new Error("mobx-react requires mobx to be available");
668
+ }
809
669
 
810
670
  Object.defineProperty(exports, 'Observer', {
811
671
  enumerable: true,