Storing a basic object #1174
-
Imagine we are to fetch lots of information from the database every time the user goes back to the main page. With jotai, everytime you update an atom with a new value, the observing components will re-render. So we need to split the data into seperate atoms, so that when an information has changed, we only notify the components that need that information. So you may say, in order to have the best performances, one can just split the object into smaller atoms for more granular updates of the UI. However, there are many cases where it could become very unpractical to split your object into many atoms, for example for your user information (can be a good chunk of data), so we store it "as-is" in an atom. Given those 2 constraints, it feels like "selectAtom" would be the best choice to read from the objects. But it feels very redux way, and will require "deepEqual" comparison strategy in most cases to really work, which seems not ideal in terms of performances, especially when updates will be frequent. Is there any other way to handle this gracefully? Thank you! EDIT: I got a second thought on this. If i'm correct, it's as simple as the next statements:
Thinking with 2 ways, that I can do. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Have you seen https://jotai.org/docs/advanced-recipes/large-objects?
const derivedAtom = selectAtom(baseAtom, x => x * 2);
// can be written as
const derivedAtom = atom((get) => get(baseAtom) * 2);
|
Beta Was this translation helpful? Give feedback.
Have you seen https://jotai.org/docs/advanced-recipes/large-objects?
selectAtom
is actually only meaningful if you needequalityFn
, and it's an escape hatch.focusAtom
can create a writable atom.selectAtom
can create only a read-only atom.