-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is atomFamily supported? #33
Comments
Have done some more digging.
Here's what the Babel plugin outputs for the same input as the test above: export const one = globalThis.jotaiAtomCache.get("/Users/martin/src/state-management/src/jotai.tsx/one", atom(0));
one.debugLabel = "one";
export const two = globalThis.jotaiAtomCache.get("/Users/martin/src/state-management/src/jotai.tsx/two", atomFamily(() => atom(1)));
two.debugLabel = "two"; so presumably the test case I wrote should pass. |
I wonder if the babel plugin version works as expected. To make it work, we would probably need |
It seems like it does; I wrote a quick test for it, in order to better understand how the hot-reload persisting was supposed to work. Basically this: const testFamily = atomFamily((n: number) => atom(0));
const Buttons = () => {
const [v0, set0] = useAtom(testFamily(0));
const [v1, set1] = useAtom(testFamily(1));
const [v2, set2] = useAtom(testFamily(2));
return (
<>
<button onClick={() => set0((c) => c + 1)}>{v0}</button>
<button onClick={() => set1((c) => c + 1)}>{v1}</button>
<button onClick={() => set2((c) => c + 1)}>{v2}</button>
</>
);
}; The buttons increment their labels when clicked, and the labels persist through hot-reloads. |
Update on the test case: adding |
I hadn't understood that I needed to add the names of my custom atom wrappers to the However, I also had to change some of my "higher order atom" functions, from export const atomFamily2 = <In, Out, At extends Atom<Out>>(fn: (i: In) => At) =>
atomFamily(fn, deepEqual); which outputs (note the missing const atomFamily2 = globalThis.jotaiAtomCache.get("/Users/martin/src/albatross/src/utils/jotai.ts/atomFamily2", atomFamily(fn, deepEqual));
atomFamily2.debugLabel = "atomFamily2"; to export const atomFamily2 = <In, Out, At extends Atom<Out>>(
fn: (i: In) => At,
) => {
const af = atomFamily(fn, deepEqual);
return af;
}; which outputs export const atomFamily2 = (fn)=>{
const af = atomFamily(fn, deepEqual);
af.debugLabel = "af";
return af;
}; |
I'll close this, since #36 is a better reproduction of the problems I had. |
The
atomFamily
utility is a useful function, but it seems thatswc-jotai
either doesn't support it altogether, or that it is buggy. Tests doesn't mention it, and I tried to write a quick test:which outputs
Two things:
export
is removed for some reason.get
is justatom(1)
and nothing elseFirst time looking at the codebase just now, so not sure if this is intentional or what, but it looks like a bug.
The text was updated successfully, but these errors were encountered: