-
Here's a simple example. The focus is on the code in the // primitive atom
export const activeAtom = atom({});
// write-only atom, In fact, it can be defined as just atoms
// but for the sake of observation and comparison,
// it is deliberately written as read-write atom
export const activeDerivedAtom = atom(
(get) => {
const list = get(listAtom);
if (list.length > 0) {
return list[list.length - 1];
}
return activeAtom;
},
(get, set, _update) => {
const list = get(listAtom);
set(activeAtom, list[list.length - 1]);
}
); After defining the code above, I observe that their behavior is very different, and the primitive atom const onClick = async () => {
await setTimeout(() => {
setList({
name: `New Item ${formatSecond(new Date())}`,
id: Date.parse(new Date().toString())
});
// Not working
setActive(list[list.length - 1]);
// It works
setActiveDerived();
console.log("comparison", active, activeDerived);
}, 1000);
}; This leads me to be very confused, want to find the answer to the question here in you. Maybe there is an obvious answer to the question I asked, but I'm sorry I didn't get it. |
Beta Was this translation helpful? Give feedback.
Answered by
dai-shi
Sep 7, 2021
Replies: 1 comment 6 replies
-
|
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
liby
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
return get(activeAtom)
in helper.ts:L31??