mobx-persist
mobx-persist copied to clipboard
hydrate() reacts and overwrites data in localstorage before object is rehydrated
Pseudocode
class RootStore(){
@persist('object', User) @observable myUser = new User() //has no properties yet
}
const hydrate = create({
storage: localForage,
jsonify: false,
})
const rootStore = new rootStore()
console.log("1 -- Hydrate user")
hydrate("user", rootStore.user).then(() => {
console.log("2 -- Hydrated user", rootStore.user)
})
...loadUserDataFromServer()
The issue here is that mobx-persist is persisting the empty user before the correct user data has been rehydrated from localstorage. It overwrites the correct data in localstorage with empty data. After data loads from server it writes the correct data again. It seems a guard is missing to prevent mobx-persist from writing to localstorage before it has loaded the data from localstorage for the first time.