D
D
Danil Zinchenko2020-06-04 21:31:04
React
Danil Zinchenko, 2020-06-04 21:31:04

How to keep store in react mobx even on page reload?

I tried to use this solution, which saves the store at a certain time interval, but for some reason a new element does not appear in localstorage.

const store = observable({
    players: [
        "Player 1",
        "Player 2",
    ],
    // ...
})

reaction(() => JSON.stringify(store), json => {
    localStorage.setItem('store',json);
}, {
    delay: 500,
});

let json = localStorage.getItem('store');
if(json) {
    Object.assign(store, JSON.parse(json));
}


Why might this method not work for me?

And once again the question:
Is it possible to store data somewhere in mobx (not in localstorage), which will be saved when the page is reloaded?
I thought that the repository meant to solve this problem immediately, and if necessary, it can be cleared on page reload.
Maybe some extra. mobx library?

I will be glad to help and solutions.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question