Skip to content

Internet Connectivity Context

A React Context provider and hook for sharing internet connectivity status across your component tree without prop drilling.

Built on top of the useCanReachToInternet hook to provide centralized connectivity monitoring.

Features

  • Centralized connectivity state: Share connectivity status across your entire app
  • No prop drilling: Access connectivity data from any component in the tree
  • Same configuration options: All useCanReachToInternet options available at provider level

Components & Hooks

CanReachToInternetCtxProvider

A context provider component that wraps your application to provide connectivity status to all child components.

It takes following props

ParameterTypeRequiredDefault ValueDescription
childrenReactNode-React children components that will have access to the connectivity context
enableNetworkPollingbooleantrueEnable automatic network polling to continuously check connectivity
networkPollingIntervalnumber3000Interval in milliseconds between network polls
testUrlstringhttps://dns.googleURL to test internet connectivity against

useCanReachToInternetCtx

A custom hook to consume the internet connectivity context values.

Return value(s):

PropertyTypeDescription
isOnlinebooleanBrowser's native online/offline status from navigator.onLine
canReachToInternetbooleanWhether the device can actually reach the internet (verified via HTTP request)
isFullyConnectedbooleanCombined status: true when both isOnline and canReachToInternet are true
isNetworkPollingEnabledbooleanCurrent state of automatic network polling
isCheckingConnectionbooleanWhether a connectivity check is currently in progress
startNetworkPolling() => voidFunction to start automatic network polling
stopNetworkPolling() => voidFunction to stop automatic network polling
forceCheckNetwork() => voidFunction to manually trigger a connectivity check
getCanReachToInternetStatus() => booleanFunction to get current internet reachability status

Usage Examples

Basic App Setup

tsx
import { CanReachToInternetCtxProvider } from 'classic-react-hooks'

function App() {
   return (
      <CanReachToInternetCtxProvider>
         <Header />
         <MainContent />
         <Footer />
      </CanReachToInternetCtxProvider>
   )
}

Custom Configuration

Example
tsx
import { CanReachToInternetCtxProvider } from 'classic-react-hooks'

function App() {
   return (
      <CanReachToInternetCtxProvider
         enableNetworkPolling={true}
         networkPollingInterval={5000}
         testUrl='https://httpbin.org/get'
      >
         <Header />
         <MainContent />
         <Footer />
      </CanReachToInternetCtxProvider>
   )
}