-
Using I have a word game where the current date is used as a RNG seed to initialize the game state. const words = [ 'list', 'of', 'words' ];
const dailySeedAtom = atomWithStorage("dailySeed", getCurrentDateString())
const todaysWord = atom(get => chooseWord(words, get(dailySeedAtom)));
const guessedWordsAtom = atomWithStorage([]); The game state will persist in local storage so that the game state is preserved across reloads. The game state will be reset whenever the setInterval(() => {
if (store.get(dailySeedAtom) !== getCurrentDateString()) {
store.set(dailySeedAtom, getCurrentDateString());
}
}, 60000);
store.sub(dailySeedAtom, () => {
store.set(guessedWordsAtom, []);
}) However, if a user visits and plays the game (leaving before midnight), and then visits another day, I want to detect that their persisted game state is no longer valid and should be reset. But the I can solve this outside of jotai by handling the localStorage code myself. But I wanted to question whether it would be worth adding an option for |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
@dai-shi i can create a pr for this fix if you're cool with it |
Beta Was this translation helpful? Give feedback.
-
That sounds good to me. I would use the different value for the initial value and the value for the unchanged state. So, maybe: const dailySeedAtom = atomWithStorage<string | null>("dailySeed", null) |
Beta Was this translation helpful? Give feedback.
-
It's a kind of trade-off because the other way will be unintuitive for others. |
Beta Was this translation helpful? Give feedback.
-
Yes, I have the same problem. At the moment, I got a empty local storage, and i have this: const quoteIdAtom = atomWithStorage('quoteid', '0'); In my template, i got:
And if I click 1st button and the the 2nd button, localstorage key of 'quoteId' will set to undefined. But quoteId will set to 'Symbol(RESET)'. Atm, I have to manually do it like:
|
Beta Was this translation helpful? Give feedback.
Thanks for asking. Well, we are not very comfortable with complicating atomWithStorage (it feels to me it's already too complicated and I'd even suggest trying a simpler alternative, https://fosstodon.org/@daishi/110993179854190377 ), so if it's solvable outside, it should be done so.