Skip to content

Jotai's selectorFamily creating explaination 🙏🙏 #2835

Answered by dai-shi
kaustubh2525 asked this question in Q&A
Discussion options

You must be logged in to vote

Jotai's atomFamily has a different signature from Recoil's one and should cover both atomFamily and selectorFamily use cases of Recoil. It's not the same, and there might be some capabilities missing.

import { atomFamily } from 'jotai/utils';

const baseAtom = atom({});
const myFamily = atomFamily((key) => atom((get) => get(baseAtom)[key]));

FYI, Jotai's atomFamily implementation is pretty simple. See this simplified code:
#1323 (reply in thread)

// simplified code
const atomFamily = (initializeAtom) => {
  const cache = new Map();
  return (param) => {
    if (!cache.has(param)) {
      cache.set(param, initializeAtom(param));
    }
    return cache.get(param);
  };
};

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kaustubh2525
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants