Getting collisions between atoms while using next.js #2422
Unanswered
Esya
asked this question in
Bug report
Replies: 1 comment 7 replies
-
Don't quite have a full explanation here, but I did find some interesting results. Note the current return value of export function useAtomValue<Value>(atom: Atom<Value>, options?: Options) {
...
return isPromiseLike(value) ? use(value) : (value as Awaited<Value>)
} where While playing around with this example, I tried swapping this return for a consistent call to export function useAtomValueAlt<Value>(atom: Atom<Value>, options?: Options) {
...
return use(isPromiseLike(value) ? value : (async () => value as Awaited<Value>))
} which does not display the buggy behavior. const DisplayAtomsWorking = () => {
const atom1 = useAtomValueAlt(asyncFirstAtom1)
const atom2 = useAtomValueAlt(asyncFirstAtom2)
console.log("Working Alt", {atom1, atom2})
return (
<>
<h1 className="font-bold">This example works!</h1>
<div>{atom1}</div>
<div>{atom2}</div>
</>
)
} Another interesting phenomenon is that the bug seems to only occur if there are an even number of atoms called: const [asyncAtom1, asyncFirstAtom1] = makeAtomPair('This is my first message')
const [asyncAtom2, asyncFirstAtom2] = makeAtomPair('This is my second message')
const [asyncAtom3, asyncFirstAtom3] = makeAtomPair('This is my third message')
const [asyncAtom4, asyncFirstAtom4] = makeAtomPair('This is my fourth message')
const Display2AtomsBugged = () => {
const atom1 = useAtomValue(asyncFirstAtom1)
const atom2 = useAtomValue(asyncFirstAtom2)
console.log("2 Bugged", {atom1, atom2})
return (
<>
<h1 className="font-bold">This example fails</h1>
<p className="pb-10">It prints "This is my first message" twice in the server console</p>
<div>{atom1}</div>
<div>{atom2}</div>
</>
)
}
const Display3AtomsWorking = () => {
const atom1 = useAtomValue(asyncFirstAtom1)
const atom2 = useAtomValue(asyncFirstAtom2)
const atom3 = useAtomValue(asyncFirstAtom3)
console.log("3 Working", {atom1, atom2, atom3})
return (
<>
<h1 className="font-bold">This example works!</h1>
<div>{atom1}</div>
<div>{atom2}</div>
<div>{atom3}</div>
</>
)
}
const Display4AtomsWorking = () => {
const atom1 = useAtomValue(asyncFirstAtom1)
const atom2 = useAtomValue(asyncFirstAtom2)
const atom3 = useAtomValue(asyncFirstAtom3)
const atom4 = useAtomValue(asyncFirstAtom4)
console.log("4 Bugged", {atom1, atom2, atom3, atom4})
return (
<>
<h1 className="font-bold">This example fails</h1>
<p className="pb-10">It prints "This is my first message" twice in the server console</p>
<div>{atom1}</div>
<div>{atom2}</div>
<div>{atom3}</div>
<div>{atom4}</div>
</>
)
} |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In my app, I've been having some strange collisions where data from some atoms seem to pollute other atoms, all coming from the same atom factory function.
I've made a very simple reproduction codesandbox here
There's also the git repository here
When my factory generates an atom pair, during SSR, the values of the atoms get mixed up
The
makeAtomPair
function :atomWithSuspenseQuery
),When running the code below, while generating two distinct pairs, the value of the first atom "goes into" the second one as shown in the logs
(Reminder: This happens in the server console, before it's sent to the client)
I'm at a complete loss as to what could be doing this...! Also, when the issue occurs, there's referential equality between the two values for some reason that escapes me
Any help would be greatly appreciated
Beta Was this translation helpful? Give feedback.
All reactions