Skip to content

Commit feab5e9

Browse files
committed
Fix optional react-dom loading in mobx-react-lite
1 parent 2a069e4 commit feab5e9

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
afterEach(() => {
2+
jest.dontMock("react-dom")
3+
jest.resetModules()
4+
})
5+
6+
test("does not fail to load when react-dom is unavailable", () => {
7+
jest.resetModules()
8+
jest.doMock("react-dom", () => {
9+
throw Object.assign(new Error("Cannot find module 'react-dom'"), {
10+
code: "MODULE_NOT_FOUND"
11+
})
12+
})
13+
14+
expect(() => require("../src/index.ts")).not.toThrow()
15+
})
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
export { unstable_batchedUpdates } from "react-dom"
1+
import { defaultNoopBatch } from "./observerBatching"
2+
3+
declare const require:
4+
| ((moduleName: string) => { unstable_batchedUpdates?: typeof defaultNoopBatch })
5+
| undefined
6+
7+
export let unstable_batchedUpdates = defaultNoopBatch
8+
9+
if (typeof require === "function") {
10+
try {
11+
const reactDom = require("react-dom")
12+
if (reactDom.unstable_batchedUpdates) {
13+
unstable_batchedUpdates = reactDom.unstable_batchedUpdates
14+
}
15+
} catch {
16+
// react-dom is optional. React 18+ batches updates automatically, so fall back to no-op
17+
// batching when react-dom isn't available (for example in React Native or server-only installs).
18+
}
19+
}

0 commit comments

Comments
 (0)