Skip to content

Zedux-like selectors #2596

Answered by dai-shi
IanDuncanson asked this question in Q&A
Jun 3, 2024 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

We used to have useSelector, but selectAtom is more capable, so unified.
However, selectAtom isn't recommended except for escape hatches, and what's recommended is derived atoms.

Here's what I would do:

const useAtomSelector = (selector, param) => useAtomValue(
  useMemo(() => atom((get) => selector(get, param)), [selector, param])
)

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@IanDuncanson
Comment options

@dai-shi
Comment options

@IanDuncanson
Comment options

Answer selected by IanDuncanson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants