react-use-poll
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

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

Package Sidebar

Install

npm i react-use-poll

Weekly Downloads

40

Version

1.1.5

License

MIT

Unpacked Size

26 kB

Total Files

26

Last publish

Collaborators

  • nickheal