-
-
Couldn't load subscription status.
- Fork 1.8k
Fix #3648: observableRequiresReaction/computedRequiresReaction shouldn't warn with enableStaticRendering(true)
#3649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
fbbc2db
e9c3aee
b05595b
aac2142
47beb21
76806b2
17c6f2d
d33d2c7
452832f
5688e72
2a6190b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "mobx-react": patch | ||
| "mobx-react-lite": patch | ||
| --- | ||
|
|
||
| fix #3648: `observableRequiresReaction`/`computedRequiresReaction` shouldn't warn with `enableStaticRendering(true)` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "mobx": patch | ||
| --- | ||
|
|
||
| `computedRequiresReaction` respects `globalState.allowStateReads` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,8 @@ import { | |
| computed, | ||
| observable, | ||
| transaction, | ||
| makeObservable | ||
| makeObservable, | ||
| configure | ||
| } from "mobx" | ||
| import { withConsole } from "./utils/withConsole" | ||
| /** | ||
|
|
@@ -1037,3 +1038,30 @@ test("SSR works #3448", () => { | |
|
|
||
| expect(consoleWarnMock).toMatchSnapshot() | ||
| }) | ||
|
|
||
| test("#3648 enableStaticRendering doesn't warn with observableRequiresReaction/computedRequiresReaction", () => { | ||
| consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {}) | ||
| try { | ||
| enableStaticRendering(true) | ||
| configure({ observableRequiresReaction: true, computedRequiresReaction: true }) | ||
| const o = observable.box(0, { name: "o" }) | ||
| const c = computed(() => o.get(), { name: "c" }) | ||
|
|
||
| @observer | ||
| class TestCmp extends React.Component<any> { | ||
| render() { | ||
| return o.get() + c.get() | ||
| } | ||
| } | ||
|
|
||
| const { unmount, container } = render(<TestCmp />) | ||
| expect(container).toHaveTextContent("0") | ||
| unmount() | ||
|
|
||
| expect(consoleWarnMock).toMatchSnapshot() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous comment |
||
| } finally { | ||
| enableStaticRendering(false) | ||
| _resetGlobalState() | ||
| consoleWarnMock.mockRestore() | ||
| } | ||
| }) | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -90,9 +90,9 @@ export class MobXGlobals { | |||||||||
|
|
||||||||||
| /** | ||||||||||
| * Is it allowed to read observables at this point? | ||||||||||
| * Used to hold the state needed for `observableRequiresReaction` | ||||||||||
| * Used to hold the state needed for `observableRequiresReaction`/`computedRequiresReaction` | ||||||||||
| */ | ||||||||||
| allowStateReads = true | ||||||||||
| allowStateReads = false | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a breaking change? By default we allow state reads outside reactive contexts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. These flags, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check, that adds up. I haven't been working on this for too long 😅. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out it's more complicated. I think it was originally designed as described, but it was maybe misunderstood and changed with the introduction of mobx/packages/mobx/src/core/derivation.ts Lines 140 to 143 in 13a222e
Notice there is a bug - the check doesn't respect enforceActions: "never" (enforceActions === false).This bug is actually a reason why the test for autoAction warning is passing - there is a test above that calls mobx.configure({ enforceActions: "never" }).If you fix this bug, the autoAction warning (Side effects like changing state are not allowed) can never occur.
|
||||||||||
|
|
||||||||||
| /** | ||||||||||
| * If strict mode is enabled, state changes are by default not allowed | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.