Skip to content

Select slice of atom with dynamic data #320

Closed Answered by dai-shi
geoffreydhuyvetters asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, in this case you create a new atom in render.
This is an expected use case, but it requires useMemo to get a stable reference.

const Product = () => {
  const productId = useProductId();
  const fooAtom = useMemo(() => atom((get) => get(barAtom)[productId].quantity), [productId]);
  const [quantityOfProductInCart] = useAtom(fooAtom)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@geoffreydhuyvetters
Comment options

Answer selected by geoffreydhuyvetters
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