πŸ”” Polling an api made easy with react-polling
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.vscode πŸŽ‰ react-ping with all the setup needed Sep 25, 2018
__mock_data__
dist
src
.babelrc πŸŽ‰ react-ping with all the setup needed Sep 25, 2018
.eslintignore πŸŽ‰ react-ping with all the setup needed Sep 25, 2018
.eslintrc.js
.gitignore πŸŽ‰ react-ping with all the setup needed Sep 25, 2018
.nvmrc
.prettierrc
.travis.yml
LICENSE πŸš€ added license and updated readme Sep 27, 2018
README.md
package.json
polyfills.js βœ… all tests added Oct 8, 2018
setupTests.js
size.png
webpack.config.js 🍺 updated final dist folder with renamed library name Oct 8, 2018
yarn.lock

README.md

πŸ”” react-polling

Build Status

Easy to use polling service built with react that follows the render props pattern.

Note: Read more about render props here Render Props Pattern

🚚 Installation

yarn add react-polling

or

npm i react-polling --save

⚑️ Usage

import React from 'react';

<ReactPolling
  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
  render={({ startPolling, stopPolling, isPolling }) => {
    if(isPolling) {
      return (
        <div> Hello I am polling</div>
      );
    } else {
      return (
        <div> Hello I stopped polling</div>
      );
    }
  }}
/>

πŸ“’ Api

πŸ”” react-polling

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
render function - Render function to render the ui
children function - React children function based on child props pattern

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.
}

πŸ“¦ Size

πŸ‘ 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