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

254
Views
¿Cómo verificar el estado en línea en React.js?

He creado una función que comprueba si la aplicación está en línea o no. Sin embargo, según la documentación de MDN, navigator.onLine está dando falsos positivos en Chrome y Safari que estoy experimentando en este momento. ¿Hay alguna otra manera de resolver este problema actual?

useOnlineStatus.ts:

 import { useEffect, useState } from 'react' const getOnlineStatus = (): boolean => { return typeof navigator !== 'undefined' && typeof navigator.onLine === 'boolean' ? navigator.onLine : true } const useOnlineStatus = (): boolean => { const [onlineStatus, setOnlineStatus] = useState(getOnlineStatus()) const goOnline = () => setOnlineStatus(true) const goOffline = () => setOnlineStatus(false) useEffect(() => { window.addEventListener('online', goOnline) window.addEventListener('offline', goOffline) return () => { window.removeEventListener('online', goOnline) window.removeEventListener('offline', goOffline) } }, []) return onlineStatus } export default useOnlineStatus
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Una forma que podría intentar es enviar una solicitud a su propio origen para verificar si su solicitud está en línea o no.

Los parámetros de consulta (q) se aleatorizan para evitar respuestas almacenadas en caché.

 const isAppOnline = async() => { if (!window.navigator.onLine) return false const url = new URL(window.location.origin) url.searchParams.set('q', new Date().toString()) try { const response = await fetch( url.toString(), { method: 'HEAD' }, ) return response.ok } catch { return false } }
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!