Socket
Book a DemoInstallSign in
Socket

@brianmcallister/use-api-poller-worker

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@brianmcallister/use-api-poller-worker

React hook for @brianmcallister/api-poller-worker

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

@brianmcallister/use-api-poller-worker

codecov CircleCI npm version

React hook for @brianmcallister/api-poller-worker

use-api-poller-worker is a React hook that you can use to integrate @brianmcallister/api-poller-worker into your React app.

Table of contents

⇡ Top

Installation

npm install @brianmcallister/use-api-poller-worker
⇡ Top

Usage

This hook takes the same options as the ApiPollerWorker class from @brianmcallister/api-poller-worker, plus an extra callback argument which will be called when the underlying worker emits a message.

It's recommended to wrap your callback in React.useCallback, so that your app does not unnecessarily re-render multiple times.

const MyComponent = () => {
  const [items, setItems] = React.useState<Item>(null);
  const callback = React.useCallback<(data: Msg<Item>) => void>(data => {
    setItems(data);
  }, [setItems]);

  useApiPollerWorker<Item>({
    workerUrl: '<my api endpoint>',
  }, callback);

  return {
    <div>
      {items}
    </div>
  };
};
⇡ Top

API

Functions

useApiPollerWorker

This is the default export.

import useApiPollerWorker from '@brianmcallister/use-api-poller-worker';

function useApiPollerWorker<T extends {}>(
  options: ApiPollerWorkerOptions,
  callback: (data: Msg<T>) => void,
)

Both ApiPollerWorkerOptions and Msg<T> come from @brianmcallister/api-poller-worker. You might want to import Msg<T> for type safety. This library re-exports Msg<T> for convenience.

useApiPollerWorker also accepts a generic, T for the type of resource you're polling.

⇡ Top

Types

Msg<T>

Represents the contents of a message from the underlying polling library.

import { Msg } from '@brianmcallister/use-api-poller-worker';
interface Msg<T> {
  newItems: Records<T>;
  updatedItems: Records<T>;
  removedItems: string[];
}
⇡ Top

Keywords

react

FAQs

Package last updated on 20 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts