Replies: 1 comment
-
This sounds a valid point. We can pass any import { atomWithStorage, createJSONStorage } from 'jotai/utils';
const createMyJsonStorage = (mergeState) => {
const storage = createJSONStorage(() => localStorage);
const getItem = (key) => {
const value = storage.getItem(key);
return { ...value, ...mergeState };
};
return { ...storage, getItem };
};
const anAtom = atomWithStorage('key', { foo: 123 }, createMyJsonStorage({ foo: 123 })); I wonder if there are some better ways to abstract this as a util function. There would be many merge strategies... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When using
atomWithStorage
with large objects it's common to add new fields later in development.Unfortunately even if I provide default value in
initialValue
object this is ignored asinitialValue
is only used when there is no data in storage.It would be useful if
atomWithStorage
accepted a flag or a function to merge initial and loaded state.Beta Was this translation helpful? Give feedback.
All reactions