Nested atoms in atomWithStorage #2978
-
It seems like nested atoms aren't supported with "atomWithStorage"? This seems like a trivial fix inside of createJSONStorage, is there some sort of gotcha that I'm missing? Possibly the performance impact of having to recurse the stored atom in order to serialize and deserialize the nested atoms within (could be opt in?). Here is the sort of code I want to write, I guess "split" is the way it would be written, but I think splitting up the storage would be more efficient for larger arrays with large objects (especially in my use case where I want to synchronize the storage with a backend, so sending smaller updates when updating a single nested atom would be preferred). //would serialize to something like `["atom:todo-0", "atom:todo-1"]`
const todos = atomWithStorage<Atom<Todo>[]>("todos", [], storage);
const addTodo = (title: string) => {
const id = createId();
const todo = {id, title};
//would serialize to `{id: 0, title: "something"}`
const atom = atomWithStorage<Todo>(`todo-${id}`, todo, storage);
store.set(todos, async arr => [...(await arr), atom]);
}; If the nested atom isn't an "atomWithStorage" then maybe it could serialize in place e.g |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
If this is something that gels, I would be happy to submit a pr |
Beta Was this translation helpful? Give feedback.
-
How does the trivial fix would look like? Notice that |
Beta Was this translation helpful? Give feedback.
-
I hope some solutions will be provided from |
Beta Was this translation helpful? Give feedback.
anyone else who wants this functionality please see:
https://github.com/morganbarrett/jotai-persistence