Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
use-scheduled
Advanced tools
React hook to handle deferring activities for a later time - once or recurring.
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:
Time | Render | useScheduled | useInterval |
---|---|---|---|
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.
npm install use-scheduled
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.
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.Key | Type | Default | Description |
---|---|---|---|
suspend | boolean | false | Will suspend the interval when set. When becoming unset it will reset the interval timer. Does not stop any tasks that have already started. |
allowConcurrent | boolean | true | Whether or not to allow multiple iterations of the interval to run at the same time. |
occurrences | number | Infinity | How many times to run the scheduled task. |
deadline | number | 1000 | If for any reason the scheduled task does not run it will wait pending at least deadline milliseconds. |
This hook returns an object with the following predefined keys.
Key | Type | Description |
---|---|---|
reset | function | Resets the interval back to the original state - including occurence count, timeouts, etc. |
scheduledCount | number | How many times this interval has been scheduled. |
successCount | number | How many times the interval has completed successfully. |
failureCount | number | How many times the interval has completed with a failure. |
lastScheduleTime | number | When the last task was successfully started. |
FAQs
React hook to handle deferring activities for a later time - once or recurring.
The npm package use-scheduled receives a total of 0 weekly downloads. As such, use-scheduled popularity was classified as not popular.
We found that use-scheduled demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.