-
Hi @dai-shi, Is it possible to create a new kind of atom called sync atom or reactive atom which can apply const namesAtom = atom([]);
const descriptionsAtom = atom([]);
const syncDescriptionsAtom = atom(
(get, set) => {
const names = get(namesAtom);
const descriptions = get(descriptionsAtom);
if (names.length === descriptions.length) {
return;
}
const newDescriptions = [...descriptions];
while (newDescriptions.length > names.length) {
newDescriptions .pop();
}
while (newDescriptions.length < names.length) {
newDescriptions .push("");
}
set(descriptionsAtom, newDescriptions);
}
); In this example, the descriptions will always be synced based on the value of names and the current value of descriptions. As you can see, the atom function only has one parameter function which contains the Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
No, the read function of atom must be "pure", no side effects like writing to atoms. You should prefer deriving atoms. If descriptionsAtom can simply be derived from namesAtom, it's the best. If it requires another state, create a new atom, and derive from two atoms. |
Beta Was this translation helpful? Give feedback.
No, the read function of atom must be "pure", no side effects like writing to atoms. You should prefer deriving atoms. If descriptionsAtom can simply be derived from namesAtom, it's the best. If it requires another state, create a new atom, and derive from two atoms.