I need to migrate MobX v.5
to MobX v6+
which supports react hooks, however after upgrading to MobX v6+
I get this error [MobX] Cannot obtain atom from undefined
I was able to track down that when I want to create Lazy observable with onBecomeObserved
, this throws error above for some reason, but I cannot figure out what cause this.
I use inject which is available from MobX to connect components to stores, it might not be a good pattern anymore, but its the fastest way for me how to migrate from old MobX
. withTheme
function is Material-UI
connector to theme system
export function injector<T>(target: T): T {
return inject('data', 'view')(withTheme(target));
}
export function storeConnect<T extends IReactComponent>(target: T): T {
return injector(observer(target));
}
Here is base store where I create lazy observable which should take care of loading data from API, however here my app crash and cant find out working solution to this.
abstract class BaseProvider {
@observable state = "NONE";
constructor() {
//this it throws error
onBecomeObserved(this, "isLoading", this.loadData);
}
public abstract loadData(): void;
@computed get isLoading() {
return (this.state === "Loading");
}
}
class Provider extends BaseProvider {
constructor() {
super();
this.loadData = this.loadData.bind(this);
}
public loadData() {
if (this.state !== "Loaded") {
//some code
}
}
}