Answer the question
In order to leave comments, you need to log in
How to throw id into react-query request?
Hello, I use the react-query library, but I just can’t figure out how to throw the id into the request
, I have an array with id-shki and a request like:
const fetchPartners = async (id: number) => {
const { data } = await axios.get(`/api/v1/shop/partner/${id}`)
return data
}
const PartnersPageMobile = () => {
const history = useHistory()
const { data: partnerData } = useShop() // здесь я достаю id
const part = useMemo(() => (partnerData && partnerData.partners ? partnerData.partners : []), [
partnerData,
])
const {data: fetchPartners, isLoading} = useQuery(['partner'], fetchPartners)
// тут мне нужно как-то передать id, чтобы получить массив с товарами,
но я не понимаю как это сделать
const Header = () => {
return (
<div className={s.header_container}>
<div className={s.left}>
<div onClick={() => history.goBack()} className={s.img_block}>
<img loading="lazy" src="/images/icons/back-icon.svg" alt="" />
</div>
</div>
<div className={s.center}>ПАРТНЕРЫ</div>
<div className={s.right} />
</div>
)
}
return (
<div>
{Header()}
<div className={s.container}>
{part.map((partner: any) => {
return <div className={s.partner}>ds</div>
})}
</div>
</div>
)
}
export default PartnersPageMobile
Answer the question
In order to leave comments, you need to log in
The same value that comes as the first parameter in useQuery will be passed to fetchPartners.
that's why you do it
const fetchPartners = async (params) => {
const id = params.queryKey[1]
const { data } = await axios.get(`/api/v1/shop/partner/${id}`)
return data
}
...
const {data: fetchPartners, isLoading} = useQuery(['partner', id], fetchPartners)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question