Socket
Socket
Sign inDemoInstall

use-scheduled

Package Overview
Dependencies
6
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    use-scheduled

React hook to handle deferring activities for a later time - once or recurring.


Version published
Weekly downloads
1
Maintainers
1
Install size
26.8 kB
Created
Weekly downloads
 

Readme

Source

Custom React Hook useScheduled :calendar:

React hook to handle deferring activities for a later time - once or recurring. This may be used as an analogue of setInterval or setTimeout in the React Programming Model.

While most implementations using the method described by Dan Abramov work well enough for many use cases, there are edge cases where they may fire off schedule - or in the worst cases, won't fire at all. This is because it uses setInterval without taking into account our current state. When a component re-renders in a way that updates the delay passed to useInterval the hook implementation will clearInterval and then start a new interval with setInterval.

For example, if you were to use useScheduled(fireworksCallback, 400) and useInterval(fireworksCallback, 400) in your React Component that often has the props change, you'd see the following behavior:

TimeRenderuseScheduleduseInterval
0ms:gift::calendar::calendar:
100ms:watch::watch:
200ms:watch::watch:
300ms:gift::watch::calendar:
400ms:gift::fireworks::calendar:
500ms:watch::watch:
600ms:watch::watch:
700ms:watch::watch:
800ms:fireworks::fireworks:

useScheduled does not suffer from this fate. It keeps a record of the last time it had scheduled an interval and on any updated inputs it will reschedule from that last time instead of the current time to keep the schedule stable.

Other interval implementations are fire and forget and only execute tasks asynchronously. This is good in some cases but may limit the usefulness in others.

Installation

npm install use-scheduled

Usage

import React, { useCallback } from 'react';
import useScheduled from 'use-scheduled';

export const App = () => {
  const [callCount, setCallCount] = useState(0);

  const { lastScheduleTime } = useScheduled(
    useCallback(
      () => setCallCount(n => n + 1),
      [setCallCount]
    ),
    5000
  );

  return (
    <div>
      Our Interval has been called {callCount} times.
      It was last called at {lastScheduleTime}.
    </div>
  );
};

Don't forget to use the useCallback hook to correctly refresh the interval callback value only when dependencies have changed.

API Reference

const returnObject = useScheduled(callback, delay, options);
  • callback is a function to be called on the schedule.
  • delay is the time in milliseconds to wait between calls to the scheduled callback.
  • options is an object to allow more detailed configuration of the useScheduled hook.

Options Object

KeyTypeDefaultDescription
suspendbooleanfalseWill suspend the interval when set. When becoming unset it will reset the interval timer. Does not stop any tasks that have already started.
allowConcurrentbooleantrueWhether or not to allow multiple iterations of the interval to run at the same time.
occurrencesnumberInfinityHow many times to run the scheduled task.
deadlinenumber1000If for any reason the scheduled task does not run it will wait pending at least deadline milliseconds.

Return Object

This hook returns an object with the following predefined keys.

KeyTypeDescription
resetfunctionResets the interval back to the original state - including occurence count, timeouts, etc.
scheduledCountnumberHow many times this interval has been scheduled.
successCountnumberHow many times the interval has completed successfully.
failureCountnumberHow many times the interval has completed with a failure.
lastScheduleTimenumberWhen the last task was successfully started.

License

MIT Licensed

Keywords

FAQs

Last updated on 24 May 2020

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc