# @grexie/refetch

> A simple refetch manager for React.

Latest version **0.1.3** (published 2022-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @grexie/refetch
pnpm add @grexie/refetch
yarn add @grexie/refetch
bun add @grexie/refetch
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2022-03-11 |
| First published | 2022-03-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | tbehrsin |

## Links

- npm: https://www.npmjs.com/package/@grexie/refetch
- Repository: https://github.com/grexie/refetch
- Homepage: https://github.com/grexie/refetch#readme
- Issues: https://github.com/grexie/refetch/issues
- npm.io page: https://npm.io/package/@grexie/refetch

## Dependencies (2)

- [events](https://npm.io/package/events.md) >=3.3.0
- [@grexie/compose](https://npm.io/package/@grexie/compose.md) >=0.1.7

## Recent versions

- 0.1.3 (latest) — 2022-03-11
- 0.1.2 — 2022-03-10
- 0.1.1 — 2022-03-10
- 0.1.0 — 2022-03-10

## README

# Grexie Refetch

A simple refetch manager for React.

Grexie Refetch allows you to install a refetch manager with an interval wrapped around your root app component. Child components can subscribe to the refetch manager and the subscribe handler will be called on the interval.

## Installing

```bash
yarn add @grexie/refetch
```

## Usage

Wrap your root app component with RefetchProvider, passing in an optional interval in milliseconds. If the interval is specified then `RefetchController#setInterval` will be called in a `useEffect` hook to set up refetch events on the interval.

```typescript
import { RefetchProvider, useRefetch } from '@grexie/refetch';

const WrappedApp = (
  <RefetchProvider interval={15_000}>
    <App />
  </RefetchProvider>
);

const SubComponent = () => {
  ...
  const refetchController = useRefetch();

  useEffect(() => {
    return refetchController.subscribe(() => {
      ... perform refetch ...
    });
  }, []);
  ...
};
```

A composable is provided which can be used with `@grexie/compose`:

```typescript
import { withRefetchProvider } from '@grexie/refetch';

const ComposeApp = compose(withRefetchProvider({ interval: 15_000 }), App);
```

If you'd prefer to control the interval in a child component of your App then you can do so by calling `RefetchController#setInterval`:

```typescript
const SubComponent = () => {
  ...
  const refetchController = useRefetch();

  useEffect(() => {
    return refetchController.setInterval(15_000);
  }, []);
  ...
};
```

Both `RefetchController#setInterval` and `RefetchController#subscribe` return hook cleanup handlers to return from the hook so that state updates don't happen once the component is unmounted.

`RefetchController` is an `EventEmitter`, so you can subscribe to events manually by calling `refetchController.on('refetch', handler)` and removing an installed handler by doing `refetchController.removeListener('refetch', handler)`. This is what `RefetchController#subscribe` does internally.

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