Jotai's selectorFamily creating explaination 🙏🙏 #2835
-
I just wanna know the explaination on how would i create a selectorFamily in jotai in such a way just like recoil's selectorFamily which takes in the args and specifically derives the piece of data / state for some usage with a small example ?? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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: // simplified code
const atomFamily = (initializeAtom) => {
const cache = new Map();
return (param) => {
if (!cache.has(param)) {
cache.set(param, initializeAtom(param));
}
return cache.get(param);
};
}; |
Beta Was this translation helpful? Give feedback.
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.
FYI, Jotai's atomFamily implementation is pretty simple. See this simplified code:
#1323 (reply in thread)