Skip to content
This repository was archived by the owner on Dec 31, 2020. It is now read-only.

Commit e727ae4

Browse files
authored
Observer batched updates from lite V2 (#787)
1 parent d1f7314 commit e727ae4

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ There are currently two actively maintained versions of mobx-react:
1919
| v6 | 16.8.0 and higher | Yes |
2020
| v5 | 0.13 and higher | No, but it is possible to use `<Observer>` sections inside hook based components |
2121

22-
The user guide covers this [in a more detail](https://mobx-react.js.org/libraries).
22+
The user guide covers this [in a more detail](https://mobx-react.js.org/libraries).
2323

2424
The V5 documentation can be found in the [README_v5](README_v5.md).
2525

@@ -319,6 +319,7 @@ Notes:
319319
- The original component wrapped by `inject` is available as the `wrappedComponent` property of the created higher order component.
320320

321321
#### "The set of provided stores has changed" error
322+
322323
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.
323324
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.
324325

@@ -520,6 +521,36 @@ And the dependency tree of a component can now be inspected by the standard devt
520521

521522
![hooks.png](hooks.png)
522523

524+
## Observer batching
525+
526+
[Check out the elaborate explanation](https://github.com/mobxjs/mobx-react/pull/787#issuecomment-573599793).
527+
528+
In short without observer batching the React doesn't guarantee the order component rendering in some cases. We highly recommend that you configure batching to avoid these random surprises.
529+
530+
Import one of these before any React rendering is happening, typically `index.js/ts`. For Jest tests you can utilize [setupFilesAfterEnv](https://jestjs.io/docs/en/configuration#setupfilesafterenv-array).
531+
532+
**React DOM:**
533+
534+
> import 'mobx-react/batchingForReactDom'
535+
536+
**React Native:**
537+
538+
> import 'mobx-react/batchingForReactNative'
539+
540+
### Opt-out
541+
542+
To opt-out from batching in some specific cases, simply import the following to silence the warning.
543+
544+
> import 'mobx-react/batchingOptOut'
545+
546+
### Custom batched updates
547+
548+
Above imports are for a convenience to utilize standard versions of batching. If you for some reason have customized version of batched updates, you can do the following instead.
549+
550+
```js
551+
import { observerBatching } from "mobx-react"
552+
observerBatching(customBatchedUpdates)
553+
523554
## FAQ
524555

525556
**Should I use `observer` for each component?**
@@ -535,14 +566,19 @@ See also [Do child components need `@observer`?](https://github.com/mobxjs/mobx/
535566
The following warning will appear if you trigger a re-rendering between instantiating and rendering a component:
536567

537568
```
569+
538570
Warning: forceUpdate(...): Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.`
571+
539572
```
540573
541574
-- or --
542575
543576
```
577+
544578
Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.
579+
545580
```
546581
547582
Usually this means that (another) component is trying to modify observables used by this components in their `constructor` or `getInitialState` methods.
548583
This violates the React Lifecycle, `componentWillMount` should be used instead if state needs to be modified before mounting.
584+
```

batchingForReactDom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("mobx-react-lite/batchingForReactDom")

batchingForReactNative.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("mobx-react-lite/batchingForReactNative")

batchingOptOut.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("mobx-react-lite/batchingOptOut")

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"typescript": "^3.7.0"
9090
},
9191
"dependencies": {
92-
"mobx-react-lite": "^1.4.2"
92+
"mobx-react-lite": "2"
9393
},
9494
"files": [
9595
"dist"
@@ -138,4 +138,4 @@
138138
],
139139
"testURL": "http://127.0.0.1/"
140140
}
141-
}
141+
}

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ if (!observable) throw new Error("mobx-react requires mobx to be available")
88
if (typeof rdBatched === "function") configure({ reactionScheduler: rdBatched })
99

1010
export {
11-
isUsingStaticRendering,
1211
Observer,
1312
useObserver,
1413
useAsObservableSource,
1514
useLocalStore,
16-
useStaticRendering
15+
isUsingStaticRendering,
16+
useStaticRendering,
17+
observerBatching,
18+
observerBatchingOptOut,
19+
isObserverBatched
1720
} from "mobx-react-lite"
1821

1922
export { observer } from "./observer"

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7189,10 +7189,10 @@ [email protected], [email protected], mkdirp@^0.5.1, mkdirp@~0.5.1:
71897189
dependencies:
71907190
minimist "0.0.8"
71917191

7192-
mobx-react-lite@^1.4.2:
7193-
version "1.5.2"
7194-
resolved "https://registry.yarnpkg.com/mobx-react-lite/-/mobx-react-lite-1.5.2.tgz#c4395b0568b9cb16f07669d8869cc4efa1b8656d"
7195-
integrity sha512-PyZmARqqWtpuQaAoHF5pKX7h6TKNLwq6vtovm4zZvG6sEbMRHHSqioGXSeQbpRmG8Kw8uln3q/W1yMO5IfL5Sg==
7192+
mobx-react-lite@2:
7193+
version "2.0.0"
7194+
resolved "https://registry.yarnpkg.com/mobx-react-lite/-/mobx-react-lite-2.0.0.tgz#52ee837a81b4a4cd02fe4a2e6e9433da89b6f9a5"
7195+
integrity sha512-IrA0WR8f15hq1BLnNfgtQQ0Gp9zRASLZxXEVfiJv8uTwj1K/wN7vHAJtr9YJyBFia0W6QUAXFPP0PHdV0M/L9g==
71967196

71977197
mobx@^5.15.4:
71987198
version "5.15.4"

0 commit comments

Comments
 (0)