@shopify/react-idle
Utilities for working with idle callbacks in React
Last updated a month ago by shopify-dep .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @shopify/react-idle 
SYNC missed versions from official npm registry.

@shopify/react-idle

Build Status License: MIT npm version npm bundle size (minified + gzip)

Utilities for working with idle callbacks in React.

Installation

$ yarn add @shopify/react-idle

Usage

This library provides a hook (useIdleCallback) and a component (OnIdle) for registering a callback to run in the next idle callback.

Note: this callback is not called with any arguments, unlike direct usage of requestIdleCallback. This makes it more suited for use with discrete operations, rather than ones that will need to schedule themselves for subsequent idle callbacks if the work has not been completed.

import {useCallback} from 'react';
import {useIdleCallback, OnIdle} from '@shopify/react-idle';

function MyComponent() {
  const callback = useCallback(() => {
    console.log('Hello from an idle callback!');
  }, []);

  useIdleCallback(callback);

  // or

  return <OnIdle perform={callback} />;
}

If the callback ever changes, or the component unmounts, the original callback will not be run.

UnsupportedBehavior

Because not every browser supports idle callbacks, this library allows you to specify the behavior of perform when requestIdleCallback is not present. There are currently two options (each of which can be passed as an unsupportedBehavior option for the hook, or an unsupportedBehavior prop of the component):

  • UnsupportedBehavior.AnimationFrame (default): run the callback in the next animation frame using requestAnimationFrame.
  • UnsupportedBehavior.Immediate: run the callback immediately on mount.
import {useIdleCallback, UnsupportedBehavior} from '@shopify/react-idle';

function MyComponent() {
  useIdleCallback(doSomethingThatCanBeDeferred, {
    unsupportedBehavior: UnsupportedBehavior.Immediate,
  });

  return null;
}

Additional types

Because the typings for requestIdleCallback are not yet provided by the TypeScript standard library, this module also exports a number of types that are needed to interact with these globals.

Current Tags

  • 1.0.19                                ...           latest (a month ago)
  • 0.0.8-sknext-beta.8                                ...           next (12 hours ago)

23 Versions

  • 0.0.8-sknext-beta.8                                ...           12 hours ago
  • 0.0.2-sknext-beta.2                                ...           6 days ago
  • 0.0.1-sknext-beta.1                                ...           6 days ago
  • 1.0.19                                ...           a month ago
  • 1.0.18                                ...           2 months ago
  • 1.0.17                                ...           4 months ago
  • 1.0.16                                ...           6 months ago
  • 1.0.15                                ...           7 months ago
  • 1.0.14                                ...           8 months ago
  • 1.0.13                                ...           9 months ago
  • 1.0.12                                ...           9 months ago
  • 1.0.11                                ...           9 months ago
  • 1.0.10                                ...           10 months ago
  • 1.0.9                                ...           10 months ago
  • 1.0.8                                ...           10 months ago
  • 1.0.7                                ...           10 months ago
  • 1.0.6                                ...           a year ago
  • 1.0.5                                ...           a year ago
  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
Downloads
Today 13
This Week 13
This Month 22
Last Day 0
Last Week 35
Last Month 28
Dependencies (2)
Dev Dependencies (0)
None

Copyright 2014 - 2016 © taobao.org |