npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-raf-polling

v1.0.3

Published

Api polling hook for react

Downloads

1,269

Readme

use-raf-polling

Overview

use-raf-polling is a lightweight npm package that provides a simple and intuitive hook for polling in React applications. It allows you to repeatedly execute a callback function at a specified interval, and provides convenient features for controlling the polling behavior.

Here are some scenarios where use-raf-polling could come in handy:

  • Data Visualization: usePolling can be used to update visual data representations, such as charts, graphs, or maps, at regular intervals.
  • Background Data Sync: usePolling can be used to sync data between a client and server on a regular basis, for instance updating data in a real-time monitoring application.
  • Real-time Notifications: usePolling can be used to check for new updates or notifications on a web page without the user having to manually refresh the page.

Features and benefits

  • Easy to use: use-raf-polling provides a straightforward way to implement polling in your React components, with no complex configuration required.
  • Flexible: you can customize the polling interval, polling condition, callback functions to suit your specific use case.
  • Efficient: use-raf-polling uses requestAnimationFrame to ensure smooth and performant polling, without blocking the main thread. Besides, the callback function will not be executed, for example, when the browser tab is not active. This can help reduce the performance impact.
  • Reliable: the hook includes error handling and retry logic.

Usage

Here's a basic example of how to use use-raf-polling:

import { usePolling, UsePollingProps } from 'react-polling-hook'

const MyComponent = () => {
  const pollingProps: UsePollingProps = {
    callback: async () => {
      // Your polling logic here
    },
    condition: true, // Use this property to start/stop polling. If set to 'true', the API polling will start immediately 
    interval: 5000, // Set this to your desired polling interval in milliseconds
    onPollingFailed: () => {
      console.error("Polling failed!") // Handle failed polling requests here
    },
    onPollingSucceed: () => {
      console.log("Polling succeeded!") // Handle successful polling requests here
    }
  }
  const {
    isPolling,
    restartPolling,
    startPolling,
    stopPolling
  } = usePolling(pollingProps)

  return (
    <div>
      <p>Polling is currently {isPolling ? 'enabled' : 'disabled'}</p>
      <button onClick={startPolling}>Start polling</button>
      <button onClick={stopPolling}>Stop polling</button>
      <button onClick={restartPolling}>Restart polling</button>
    </div>
  )
}

For more information of usage, please refer to the example folder.
Also, check the demo.

Installation

To install use-raf-polling in your project, simply run:

# If you use npm:
npm install use-raf-polling

# Or if you use Yarn:
yarn add use-raf-polling

And then import it in your React components as shown in the above example.

License

This package is distributed under the MIT License.