🌟Introduction

A React custom hook to check the online status of a user's device.

This version supports TypeScript

Installation

Install the package using npm or yarn

npm install re-use-online

or

yarn add re-use-online

Usage

Import the useOnlineStatus hook into your React component and use it to check the online status.

import React from "react";
import useOnlineStatus from "re-use-online";

function MyComponent() {
  const isOnline = useOnlineStatus();

  return (
    <div>
      {isOnline ? (
        <p>You are connected to the internet!</p>
      ) : (
        <p>No Internet Connection</p>
      )}
    </div>
  );
}

export default MyComponent;

How it works

The useOnlineStatus hook utilizes the navigator.onLine property to determine the initial online status of the user's device. It also adds event listeners for the online and offline events to update the online status dynamically.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Last updated