dk-mobx-restore-state
v3.4.5
Published
Restores state from initial data object
Downloads
29
Readme
Library for safe merging of MobX observables
[!WARNING]
It's fine if you use this library from NPM package with a static versioning in case you want it for some pet-project or to test it's capabilities.But for production use it's strongly recommended to create a fork, because I do not write Changelogs and may break / add some functionality without notice.
Purpose
The purpose of this library is to safely restore state during SSR.
If you want to restore your state and see a problem, it may be as follows:
- You use MobX 4. It has a bug where newly added objects are not observable, like
const result = Object.assign(observable({ str: '123' }), { str: '321', obj: {} });
expect(isObservable(result.obj)).to.eq(false); // BUG in MobX 4
Nowadays MobX 5 / 6 versions do not have this bug, we can use Object.assign
. But when we speak about
classes, the behavior remains inconsistent.
- You use a class mobx store without enumerated value
class Target {
constructor() { makeAutoObservable(this); }
str = '123';
}
const result = Object.assign(new Target(), { str: '321', obj: {} });
expect(isObservable(result.obj)).to.eq(false); // BUG in all MobX versions
- You use a class mobx store without initial value
class Target {
constructor() { makeAutoObservable(this); }
str = '123';
obj?: SomeType;
}
const result = Object.assign(new Target(), { str: '321', obj: {} });
expect(isObservable(result.obj)).to.eq(false); // BUG in all MobX versions
This is very confusing and depends on transpilers (Babel, TSC, Esbuild, SWC) which all behave differently. So, this library makes everything consistent.
Now that you have fixed these errors and feel happy and still don't want dk-mobx-restore-state
...
class Target {
constructor() { makeAutoObservable(this); }
str = '123';
obj?: SomeType = undefined;
}
const result = Object.assign(new Target(), { str: '321', obj: {} });
expect(isObservable(result.obj)).to.eq(true); // No bug finally!
- You come across a problem that
Object.assign
is not a deep merge. So you can not restore a partial data like
class Target {
constructor() { makeAutoObservable(this); }
obj = { a: 1 };
}
const result = Object.assign(new Target(), { obj: { b: 2 } });
expect(result.obj.a).to.eq(undefined); // Lost some initial data!
- You try
lodash.merge
and see that it merges deeply, but with the same inconsistencies asObject.assign
. You write a customizer and feel happy finally...
mergeWith(new Target(), source, (objValue, srcValue) => {
if (objValue == null && Object.prototype.toString.call(srcValue) === '[object Object]') {
return observable(srcValue);
}
}
But... There are some edge-cases in the strategy of merging and you need to see the logs of the process...
Actually, you may consider dk-mobx-restore-state
just from the start. It is thoroughly tested, 1.8kb unminified,
handles most of edge-cases and has no deps.
Usage
Install dk-mobx-restore-state
and use it instead of Object.assign / mergeWith
where needed. Everything
will be observable
(in MobX 4 or in class objects) in all the cases mentioned above.
Syntax
restoreState({ logs, target, source })
logs
(boolean): logging of operations
target
: object that needs to be filled with observable data
source
: object with some data (may be observable or not)