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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # mobx-react
2
2
 
3
+ ## 9.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - [`473cb3f5`](https://github.com/mobxjs/mobx/commit/473cb3f5fc8bf43abdd1c9c7857fe2820d2291fe) [#3718](https://github.com/mobxjs/mobx/pull/3718) Thanks [@mweststrate](https://github.com/mweststrate)! - - Fixed `observer` in `StrictMode` #3671
8
+ - **[BREAKING CHANGE]** Class component's `props`/`state`/`context` are no longer observable. Attempt to use these in any derivation other than component's `render` throws and error. For details see https://github.com/mobxjs/mobx/blob/main/packages/mobx-react/README.md#note-on-using-props-and-state-in-derivations
9
+ - Extending or applying `observer` classes is now explicitly forbidden
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [[`58bb052c`](https://github.com/mobxjs/mobx/commit/58bb052ca41b8592e5bd5c3003b68ec52da53f33), [`473cb3f5`](https://github.com/mobxjs/mobx/commit/473cb3f5fc8bf43abdd1c9c7857fe2820d2291fe)]:
14
+ - mobx-react-lite@4.0.3
15
+
16
+ ## 8.0.0
17
+
18
+ ### Major Changes
19
+
20
+ - [`44a2cf42`](https://github.com/mobxjs/mobx/commit/44a2cf42dec7635f639ddbfb19202ebc710bac54) [#3590](https://github.com/mobxjs/mobx/pull/3590) Thanks [@urugator](https://github.com/urugator)! - Functional components now use `useSyncExternalStore`, which should prevent tearing - you have to update mobx, otherwise it should behave as previously.<br>
21
+ Improved displayName/name handling of functional components as discussed in #3438.<br>
22
+ Reactions of uncommited class components are now correctly disposed, fixes #3492.<br>
23
+ Reactions don't notify uncommited class components, avoiding the warning, fixes #3492.<br>
24
+ Removed symbol "polyfill" and replaced with actual Symbols.<br>
25
+ Removed `this.render` replacement detection + warning. `this.render` is no longer configurable/writable (possibly BC).<br>
26
+ Class component instance is no longer exposed as `component[$mobx]["reactcomponent"]` (possibly BC).<br>
27
+ Deprecated `disposeOnUnmount`, it's not compatible with remounting.<br>
28
+
29
+ ### Patch Changes
30
+
31
+ - Updated dependencies [[`44a2cf42`](https://github.com/mobxjs/mobx/commit/44a2cf42dec7635f639ddbfb19202ebc710bac54)]:
32
+ - mobx-react-lite@4.0.0
33
+
3
34
  ## 7.6.0
4
35
 
5
36
  ### Minor Changes
package/README.md CHANGED
@@ -11,15 +11,16 @@ Exports the `observer` decorator and other utilities.
11
11
  For documentation, see the [MobX](https://mobx.js.org) project.
12
12
  This package supports both React and React Native.
13
13
 
14
- ## Choosing your version
14
+ ## Compatibility matrix
15
15
 
16
- There are currently two actively maintained versions of mobx-react:
16
+ Only the latest version is actively maintained. If you're missing a fix or a feature in older version, consider upgrading or using [patch-package](https://www.npmjs.com/package/patch-package)
17
17
 
18
- | NPM Version | Support MobX version | Supported React versions | Supports hook based components |
18
+ | NPM Version | Support MobX version | Supported React versions | Added support for: |
19
19
  | ----------- | -------------------- | ------------------------ | -------------------------------------------------------------------------------- |
20
- | v7 | 6.\* | 16.8+ | Yes |
21
- | v6 | 4._ / 5._ | 16.8+ | Yes |
22
- | v5 | 4._ / 5._ | 0.13+ | No, but it is possible to use `<Observer>` sections inside hook based components |
20
+ | v8 | 6.\* | >16.8 | Hooks, React 18.2 in strict mode |
21
+ | v7 | 6.\* | >16.8 < 18.2 | Hooks |
22
+ | v6 | 4.\* / 5.\* | >16.8 <18 | Hooks |
23
+ | v5 | 4.\* / 5.\* | >0.13 <18 | No, but it is possible to use `<Observer>` sections inside hook based components |
23
24
 
24
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:
25
26
 
@@ -57,10 +58,10 @@ Function (and decorator) that converts a React component definition, React compo
57
58
 
58
59
  #### Class Components
59
60
 
60
- When using component classes, `this.props` and `this.state` will be made observables, so the component will react to all changes in props and state that are used by `render`.
61
-
62
61
  `shouldComponentUpdate` is not supported. As such, it is recommended that class components extend `React.PureComponent`. The `observer` will automatically patch non-pure class components with an internal implementation of `React.PureComponent` if necessary.
63
62
 
63
+ Extending `observer` class components is not supported. Always apply `observer` only on the last class in the inheritance chain.
64
+
64
65
  See the [MobX](https://mobx.js.org/react-integration.html#react-integration) documentation for more details.
65
66
 
66
67
  ```javascript
@@ -87,6 +88,92 @@ class TodoView extends React.Component {
87
88
  const TodoView = observer(({ todo }) => <div>{todo.title}</div>)
88
89
  ```
89
90
 
91
+ ##### Note on using props and state in derivations
92
+
93
+ `mobx-react` version 6 and lower would automatically turn `this.state` and `this.props` into observables.
94
+ This has the benefit that computed properties and reactions were able to observe those.
95
+ However, since this pattern is fundamentally incompatible with `StrictMode` in React 18.2 and higher, this behavior has been removed in React 18.
96
+
97
+ As a result, we recommend to no longer mark properties as `@computed` in observer components if they depend on `this.state` or `this.props`.
98
+
99
+ ```javascript
100
+ @observer
101
+ class Doubler extends React.Component<{ counter: number }> {
102
+ @computed // BROKEN! <-- @computed should be removed in mobx-react > 7
103
+ get doubleValue() {
104
+ // Changes to this.props will no longer be detected properly, to fix it,
105
+ // remove the @computed annotation.
106
+ return this.props * 2
107
+ }
108
+
109
+ render() {
110
+ return <div>{this.doubleValue}</div>
111
+ }
112
+ }
113
+ ```
114
+
115
+ Similarly, reactions will no longer respond to `this.state` / `this.props`. This can be overcome by creating an observable copy:
116
+
117
+ ```javascript
118
+ @observer
119
+ class Alerter extends React.Component<{ counter: number }> {
120
+ @observable observableCounter: number
121
+ reactionDisposer
122
+
123
+ constructor(props) {
124
+ this.observableCounter = counter
125
+ }
126
+
127
+ componentDidMount() {
128
+ // set up a reaction, by observing the observable,
129
+ // rather than the prop which is non-reactive:
130
+ this.reactionDisposer = autorun(() => {
131
+ if (this.observableCounter > 10) {
132
+ alert("Reached 10!")
133
+ }
134
+ })
135
+ }
136
+
137
+ componentDidUpdate() {
138
+ // sync the observable from props
139
+ this.observableCounter = this.props.counter
140
+ }
141
+
142
+ componentWillUnmount() {
143
+ this.reactionDisposer()
144
+ }
145
+
146
+ render() {
147
+ return <div>{this.props.counter}</div>
148
+ }
149
+ }
150
+ ```
151
+
152
+ MobX-react will try to detect cases where `this.props`, `this.state` or `this.context` are used by any other derivation than the `render` method of the owning component and throw.
153
+ This is to make sure that neither computed properties, nor reactions, nor other components accidentally rely on those fields to be reactive.
154
+
155
+ This includes cases where a render callback is passed to a child, that will read from the props or state of a parent component.
156
+ As a result, passing a function that might later read a property of a parent in a reactive context will throw as well.
157
+ Instead, when using a callback function that is being passed to an `observer` based child, the capture should be captured locally first:
158
+
159
+ ```javascript
160
+ @observer
161
+ class ChildWrapper extends React.Component<{ counter: number }> {
162
+ render() {
163
+ // Collapsible is an observer component that should respond to this.counter,
164
+ // if it is expanded
165
+
166
+ // BAD:
167
+ return <Collapsible onRenderContent={() => <h1>{this.props.counter}</h1>} />
168
+
169
+ // GOOD: (causes to pass down a fresh callback whenever counter changes,
170
+ // that doesn't depend on its parents props)
171
+ const counter = this.props.counter
172
+ return <Collapsible onRenderContent={() => <h1>{counter}</h1>} />
173
+ }
174
+ }
175
+ ```
176
+
90
177
  ### `Observer`
91
178
 
92
179
  `Observer` is a React component, which applies `observer` to an anonymous region in your component.
@@ -136,8 +223,6 @@ person.name = "Mike" // will cause the Observer region to re-render
136
223
 
137
224
  ### `useLocalObservable` hook
138
225
 
139
- [User guide](https://mobx-react.js.org/state-local)
140
-
141
226
  Local observable state can be introduced by using the `useLocalObservable` hook, that runs once to create an observable store. A quick example would be:
142
227
 
143
228
  ```javascript
@@ -218,8 +303,6 @@ Use `import { PropTypes } from "mobx-react"` to import them, then use for exampl
218
303
 
219
304
  ### `Provider` and `inject`
220
305
 
221
- See also [the migration guide to React Hooks](https://mobx-react.js.org/recipes-migration).
222
-
223
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`._
224
307
 
225
308
  `Provider` is a component that can pass stores (or other stuff) using React's context mechanism to child components.
@@ -270,7 +353,7 @@ Notes:
270
353
  #### "The set of provided stores has changed" error
271
354
 
272
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.
273
- 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://mobx-react.js.org/recipes-context) directly which does not have this restriction.
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.
274
357
 
275
358
  #### Inject as function
276
359
 
@@ -398,8 +481,6 @@ public render() {
398
481
  }
399
482
  ```
400
483
 
401
- By [migrating to React Hooks](https://mobx-react.js.org/recipes-migration) you can avoid problems with TypeScript.
402
-
403
484
  #### Testing store injection
404
485
 
405
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.
@@ -1,5 +1,11 @@
1
1
  import React from "react";
2
2
  declare type Disposer = () => void;
3
+ /**
4
+ * @deprecated `disposeOnUnmount` is not compatible with React 18 and higher.
5
+ */
3
6
  export declare function disposeOnUnmount(target: React.Component<any, any>, propertyKey: PropertyKey): void;
7
+ /**
8
+ * @deprecated `disposeOnUnmount` is not compatible with React 18 and higher.
9
+ */
4
10
  export declare function disposeOnUnmount<TF extends Disposer | Array<Disposer>>(target: React.Component<any, any>, fn: TF): TF;
5
11
  export {};