{"id":17581844,"url":"https://github.com/vivek12345/react-polling-hook","last_synced_at":"2025-04-28T16:10:05.844Z","repository":{"id":43595501,"uuid":"156772933","full_name":"vivek12345/react-polling-hook","owner":"vivek12345","description":"Polling an api made easy with react-hooks  🔔","archived":false,"fork":false,"pushed_at":"2022-12-08T17:27:23.000Z","size":807,"stargazers_count":33,"open_issues_count":20,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-28T16:10:00.391Z","etag":null,"topics":["hooks","polling-service","react","react-hooks","react-polling"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vivek12345.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-08T21:44:21.000Z","updated_at":"2024-09-13T23:55:10.000Z","dependencies_parsed_at":"2023-01-25T19:01:49.968Z","dependency_job_id":null,"html_url":"https://github.com/vivek12345/react-polling-hook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vivek12345%2Freact-polling-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vivek12345%2Freact-polling-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vivek12345%2Freact-polling-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vivek12345%2Freact-polling-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vivek12345","download_url":"https://codeload.github.com/vivek12345/react-polling-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251342724,"owners_count":21574245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","polling-service","react","react-hooks","react-polling"],"created_at":"2024-10-22T01:44:32.931Z","updated_at":"2025-04-28T16:10:05.825Z","avatar_url":"https://github.com/vivek12345.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🔔 react-polling-hook\n[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)\n\n\n[![Build Status](https://travis-ci.com/vivek12345/react-polling-hook.svg)](https://travis-ci.com/vivek12345/react-polling-hook)\n\n\u003e ⚠️ Not yet published on npm and is not being currently developed. If you still need it, please fork the repo.\n\nEasy to use polling service built with react that uses the new React Hooks(Note: This is an alpha release)\n\n**Note: Read more about react hooks here  [React hooks](https://reactjs.org/docs/hooks-intro.html)**\n\n### 🚚 Installation\n\n```\nyarn add react-polling-hook\n```\n\nor\n\n```\nnpm i react-polling-hook --save\n```\n\n### ⚡️ Usage\n\n```javascript\nimport React, { Fragment } from 'react';\nimport { usePolling } from 'react-polling-hook';\n\nconst App = () =\u003e {\n  const [isPolling, startPolling, stopPolling] = usePolling({\n    url: 'url to poll',\n    interval: 3000, // in milliseconds(ms)\n    retryCount: 3, // this is optional\n    onSuccess: () =\u003e console.log('handle success'),\n    onFailure: () =\u003e console.log('handle failure'), // this is optional\n    method: 'GET',\n    headers: \"headers object\", // this is optional\n    body: JSON.stringify(data) // data to send in a post call. Should be stringified always\n  });\n\n  return (\n    \u003cdiv className=\"App\"\u003e\n      {isPolling ? (\n        \u003cFragment\u003e\n          \u003cdiv\u003e Hello I am polling\u003c/div\u003e\n          \u003cbutton onClick={stopPolling}\u003eStop Polling\u003c/button\u003e\n        \u003c/Fragment\u003e\n      ) : (\n        \u003cFragment\u003e\n          \u003cdiv\u003e Hello I have stopped polling\u003c/div\u003e\n          \u003cbutton onClick={startPolling}\u003eStart Polling\u003c/button\u003e\n        \u003c/Fragment\u003e\n      )}\n    \u003c/div\u003e\n  );\n}\n```\n\n## 📒 Api\n\n### 🔔 react-polling-hook\n\n| Props                   | Type                   | Default   | Description                                                                                         |\n|-------------------------|------------------------|-----------|-----------------------------------------------------------------------------------------------------|\n| url                     | string                 | null      | url/api to poll                                                                                     |\n| interval                | number                 | 3000      | Interval of polling                                                                                 |\n| retryCount              | number                 | 0         | Number of times to retry when an api polling call fails                                             |\n| onSuccess               | function               | -         | Callback function on successful polling. This should return true to continue polling                |\n| onFailure               | function               | () =\u003e {}  | Callback function on failed polling or api failure                                                  |\n| method                  | string                 | GET       | HTTP Method of the api to call                                                                      |\n| headers                 | object                 | -         | Any specific http headers that need to be sent with the request                                     |\n| body                    | object                 | -         | The data that need to be sent in a post/put call\n\n#### onSuccess (required)\n\nThis function will be called every time the polling service gets a successful response.\nYou should return true to continue polling and false to stop polling. It has the following signature:\n\n```javascript\nfunction onSuccess(response) {\n  // You can do anything with this response, may be add to an array of some state of your react component\n  // return true to continue polling\n  // return false to stop polling\n}\n```\n\n#### onFailure (not compulsory field)\n\nThis 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.\nYou can do some cleaning up of your variables or reseting the state here.\n\n```javascript\nfunction onFailure(error) {\n  // You can log this error to some logging service\n  // clean up some state and variables.\n}\n```\n\n## 👍 Contribute\n\nShow your ❤️ and support by giving a ⭐. Any suggestions and pull request are welcome !\n\n### 📝 License\n\nMIT © [viveknayyar](https://github.com/vivek12345)\n\n## 👷 TODO\n\n- [x] Complete README\n- [ ] Add Examples and Demo\n- [ ] Test Suite\n\n## Contributors\n\nThanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore --\u003e\n| [\u003cimg src=\"https://avatars3.githubusercontent.com/u/4931048?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eVivek Nayyar\u003c/b\u003e\u003c/sub\u003e](https://www.viveknayyar.in/)\u003cbr /\u003e[📖](https://github.com/vivek12345/react-polling-hook/commits?author=vivek12345 \"Documentation\") [💻](https://github.com/vivek12345/react-polling-hook/commits?author=vivek12345 \"Code\") [🎨](#design-vivek12345 \"Design\") [💡](#example-vivek12345 \"Examples\") |\n| :---: |\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvivek12345%2Freact-polling-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvivek12345%2Freact-polling-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvivek12345%2Freact-polling-hook/lists"}