Is it a good idea to create a weakmap atom? #2991
-
I'm building a simple canvas application with jotai. const elementWithCanvasCacheAtom = atom(
new WeakMap<IElement, IElementWithCanvas>(),
);
// export const getElementCanvasFromCacheAtom = atom((get) =>
// get(elementWithCanvasCacheAtom).get(),
// );
export function getElementCanvasFromCache(element: IElement) {
return atom((get) => get(elementWithCanvasCacheAtom).get(element));
} I don't know if it's recommended, I just went through the documents quickly. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you want to have separate weakmaps per store, you should do: atom(() => new WeakMap()) This notation will ensure that the weakmap isn't the same across all stores. I'm not sure of the benefit of putting a weakmap in an atom (unless you want unique weakmaps for each store). Since the reference to the weakmap never changes, you don't really need reactivity. A global weakmap should be sufficient. |
Beta Was this translation helpful? Give feedback.
If you want to have separate weakmaps per store, you should do:
This notation will ensure that the weakmap isn't the same across all stores.
I'm not sure of the benefit of putting a weakmap in an atom (unless you want unique weakmaps for each store). Since the reference to the weakmap never changes, you don't really need reactivity. A global weakmap should be sufficient.