Replies: 1 comment
-
Those docs should probably have an Ions are just atoms. All atoms and selectors only rerun when one of their dependencies updates. In the example in the docs: const sortedUsersAtom = ion('sortedUsers', ({ get }) =>
get(usersAtom).sort((userA, userB) => userA.name.localeCompare(userB.name))
) This atom will only run when const sortedUsersAtom = ion('sortedUsers', ({ get }) => {
const users = get(usersAtom)
const sortedUsers = injectMemo(
() => users.sort((userA, userB) => userA.name.localeCompare(userB.name)),
[users]
)
return sortedUsers
}) But this is unnecessary since EDIT: Better Example & Some Context: But if the atom had more dependencies or its own store, const sortedUsersAtom = ion('sortedUsers', ({ get }) => {
const isFiltering = get(isFilteringUsersAtom)
const users = get(isFiltering ? filteredUsersAtom : usersAtom)
const sortedUsers = injectMemo(
() => users.sort((userA, userB) => userA.name.localeCompare(userB.name)),
[users]
)
return sortedUsers
}) In this example, the atom now has multiple dependencies that could all make this atom reevaluate. But we only care about re-sorting the users if the actual This is the difference between selectors and atoms. Selectors can't control whether any of the inner operations run when a particular dependency updates. The whole selector re-runs every time. Atoms give you fine-grained control via |
Beta Was this translation helpful? Give feedback.
-
I've been using selectors to control my renders a bit more as of late, and outside of the #83 bug, it's been working well.
But some of the selectors iterate pretty long lists, which isn't a problem yet but easily could be... which @bowheart addressed in the docs:
https://omnistac.github.io/zedux/docs/walkthrough/selectors#ions
Only... it talks about injectMemo, but then doesn't go about using it. Is that an oversight or because there's some implicit use here?
Beta Was this translation helpful? Give feedback.
All reactions