mobx-react 9.2.2 → 10.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/README.md +8 -264
  3. package/dist/index.d.ts +1 -6
  4. package/dist/mobxreact.cjs.development.js +101 -449
  5. package/dist/mobxreact.cjs.development.js.map +1 -1
  6. package/dist/mobxreact.cjs.production.min.js +1 -1
  7. package/dist/mobxreact.cjs.production.min.js.map +1 -1
  8. package/dist/mobxreact.esm.development.js +76 -405
  9. package/dist/mobxreact.esm.development.js.map +1 -1
  10. package/dist/mobxreact.esm.js +75 -404
  11. package/dist/mobxreact.esm.js.map +1 -1
  12. package/dist/mobxreact.esm.production.min.js +1 -1
  13. package/dist/mobxreact.esm.production.min.js.map +1 -1
  14. package/dist/mobxreact.mjs +338 -0
  15. package/dist/mobxreact.mjs.map +1 -0
  16. package/dist/mobxreact.umd.development.js +103 -450
  17. package/dist/mobxreact.umd.development.js.map +1 -1
  18. package/dist/mobxreact.umd.production.min.js +1 -1
  19. package/dist/mobxreact.umd.production.min.js.map +1 -1
  20. package/dist/utils/utils.d.ts +0 -8
  21. package/package.json +30 -20
  22. package/src/index.ts +3 -12
  23. package/src/observer.tsx +9 -8
  24. package/src/utils/utils.ts +0 -46
  25. package/batchingForReactDom.js +0 -1
  26. package/batchingForReactNative.js +0 -1
  27. package/batchingOptOut.js +0 -1
  28. package/dist/Provider.d.ts +0 -10
  29. package/dist/disposeOnUnmount.d.ts +0 -11
  30. package/dist/inject.d.ts +0 -6
  31. package/dist/propTypes.d.ts +0 -9
  32. package/dist/types/IStoresToProps.d.ts +0 -2
  33. package/dist/types/IValueMap.d.ts +0 -1
  34. package/dist/types/IWrappedComponent.d.ts +0 -4
  35. package/src/Provider.tsx +0 -29
  36. package/src/disposeOnUnmount.ts +0 -111
  37. package/src/inject.ts +0 -107
  38. package/src/propTypes.ts +0 -210
  39. package/src/types/IStoresToProps.ts +0 -7
  40. package/src/types/IValueMap.ts +0 -1
  41. package/src/types/IWrappedComponent.ts +0 -4
package/CHANGELOG.md CHANGED
@@ -1,5 +1,78 @@
1
1
  # mobx-react
2
2
 
3
+ ## 10.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [`1926c69f53168619d688f348aa587e8f3ae579ae`](https://github.com/mobxjs/mobx/commit/1926c69f53168619d688f348aa587e8f3ae579ae) [#4671](https://github.com/mobxjs/mobx/pull/4671) Thanks [@kubk](https://github.com/kubk)! - Release MobX 7, mobx-react-lite 5, and mobx-react 10.
8
+
9
+ Bundle sizes are down: ESM prod 17.02 KiB gzip -> 13.96 KiB gzip; a minimal tree-shaken example is 10.32 KiB gzip now.
10
+
11
+ It removes long-deprecated compatibility paths and keeps the React bindings split between `mobx-react-lite` for function components and `mobx-react` for class-component support.
12
+
13
+ ## MobX 7
14
+
15
+ MobX 7 is a cleanup release focused on the modern runtime and decorator model.
16
+
17
+ - MobX now always uses Proxy-backed observable objects and arrays. The ES5/non-proxy fallback has been removed.
18
+ - `configure({ useProxies: ... })` is no longer supported.
19
+ - `{ proxy: false }` options for `observable`, `observable.object`, and `observable.array` are no longer supported.
20
+ - Legacy decorators are no longer supported.
21
+ - Namespaced annotation and comparer properties now use named exports to reduce bundle size:
22
+
23
+ | Removed API | Replacement |
24
+ | --------------------- | ------------------- |
25
+ | `observable.ref` | `observableRef` |
26
+ | `observable.shallow` | `observableShallow` |
27
+ | `observable.deep` | `observableDeep` |
28
+ | `observable.struct` | `observableStruct` |
29
+ | `computed.struct` | `computedStruct` |
30
+ | `action.bound` | `actionBound` |
31
+ | `flow.bound` | `flowBound` |
32
+ | `comparer.identity` | `compareIdentity` |
33
+ | `comparer.default` | `compareDefault` |
34
+ | `comparer.structural` | `compareStructural` |
35
+ | `comparer.shallow` | `compareShallow` |
36
+
37
+ - The public `trace` API and its related runtime support have been removed. Use `toJS`, `getDependencyTree`, `getObserverTree`, `spy` or `mobx-log` package for debugging.
38
+
39
+ ## mobx-react-lite 5 and mobx-react 10
40
+
41
+ mobx-react-lite 5 and mobx-react 10 require MobX 7 and React 18 or later.
42
+
43
+ `mobx-react-lite` remains the function-component package. `mobx-react` remains a thin wrapper around `mobx-react-lite` that adds class component and Stage 3 `@observer` class decorator support.
44
+
45
+ - Keep function-component imports on `mobx-react-lite` if you do not need class component support.
46
+ - Use `mobx-react` when you need class components or `@observer` class decorators.
47
+ - `mobx-react-lite` supports function components and `forwardRef`; `mobx-react` delegates function components to `mobx-react-lite` and handles classes itself.
48
+ - Remove React batching imports, including the stale React Native batching deep import. React 18+ renderers handle batching.
49
+
50
+ The recommended public React binding surface for both packages is:
51
+
52
+ - `observer`
53
+ - `Observer`
54
+ - `useLocalObservable`
55
+ - `enableStaticRendering`
56
+ - `isUsingStaticRendering`
57
+
58
+ The following APIs have been removed from the React binding packages:
59
+
60
+ - `Provider`, `inject`, and `MobXProviderContext`; use `React.createContext` directly.
61
+ - `disposeOnUnmount`; dispose reactions in `componentWillUnmount` or return cleanup functions from `useEffect`.
62
+ - `PropTypes`; use TypeScript or the regular `prop-types` package.
63
+ - `useObserver`; wrap components with `observer` or use `<Observer>`.
64
+ - `useLocalStore`; use `useLocalObservable`.
65
+ - `useAsObservableSource`; synchronize values from props into local observable state explicitly.
66
+ - `useStaticRendering`; use `enableStaticRendering`.
67
+ - `observerBatching`, `isObserverBatched`, `batchingForReactDom`, `batchingOptOut`, and `batchingForReactNative`; remove these imports because React 18+ renderers handle batching.
68
+ - Deprecated `observer(fn, { forwardRef: true })`; pass an already-created `React.forwardRef(...)` component to `observer` instead.
69
+ - Legacy function-component `contextTypes` handling.
70
+
71
+ ### Patch Changes
72
+
73
+ - Updated dependencies [[`1926c69f53168619d688f348aa587e8f3ae579ae`](https://github.com/mobxjs/mobx/commit/1926c69f53168619d688f348aa587e8f3ae579ae)]:
74
+ - mobx-react-lite@5.0.0
75
+
3
76
  ## 9.2.2
4
77
 
5
78
  ### Patch Changes
package/README.md CHANGED
@@ -17,21 +17,20 @@ Only the latest version is actively maintained. If you're missing a fix or a fea
17
17
 
18
18
  | NPM Version | Support MobX version | Supported React versions | Added support for: |
19
19
  | ----------- | -------------------- | ------------------------ | -------------------------------------------------------------------------------- |
20
+ | v10 | 7.\* | >=18 | MobX 7, Hooks, React 18 strict mode |
20
21
  | v9 | 6.\* | >16.8 | Hooks, React 18.2 in strict mode |
21
22
  | v7 | 6.\* | >16.8 < 18.2 | Hooks |
22
23
  | v6 | 4.\* / 5.\* | >16.8 <17 | Hooks |
23
24
  | v5 | 4.\* / 5.\* | >0.13 <17 | No, but it is possible to use `<Observer>` sections inside hook based components |
24
25
 
25
- mobx-react 6 / 7 is a repackage of the smaller [mobx-react-lite](https://github.com/mobxjs/mobx/tree/main/packages/mobx-react-lite) package + following features from the `mobx-react@5` package added:
26
+ `mobx-react` is a wrapper around `mobx-react-lite` for applications that also need class component support:
26
27
 
28
+ - Support for function components through `mobx-react-lite`
27
29
  - Support for class based components for `observer` and `@observer`
28
- - `Provider / inject` to pass stores around (but consider to use `React.createContext` instead)
29
- - `PropTypes` to describe observable based property checkers (but consider to use TypeScript instead)
30
- - The `disposeOnUnmount` utility / decorator to easily clean up resources such as reactions created in your class based components.
31
30
 
32
31
  ## Installation
33
32
 
34
- `npm install mobx-react --save`
33
+ `npm install mobx-react`
35
34
 
36
35
  Or CDN: https://unpkg.com/mobx-react (UMD namespace: `mobxReact`)
37
36
 
@@ -42,7 +41,7 @@ import { observer } from "mobx-react"
42
41
  This package provides the bindings for MobX and React.
43
42
  See the [official documentation](https://mobx.js.org/react-integration.html) for how to get started.
44
43
 
45
- For greenfield projects you might want to consider to use [mobx-react-lite](https://github.com/mobxjs/mobx/tree/main/packages/mobx-react-lite), if you intend to only use function based components. `React.createContext` can be used to pass stores around.
44
+ Use `React.createContext` to pass stores around.
46
45
 
47
46
  ## API documentation
48
47
 
@@ -226,7 +225,7 @@ person.name = "Mike" // will cause the Observer region to re-render
226
225
  Local observable state can be introduced by using the `useLocalObservable` hook, that runs once to create an observable store. A quick example would be:
227
226
 
228
227
  ```javascript
229
- import { useLocalObservable, Observer } from "mobx-react-lite"
228
+ import { useLocalObservable, Observer } from "mobx-react"
230
229
 
231
230
  const Todo = () => {
232
231
  const todo = useLocalObservable(() => ({
@@ -241,7 +240,7 @@ const Todo = () => {
241
240
  <Observer>
242
241
  {() => (
243
242
  <h1 onClick={todo.toggle}>
244
- {todo.title} {todo.done ? "[DONE]" : "[TODO]"}
243
+ {todo.title} {todo.done ? "[DONE]" : "[OPEN]"}
245
244
  </h1>
246
245
  )}
247
246
  </Observer>
@@ -253,7 +252,7 @@ When using `useLocalObservable`, all properties of the returned object will be m
253
252
 
254
253
  It is important to realize that the store is created only once! It is not possible to specify dependencies to force re-creation, _nor should you directly be referring to props for the initializer function_, as changes in those won't propagate.
255
254
 
256
- Instead, if your store needs to refer to props (or `useState` based local state), the `useLocalObservable` should be combined with the `useAsObservableSource` hook, see below.
255
+ Instead, if your store needs to refer to props (or `useState` based local state), sync those values into the store with `useEffect`.
257
256
 
258
257
  Note that in many cases it is possible to extract the initializer function to a function outside the component definition. Which makes it possible to test the store itself in a more straight-forward manner, and avoids creating the initializer closure on each re-render.
259
258
 
@@ -287,261 +286,6 @@ Decorators are currently a stage-2 ESNext feature. How to enable them is documen
287
286
  See this [thread](https://www.reddit.com/r/reactjs/comments/4vnxg5/free_eggheadio_course_learn_mobx_react_in_30/d61oh0l).
288
287
  TL;DR: the conceptual distinction makes a lot of sense when using MobX as well, but use `observer` on all components.
289
288
 
290
- ### `PropTypes`
291
-
292
- MobX-react provides the following additional `PropTypes` which can be used to validate against MobX structures:
293
-
294
- - `observableArray`
295
- - `observableArrayOf(React.PropTypes.number)`
296
- - `observableMap`
297
- - `observableObject`
298
- - `arrayOrObservableArray`
299
- - `arrayOrObservableArrayOf(React.PropTypes.number)`
300
- - `objectOrObservableObject`
301
-
302
- Use `import { PropTypes } from "mobx-react"` to import them, then use for example `PropTypes.observableArray`
303
-
304
- ### `Provider` and `inject`
305
-
306
- _Note: usually there is no need anymore to use `Provider` / `inject` in new code bases; most of its features are now covered by `React.createContext`._
307
-
308
- `Provider` is a component that can pass stores (or other stuff) using React's context mechanism to child components.
309
- This is useful if you have things that you don't want to pass through multiple layers of components explicitly.
310
-
311
- `inject` can be used to pick up those stores. It is a higher order component that takes a list of strings and makes those stores available to the wrapped component.
312
-
313
- Example (based on the official [context docs](https://facebook.github.io/react/docs/context.html#passing-info-automatically-through-a-tree)):
314
-
315
- ```javascript
316
- @inject("color")
317
- @observer
318
- class Button extends React.Component {
319
- render() {
320
- return <button style={{ background: this.props.color }}>{this.props.children}</button>
321
- }
322
- }
323
-
324
- class Message extends React.Component {
325
- render() {
326
- return (
327
- <div>
328
- {this.props.text} <Button>Delete</Button>
329
- </div>
330
- )
331
- }
332
- }
333
-
334
- class MessageList extends React.Component {
335
- render() {
336
- const children = this.props.messages.map(message => <Message text={message.text} />)
337
- return (
338
- <Provider color="red">
339
- <div>{children}</div>
340
- </Provider>
341
- )
342
- }
343
- }
344
- ```
345
-
346
- Notes:
347
-
348
- - It is possible to read the stores provided by `Provider` using `React.useContext`, by using the `MobXProviderContext` context that can be imported from `mobx-react`.
349
- - If a component asks for a store and receives a store via a property with the same name, the property takes precedence. Use this to your advantage when testing!
350
- - When using both `@inject` and `@observer`, make sure to apply them in the correct order: `observer` should be the inner decorator, `inject` the outer. There might be additional decorators in between.
351
- - The original component wrapped by `inject` is available as the `wrappedComponent` property of the created higher order component.
352
-
353
- #### "The set of provided stores has changed" error
354
-
355
- Values provided through `Provider` should be final. Make sure that if you put things in `context` that might change over time, that they are `@observable` or provide some other means to listen to changes, like callbacks. However, if your stores will change over time, like an observable value of another store, MobX will throw an error.
356
- This restriction exists mainly for legacy reasons. If you have a scenario where you need to modify the set of stores, please leave a comment about it in this issue https://github.com/mobxjs/mobx-react/issues/745. Or a preferred way is to [use React Context](https://reactjs.org/docs/context.html) directly which does not have this restriction.
357
-
358
- #### Inject as function
359
-
360
- The above example in ES5 would start like:
361
-
362
- ```javascript
363
- var Button = inject("color")(
364
- observer(
365
- class Button extends Component {
366
- /* ... etc ... */
367
- }
368
- )
369
- )
370
- ```
371
-
372
- A functional stateless component would look like:
373
-
374
- ```javascript
375
- var Button = inject("color")(
376
- observer(({ color }) => {
377
- /* ... etc ... */
378
- })
379
- )
380
- ```
381
-
382
- #### Customizing inject
383
-
384
- Instead of passing a list of store names, it is also possible to create a custom mapper function and pass it to inject.
385
- The mapper function receives all stores as argument, the properties with which the components are invoked and the context, and should produce a new set of properties,
386
- that are mapped into the original:
387
-
388
- `mapperFunction: (allStores, props, context) => additionalProps`
389
-
390
- Since version 4.0 the `mapperFunction` itself is tracked as well, so it is possible to do things like:
391
-
392
- ```javascript
393
- const NameDisplayer = ({ name }) => <h1>{name}</h1>
394
-
395
- const UserNameDisplayer = inject(stores => ({
396
- name: stores.userStore.name
397
- }))(NameDisplayer)
398
-
399
- const user = mobx.observable({
400
- name: "Noa"
401
- })
402
-
403
- const App = () => (
404
- <Provider userStore={user}>
405
- <UserNameDisplayer />
406
- </Provider>
407
- )
408
-
409
- ReactDOM.render(<App />, document.body)
410
- ```
411
-
412
- _N.B. note that in this *specific* case neither `NameDisplayer` nor `UserNameDisplayer` needs to be decorated with `observer`, since the observable dereferencing is done in the mapper function_
413
-
414
- #### Using `PropTypes` and `defaultProps` and other static properties in combination with `inject`
415
-
416
- Inject wraps a new component around the component you pass into it.
417
- This means that assigning a static property to the resulting component, will be applied to the HoC, and not to the original component.
418
- So if you take the following example:
419
-
420
- ```javascript
421
- const UserName = inject("userStore")(({ userStore, bold }) => someRendering())
422
-
423
- UserName.propTypes = {
424
- bold: PropTypes.boolean.isRequired,
425
- userStore: PropTypes.object.isRequired // will always fail
426
- }
427
- ```
428
-
429
- The above propTypes are incorrect, `bold` needs to be provided by the caller of the `UserName` component and is checked by React.
430
- However, `userStore` does not need to be required! Although it is required for the original stateless function component, it is not
431
- required for the resulting inject component. After all, the whole point of that component is to provide that `userStore` itself.
432
-
433
- So if you want to make assertions on the data that is being injected (either stores or data resulting from a mapper function), the propTypes
434
- should be defined on the _wrapped_ component. Which is available through the static property `wrappedComponent` on the inject component:
435
-
436
- ```javascript
437
- const UserName = inject("userStore")(({ userStore, bold }) => someRendering())
438
-
439
- UserName.propTypes = {
440
- bold: PropTypes.boolean.isRequired // could be defined either here ...
441
- }
442
-
443
- UserName.wrappedComponent.propTypes = {
444
- // ... or here
445
- userStore: PropTypes.object.isRequired // correct
446
- }
447
- ```
448
-
449
- The same principle applies to `defaultProps` and other static React properties.
450
- Note that it is not allowed to redefine `contextTypes` on `inject` components (but is possible to define it on `wrappedComponent`)
451
-
452
- Finally, mobx-react will automatically move non React related static properties from wrappedComponent to the inject component so that all static fields are
453
- actually available to the outside world without needing `.wrappedComponent`.
454
-
455
- #### Strongly typing inject
456
-
457
- ##### With TypeScript
458
-
459
- `inject` also accepts a function (`(allStores, nextProps, nextContext) => additionalProps`) that can be used to pick all the desired stores from the available stores like this.
460
- The `additionalProps` will be merged into the original `nextProps` before being provided to the next component.
461
-
462
- ```typescript
463
- import { IUserStore } from "myStore"
464
-
465
- @inject(allStores => ({
466
- userStore: allStores.userStore as IUserStore
467
- }))
468
- class MyComponent extends React.Component<{ userStore?: IUserStore; otherProp: number }, {}> {
469
- /* etc */
470
- }
471
- ```
472
-
473
- Make sure to mark `userStore` as an optional property. It should not (necessarily) be passed in by parent components at all!
474
-
475
- Note: If you have strict null checking enabled, you could muffle the nullable type by using the `!` operator:
476
-
477
- ```
478
- public render() {
479
- const {a, b} = this.store!
480
- // ...
481
- }
482
- ```
483
-
484
- #### Testing store injection
485
-
486
- It is allowed to pass any declared store in directly as a property as well. This makes it easy to set up individual component tests without a provider.
487
-
488
- So if you have in your app something like:
489
-
490
- ```javascript
491
- <Provider profile={profile}>
492
- <Person age={"30"} />
493
- </Provider>
494
- ```
495
-
496
- In your test you can easily test the `Person` component by passing the necessary store as prop directly:
497
-
498
- ```
499
- const profile = new Profile()
500
- const mountedComponent = mount(
501
- <Person age={'30'} profile={profile} />
502
- )
503
- ```
504
-
505
- Bear in mind that using shallow rendering won't provide any useful results when testing injected components; only the injector will be rendered.
506
- To test with shallow rendering, instantiate the `wrappedComponent` instead: `shallow(<Person.wrappedComponent />)`
507
-
508
- ### disposeOnUnmount(componentInstance, propertyKey | function | function[])
509
-
510
- Function (and decorator) that makes sure a function (usually a disposer such as the ones returned by `reaction`, `autorun`, etc.) is automatically executed as part of the componentWillUnmount lifecycle event.
511
-
512
- ```javascript
513
- import { disposeOnUnmount } from "mobx-react"
514
-
515
- class SomeComponent extends React.Component {
516
- // decorator version
517
- @disposeOnUnmount
518
- someReactionDisposer = reaction(...)
519
-
520
- // decorator version with arrays
521
- @disposeOnUnmount
522
- someReactionDisposers = [
523
- reaction(...),
524
- reaction(...)
525
- ]
526
-
527
-
528
- // function version over properties
529
- someReactionDisposer = disposeOnUnmount(this, reaction(...))
530
-
531
- // function version inside methods
532
- componentDidMount() {
533
- // single function
534
- disposeOnUnmount(this, reaction(...))
535
-
536
- // or function array
537
- disposeOnUnmount(this, [
538
- reaction(...),
539
- reaction(...)
540
- ])
541
- }
542
- }
543
- ```
544
-
545
289
  ## DevTools
546
290
 
547
291
  `mobx-react@6` and higher are no longer compatible with the mobx-react-devtools.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,2 @@
1
- export { Observer, useObserver, useAsObservableSource, useLocalStore, isUsingStaticRendering, useStaticRendering, enableStaticRendering, observerBatching, useLocalObservable } from "mobx-react-lite";
1
+ export { Observer, isUsingStaticRendering, enableStaticRendering, useLocalObservable, _observerFinalizationRegistry, clearTimers } from "mobx-react-lite";
2
2
  export { observer } from "./observer";
3
- export { MobXProviderContext, Provider, ProviderProps } from "./Provider";
4
- export { inject } from "./inject";
5
- export { disposeOnUnmount } from "./disposeOnUnmount";
6
- export { PropTypes } from "./propTypes";
7
- export { IWrappedComponent } from "./types/IWrappedComponent";