mobx-persist icon indicating copy to clipboard operation
mobx-persist copied to clipboard

hydrate() reacts and overwrites data in localstorage before object is rehydrated

Open evelant opened this issue 7 years ago • 0 comments

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.

evelant avatar Feb 09 '18 18:02 evelant