# @eballoi/react-use-polling

> ☕ A hassle-free react hook for periodic data polling 🔄 with error management

Latest version **1.1.0** (published 2023-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @eballoi/react-use-polling
pnpm add @eballoi/react-use-polling
yarn add @eballoi/react-use-polling
bun add @eballoi/react-use-polling
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-10-24 |
| First published | 2023-10-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 90.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | eballoi |
| Maintainers | eballoi |
| Keywords | react, reactjs, hook, hooks, react-hooks, polling, api, REST |

## Links

- npm: https://www.npmjs.com/package/@eballoi/react-use-polling
- Repository: https://github.com/eballoi/react-use-polling
- Homepage: https://github.com/eballoi/react-use-polling#readme
- Issues: https://github.com/eballoi/react-use-polling/issues
- npm.io page: https://npm.io/package/@eballoi/react-use-polling

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^18.2.0
- [@types/react](https://npm.io/package/@types/react.md) ^18.2.30

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2023-10-24
- 1.0.0 — 2023-10-20

## README

# React Use Polling

`react-use-polling` is a custom React hook that simplifies periodic polling with error handling in your React applications. It allows you to easily fetch and update data at regular intervals while handling errors gracefully.

## Installation

You can install the `react-use-polling` package from npm using the following command:

```bash
npm install @eballoi/react-use-polling
```

## Usage

To use the `usePolling` hook in your React components, follow these steps:

1.  Import the hook:

```JSX
import usePolling from '@eballoi/react-use-polling';
```

2.  Create a polling callback function that fetches your data. For example:

```JSX
const pollingCallback = async () => {
  // Your data fetching logic here
  const response = await fetchDataFromApi();
  return response.data;
};
```

3.  Set up polling options:

```JSX
const pollingOptions = {
  interval: 5000, // Polling interval in milliseconds (e.g., every 5 seconds)
  onError: (error) => {
    // Handle errors here (optional)
    console.error('Polling error:', error);
  },
};
```

4.  Use the `usePolling` hook within your component:

```JSX
const YourComponent = () => {
  const { data, loading, error } = usePolling(pollingCallback, pollingOptions);

  return (
    <div>
      {loading && <p>Loading...</p>}
      {error && <p>Error: {error.message}</p>}
      {data && <p>Data: {data}</p>}
    </div>
  );
};
``` 

## API

### `usePolling(callback: () => Promise<any>, options: PollingOptions): PollingResult`

-   `callback`: A function that returns a promise, which should fetch the data for polling.

-   `options` (optional): An object containing polling configuration options:

    -   `interval` (number, required): The polling interval in milliseconds.
    -   `onError` (function, optional): A callback function to handle errors when they occur during polling.

### `PollingResult`

The `usePolling` hook returns an object with the following properties:

-   `data`: The latest data fetched during polling.
-   `loading`: A boolean indicating whether polling is in progress.
-   `error`: An error object if an error occurred during polling; otherwise, it's `null`.

## License

This project is licensed under the MIT License. See the [LICENSE](https://chat.openai.com/c/LICENSE) file for details.

## Contributors

-   [eballoi](https://github.com/eballoi)

## Issues and Contributions

If you encounter any issues or have suggestions for improvements, please open an issue on the [GitHub repository](https://github.com/eballoi/react-use-polling).

We welcome contributions via pull requests. Fork the repository and create a pull request with your changes.

## Changelog

See the [CHANGELOG](https://github.com/eballoi/react-use-polling/blob/4f21f44580a819b8eac280ae9a5d0c5f7665248e/CHANGELOG.md) for information about the latest updates and changes to the package.

---
_Source: https://npm.io/package/@eballoi/react-use-polling · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
