Skip to content

add builtin weakmap version of atomFamily? #2239

Answered by dai-shi
himself65 asked this question in General
Discussion options

You must be logged in to vote

I have been interested in the idea of weakAtomFamily. It can be the Jotai way.
Would it be pretty simple?

export function weakAtomFamily(initializeAtom) {
  const atoms = new WeakMap();
  return (obj) => {
    if (!atoms.has(obj)) {
      atoms.set(obj, initializeAtom(obj));
    }
    return atoms.get(obj);
  };
}

Maybe, it's better in a recipe.
(tbh, I'd like to move atomFamily to recipes in docs, if possible.)

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@grzesiek-ds
Comment options

@dai-shi
Comment options

@jaens
Comment options

@dai-shi
Comment options

@jaens
Comment options

Answer selected by himself65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants