JSPM

preact-fetching

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 845
  • Score
    100M100P100Q104009F
  • License MIT

Preact hooks for asynchronous data fetching

Package Exports

  • preact-fetching

Readme

Preact Fetching

Preact hooks for asynchronous data fetching.

Note: This project is currently in an early prerelease status, and breaking changes to the API may still occur until a 1.0 release.

Features

  • Promise-based data fetching. Unopinionated about the source of your data.
  • Small bundle size. Less than 0.5kb minified and gzipped.
  • TypeScript support. Respects the resolved type of your fetcher implementation.
  • Configurable global cache. All components share the same data by distinct keys.

Example

import { useQuery } from 'preact-fetching';

function GitHubStars({ owner, repo }) {
    const { isLoading, isError, error, data } = useQuery(#[owner, repo], async () => {
        const response = await fetch(`https://api.github.com/repos/${owner}/${repo}`);
        const data = await response.json();
        return data.stargazers_count;
    });

    if (isError) {
        return `An error occurred! ${error.message}`;
    }

    if (isLoading) {
        return 'Loading...';
    }

    return `Count: ${data}`;
}

useQuery accepts two arguments:

  • A key to uniquely identify the data being fetched.
  • A function which fetches the data and returns either a promise or the resulting data.

The fetching function will be called any time a component is mounted and there is not already a fetch in progress for that key. Requests are deduplicated if many components are mounted at the same time which reference the same data. Data is refetched if when future components are mounted using that same key, during which time the stale data will still be available.

The default cache behavior uses a simple Map object to reference cached values by key. For array or object values, you can consider one of the following:

Installation

Install using NPM or Yarn:

npm install preact-fetching
yarn add preact-fetching

API

useQuery

function useQuery<Data>(
    key: any,
    fetcher: () => Data | Promise<Data>
): LoadingResult<Data> | SuccessResult<Data> | ErrorResult<Data> | IdleResult<Data>;

Triggers a new fetch request as appropriate and returns a result of the current status.

CacheContext

let CacheContext: import('preact').Context<MapLike<
    any,
    CacheLoadingEntry<any> | CacheSuccessEntry<any> | CacheErrorEntry<any> | CacheIdleEntry<any>
>>;

Context serving as cache state container. For most usage, you shouldn't need to interface with the context object, but in advanced use-cases you can use this to substitute or scope caches.

Project Goals and Non-Goals

Goals:

  • Lack of configurability as a feature, preferring smart defaults and a minimal-but-flexible API over a multitude of settings.
  • Efficiency in bundle size, performance, and cache invalidations. Micro-optimizations and code golf are welcome.

Non-goals:

  • Feature parity with similar libraries (swr, react-query, etc.).

License

Copyright 2020 Andrew Duthie

Released under the MIT License. See LICENSE.md.