UseonlineStatus — A React Hook to Know When Your App Is Offline

UseonlineStatus — A React Hook to Know When Your App Is Offline

UseOnlineStatus — A React hook to know when your app is offline. We needed to be able to visually indicate if the application was online or offline. I was recently working on a client’s Progressive Web App (PWA) project and stumbled upon an interesting problem.

I was recently working on a client’s Progressive Web App (PWA) project and stumbled upon an interesting problem. I needed a reusable way to know if the app was online or offline. The ticket had the following requirements:

  • We needed to be able to visually indicate if the application was online or offline
  • We needed to handle situations where the device might be connected to a network but the internet connection is weak or not existent.

If you are up for a challenge feel free to try and do this yourself before reading the rest of this article.

Just a note, all the code samples are in Typescript but should work just the same in Javascript with the types removed.

Since we needed to use this status in multiple parts of the application I decided that a hook would be a great way to solve this problem while being reusable. If you aren’t familiar with hooks in react, the official docs are a great place to start ( https://reactjs.org/docs/hooks-intro.html). This custom hook can serve multiple purposes, as a status indicator for the user, to know if you should make an API call or when to cache information in the application.

We can solve the first problem easily by using the navigator.onLine property, available on window in most browsers ( navigator.Online Docs). It’s broadly available in nearly every browser. We’ll start with a simple hook that looks like this.

react hooks typescript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

What are hooks in React JS? - INFO AT ONE

In this article, you will learn what are hooks in React JS? and when to use react hooks? Also, we will see the react hooks example.

How to Build Meme Generator with React, React Hooks and TypeScript

The best way to learn something is by doing. It works even better if it means working on something for fun. So, how about learning about React, React hooks and TypeScript by building your own meme generator? This tutorial will show you how to do it.

Validating React Forms With React-Hook-Form

In this article we will learn how to Validating React Forms With React-Hook-Form . Validating inputs is very often required. For example, when you want to make sure two passwords inputs are the same, an email input should in fact be an email or that the input is not too long

Hooks, Hooks, Hooks!

Hooks are functions, provided by React (and completely opt-in, not required), that allows you to “hook into” React features from functional components. So you can continue to use functional components, which are inherently simpler and less bloated, and access state.