Select slice of atom with dynamic data #320
-
Is there a way to select data based on data coming from another hook? Like this data structure {
"be4b21e0-bd0a-4b7e-9e17-a8da4d003634": {
"name": "sweater",
"quantity": 2
}
}
const Product = () => {
const productId = useProductId();
const [quantityOfProductInCart] = useAtom(???) // need to pass down the the product id
|
Beta Was this translation helpful? Give feedback.
Answered by
dai-shi
Feb 26, 2021
Replies: 1 comment 1 reply
-
Yes, in this case you create a new atom in render. const Product = () => {
const productId = useProductId();
const fooAtom = useMemo(() => atom((get) => get(barAtom)[productId].quantity), [productId]);
const [quantityOfProductInCart] = useAtom(fooAtom) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
geoffreydhuyvetters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.