D
D
deantek2021-08-09 17:49:50
React
deantek, 2021-08-09 17:49:50

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
}


and the component itself:

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


Please tell me, preferably with an example

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2021-08-09
@deantek

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 question

Ask a Question

731 491 924 answers to any question