JSPM

react-use-poll

1.1.5
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 61
  • Score
    100M100P100Q67597F
  • License MIT

React polling hook

Package Exports

  • react-use-poll
  • react-use-poll/cjs/index.js
  • react-use-poll/esm/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-use-poll) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-use-poll

Codecov Coverage GitHub

Purpose

react-use-poll is a package designed to simplify polling in functional react components.

Installation

Add to your project using npm i -S react-use-poll

How to use

The simplest possible use is below.

import React from 'react';
import usePoll from 'react-use-poll';

export default function MyComponent() {
  usePoll(() => {
    console.log('Hello world!');
  });
}

// Will log 'Hello world!' once every 5 seconds

It supports dependencies in the same way that useEffect does, to restart the poll.

import React from 'react';
import usePoll from 'react-use-poll';

export default function MyComponent({ prop1 }) {
  usePoll(() => {
    console.log('Hello world!');
  }, [prop1]);
}

// Will log 'Hello world!' once every 5 seconds

It supports asynchronous callbacks. The polling timeout will start at the point that the async function completes.

import React from 'react';
import usePoll from 'react-use-poll';

export default function MyComponent() {
  usePoll(async () => {
    await fetch();
    console.log('Hello world!');
  });
}

// Will log 'Hello world!' once every 5 seconds + however long the async function takes to respond

It supports different callback times.

import React from 'react';
import usePoll from 'react-use-poll';

export default function MyComponent() {
  usePoll(async () => {
    await fetch();
    console.log('Hello world!');
  }, [], {
    interval: 3000
  });
}

// Will log 'Hello world!' once every 3 seconds