If I use Atom on a mounted component, how can I avoid multiple reads when dependencies are updated? #2782
Replies: 2 comments 9 replies
-
I'm not sure if I follow 100%, but can you define a write-only atom to prevent it? |
Beta Was this translation helpful? Give feedback.
-
There are several read-only atoms that communicate with the backend. They use multiple common atoms (dependencies) for fetching data. For example: export const fetch1 = atom(async (get) => {
const atomData1 = await get(atom1);
const atomData2 = await get(atom2);
return await fetchData1(atomData1, atomData2);
});
export const fetch2 = atom(async (get) => {
const atomData1 = await get(atom1);
const atomData2 = await get(atom2);
return await fetchData2(atomData1, atomData2);
}); It’s very convenient that updating When If there are cases where an atom is read multiple times and others where it’s not, please let me know.🙇♂️ |
Beta Was this translation helpful? Give feedback.
-
Hi
When using Atom on a mounted component
When you update a dependency, it will be read multiple times.
So I'm trying to avoid this by following these steps
Is there another way?
*I especially want to prevent multiple fetches.
step
another idea(No Tried)
Read dependent atoms and if the value has not changed, create an atom that returns early with the previously used value
*It may be possible to be read multiple times but not be fetched multiple times
Tried and tested code
*Uncomment out console.log as needed to confirm
Beta Was this translation helpful? Give feedback.
All reactions