useCurrentTime
Overview
The useCurrentTime hook is a custom React hook that provides a way to execute a callback function at a specified frequency with the current time as input. The hook leverages the @js-joda/core library for working with time in a precise and immutable manner.
Importing
To use the useCurrentTime hook, you need to import it:
import { useCurrentTime } from './jmaelements/hooks/useCurrentTime'
Hook Signature
useCurrentTime(
onTick: (_: jsjoda.Instant) => void,
frequency?: number
): void
Parameters
- onTick: A callback function that receives the current time as a
jsjoda.Instant. This function is invoked at the specified interval. - frequency: An optional number representing the interval (in milliseconds) at which the
onTickcallback is executed. Defaults to100msif not provided.
Returns
This hook does not return any values; it only invokes the provided onTick callback at the specified interval.
How It Works
- Uses
React.useEffectto set up asetIntervalthat invokes theonTickcallback with the current time (jsjoda.Instant.now()). - The interval runs at the specified
frequency. - Cleans up the interval using
clearIntervalwhen the component unmounts or dependencies change.
Example Usage
Basic Example
import React from 'react';
import { useCurrentTime } from './jmaelements/hooks/useCurrentTime';
import * as jsjoda from '@js-joda/core';
const TimeDisplay: React.FC = () => {
const [currentTime, setCurrentTime] = React.useState<string>('');
// Using the custom hook to update the current time
useCurrentTime((instant) => {
setCurrentTime(instant.toString());
}, 1000); // Updates every second
return (
<div>
<h1>Current Time</h1>
<p>{currentTime}</p>
</div>
);
};
export default TimeDisplay;
Explanation
- Callback Execution: The
onTickfunction is called every second with the current time. - State Update: The
setCurrentTimefunction updates the displayed time in the component.
Example with Custom Frequency
import React from 'react';
import { useCurrentTime } from './jmaelements/hooks/useCurrentTime';
import * as jsjoda from '@js-joda/core';
const FastClock: React.FC = () => {
const [currentTime, setCurrentTime] = React.useState<string>('');
// Using the custom hook with a faster frequency
useCurrentTime((instant) => {
setCurrentTime(instant.toString());
}, 100); // Updates every 100ms
return (
<div>
<h1>Fast Clock</h1>
<p>{currentTime}</p>
</div>
);
};
export default FastClock;
Explanation
- Custom Frequency: The clock updates 10 times per second due to the
frequencyset to100ms.
Best Practices
- Optimize Callback Logic: Avoid expensive computations in the
onTickcallback to prevent performance bottlenecks. - Cleanup: Ensure proper cleanup of intervals when the component unmounts.
- Frequency: Use an appropriate
frequencyvalue based on the precision needed for your use case.
Notes
- This hook uses the
@js-joda/corelibrary for time handling, ensuring high precision and immutability. - The hook is designed to be lightweight and simple, with flexibility for different update intervals.
Contributing
If you encounter issues or have suggestions for improving the useCurrentTime hook, feel free to contribute or report an issue on the repository.