Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
react-query no deja de intentar recuperar una API

Quiero implementar este escenario mediante react-query :

Mi componente obtiene una API y debe intentarlo una vez cuando se desconectó Internet del cliente y nunca volver a recuperar si se volvió a conectar Internet... y después de 3 segundos si el reintento no tuvo éxito, debería mostrar un error con un botón para volver a intentar la solicitud.

 const URL = 'https://randomuser.me/api/?results=5&inc=name'; const Example = () => { const { error, data, isLoading, refetch } = useQuery('test', () => fetch(URL).then(response => response.json()).then(data => data.results), { refetchOnWindowFocus: false, refetchOnReconnect: false, retry: 1, retryDelay: 3000 }); if (isLoading) return <span>Loading...</span> if (error) return <span>Error: {error.message} <button onClick={refetch}>retry</button></span> return ( <div> <h1>Length: {data ? console.log(data.length) : null}</h1> <button onClick={refetch}>Refetch</button> </div> ) }

Al considerar el código anterior, configuré refetchOnReconnect: false para deshabilitar la recuperación después de que se conectó a Internet, retry: 1 para configurar una vez intentar y retryDelay: 3000 para establecer un límite para el tiempo de reintento.

Pero cuando uso Throttling -> offline en DevTools, después de hacer clic en el botón solo muestra el último resultado y no muestra el error y el botón de reintento después de 3 segundos...

Entonces, ¿hay alguna forma de manejar esta función?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

React-query está usando los datos en el caché, debe invalidar la consulta para obtener los datos nuevamente llamando a la función invalidateQueries :

 onst URL = 'https://randomuser.me/api/?results=5&inc=name' const Example = () => { // Get QueryClient from the context const queryClient = useQueryClient() const { error, data, isLoading, refetch } = useQuery( 'test', () => fetch(URL) .then(response => response.json()) .then(data => data.results), { refetchOnWindowFocus: false, refetchOnReconnect: false, retry: 1, retryDelay: 3000 } ) const buttonClickHandler = () => queryClient.invalidateQueries('test') // <=== invalidate the cache if (isLoading) return <span>Loading...</span> if (error) return ( <span> Error: {error.message} <button onClick={refetch}>retry</button> </span> ) return ( <div> <h1>Length: {data ? console.log(data.length) : null}</h1> <button onClick={buttonClickHandler}>Refetch</button> </div> ) }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!