enforceActions observed-to-always transition in MobX 6 incremental migration
0 reputation · 26 Sept 2024, 17:47 UTC
0 reputation · 26 Sept 2024, 17:47 UTC
An incremental MobX migration for a small React application aims to transition from decorator-based observable inference in MobX 5 to the makeAutoObservable API in MobX 6 without service interruption. The precise goal is to preserve observable consistency across coexisting store modules while aligning with MobX 6's configuration boundaries. A key uncertainty involves the enforceActions setting: keeping it at observed maintains backward compatibility but defers runtime checks, whereas switching to always treats mutations outside actions as exceptions, potentially breaking existing code paths. Additionally, makeAutoObservable inference depends on TypeScript and Babel configuration, and shared observable references between MobX 5 and MobX 6 modules can produce double tracking or missed reactions when inference rules diverge. How should the enforceActions configuration be set during incremental migration to balance compatibility with MobX 5 stores and enforcement consistency in MobX 6? What annotations or configuration adjustments prevent double tracking when cross-version store modules reference shared state? Does the opt-in decorator requirement in MobX 6 necessitate a full store rewrite, or can incremental adoption coexist with existing decorator-based definitions?
31275 reputation · 26 Sept 2024, 20:34 UTC
During an incremental migration from MobX 5 to MobX 6, set enforceActions to "observed". This maintains backward compatibility by only throwing errors when an observable is actually being observed by a reaction, preventing immediate runtime crashes in legacy MobX 5 stores that may perform direct mutations.
configure({ enforceActions: "observed" }) to allow legacy stores to coexist with new makeAutoObservable modules.@observable decorators rather than relying solely on makeAutoObservable inference to ensure consistent tracking across version boundaries.runInAction or defined actions to prepare for strict enforcement.enforceActions: "always" to guarantee state consistency.Double tracking typically occurs when a property is marked as observable by both a legacy decorator and a MobX 6 inference rule. To prevent this, ensure a shared store is instantiated once and that its definition is consistent across the application. The opt-in decorator requirement in MobX 6 does not necessitate a full rewrite; existing decorator-based stores can coexist with makeAutoObservable as long as the Babel/TypeScript configuration supports the legacy decorator syntax.
Verify the configuration at the application entry point:
import { configure } from "mobx";
configure({
enforceActions: "observed", // Transition to "always" post-migration
});
Missing Diagnostic Detail: Are there specific third-party libraries interacting with your stores that rely on direct mutation? This determines if "always" can ever be safely enabled.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.