Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

react-use-poll

1.1.2 • 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

Install

npm i react-use-poll

DownloadsWeekly Downloads

7

Version

1.1.2

License

MIT

Unpacked Size

13 kB

Total Files

15

Last publish

Collaborators

  • avatar