Git Product home page Git Product logo

react-polling-hook's Introduction

๐Ÿ”” react-polling-hook

All Contributors

Build Status

โš ๏ธ Not yet published on npm and is not being currently developed. If you still need it, please fork the repo.

Easy to use polling service built with react that uses the new React Hooks(Note: This is an alpha release)

Note: Read more about react hooks here React hooks

๐Ÿšš Installation

yarn add react-polling-hook

or

npm i react-polling-hook --save

โšก๏ธ Usage

import React, { Fragment } from 'react';
import { usePolling } from 'react-polling-hook';

const App = () => {
  const [isPolling, startPolling, stopPolling] = usePolling({
    url: 'url to poll',
    interval: 3000, // in milliseconds(ms)
    retryCount: 3, // this is optional
    onSuccess: () => console.log('handle success'),
    onFailure: () => console.log('handle failure'), // this is optional
    method: 'GET',
    headers: "headers object", // this is optional
    body: JSON.stringify(data) // data to send in a post call. Should be stringified always
  });

  return (
    <div className="App">
      {isPolling ? (
        <Fragment>
          <div> Hello I am polling</div>
          <button onClick={stopPolling}>Stop Polling</button>
        </Fragment>
      ) : (
        <Fragment>
          <div> Hello I have stopped polling</div>
          <button onClick={startPolling}>Start Polling</button>
        </Fragment>
      )}
    </div>
  );
}

๐Ÿ“’ Api

๐Ÿ”” react-polling-hook

Props Type Default Description
url string null url/api to poll
interval number 3000 Interval of polling
retryCount number 0 Number of times to retry when an api polling call fails
onSuccess function - Callback function on successful polling. This should return true to continue polling
onFailure function () => {} Callback function on failed polling or api failure
method string GET HTTP Method of the api to call
headers object - Any specific http headers that need to be sent with the request
body object - The data that need to be sent in a post/put call

onSuccess (required)

This function will be called every time the polling service gets a successful response. You should return true to continue polling and false to stop polling. It has the following signature:

function onSuccess(response) {
  // You can do anything with this response, may be add to an array of some state of your react component
  // return true to continue polling
  // return false to stop polling
}

onFailure (not compulsory field)

This function will be called every time the polling service gets a failure response from the api, it can be 401 or 500 or any failure status code. You can do some cleaning up of your variables or reseting the state here.

function onFailure(error) {
  // You can log this error to some logging service
  // clean up some state and variables.
}

๐Ÿ‘ Contribute

Show your โค๏ธ and support by giving a โญ. Any suggestions and pull request are welcome !

๐Ÿ“ License

MIT ยฉ viveknayyar

๐Ÿ‘ท TODO

  • Complete README
  • Add Examples and Demo
  • Test Suite

Contributors

Thanks goes to these wonderful people (emoji key):


Vivek Nayyar

๐Ÿ“– ๐Ÿ’ป ๐ŸŽจ ๐Ÿ’ก

This project follows the all-contributors specification. Contributions of any kind welcome!

react-polling-hook's People

Contributors

dependabot[bot] avatar finnyfound avatar vivek12345 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.