3.2.14 • Published 1 month ago

dk-mobx-restore-state v3.2.14

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Library for safe merging of MobX observables

coverage npm license

!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.

MobX 4 had a bug where newly added objects were not observable, like

const result = Object.assign(observable({ str: '123' }), { str: '321', obj: {} });

expect(isObservable(result.obj)).to.deep.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 remain inconsistent:

class Target {
  constructor() { makeAutoObservable(this); }
  str = '123';
}
    
const result = Object.assign(new Target(), { str: '321', obj: {} });

expect(isObservable(result.obj)).to.deep.eq(false); // BUG in all MobX versions

class Target {
  constructor() { makeAutoObservable(this); }
  str = '123';
  obj;
}
    
const result = Object.assign(new Target(), { str: '321', obj: {} });

expect(isObservable(result.obj)).to.deep.eq(false); // BUG in all MobX versions

class Target {
  constructor() { makeAutoObservable(this); }
  str = '123';
  obj = undefined;
}
    
const result = Object.assign(new Target(), { str: '321', obj: {} });

expect(isObservable(result.obj)).to.deep.eq(true); // No bug finally!

So, if you declare fore example user?: User inside your model and then SSR gives you a serialized object like user: { name: 'John' } it will not become observable if you use Object.assign(store, SSR_DATA).

This is very confusing and depends on transpilers (Babel, TSC, Esbuild, SWC) which all behave differently. So, this library makes everything consistent.

Usage

Install dk-mobx-restore-state and use it instead of Object.assign where needed. Everything will be observable (in MobX 4 or in class objects) in all the cases mentioned above.

3.2.9

1 month ago

3.2.13

1 month ago

3.2.12

1 month ago

3.2.14

1 month ago

3.2.11

1 month ago

3.2.10

1 month ago

3.2.8

1 month ago

3.2.7

1 month ago

3.2.6

1 month ago

3.2.6-alpha.0

1 month ago

3.2.2

1 month ago

3.2.3

1 month ago

3.2.1

1 month ago

3.2.0

1 month ago

3.1.43

3 months ago

3.1.42

3 months ago

3.1.41

3 months ago

3.1.40

3 months ago

3.1.39

3 months ago

3.1.38

4 months ago

3.1.34

6 months ago

3.1.33

6 months ago

3.1.36

6 months ago

3.1.35

6 months ago

3.1.16

10 months ago

3.1.37

5 months ago

3.1.15

10 months ago

3.1.18

10 months ago

3.1.17

10 months ago

3.1.30

7 months ago

3.1.32

6 months ago

3.1.31

6 months ago

3.1.23

10 months ago

3.1.22

10 months ago

3.1.25

10 months ago

3.1.24

10 months ago

3.1.27

7 months ago

3.1.26

9 months ago

3.1.29

7 months ago

3.1.28

7 months ago

3.1.21

10 months ago

3.1.20

10 months ago

3.1.19

10 months ago

3.1.14

11 months ago

3.1.13

11 months ago

3.1.12

11 months ago

3.1.11

11 months ago

3.1.9

11 months ago

3.1.8

11 months ago