signal-desktop/ts/hooks/useValueAtFixedRate.ts

20 lines
462 B
TypeScript
Raw Normal View History

2023-02-28 20:01:52 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useEffect, useState } from 'react';
export function useValueAtFixedRate<T>(value: T, rate: number): T {
const [currentValue, setCurrentValue] = useState(value);
useEffect(() => {
const timeout = setTimeout(() => {
setCurrentValue(value);
}, rate);
return () => {
clearTimeout(timeout);
};
}, [value, rate]);
return currentValue;
}