signal-desktop/ts/hooks/useValueAtFixedRate.ts
2023-02-28 15:01:52 -05:00

19 lines
462 B
TypeScript

// 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;
}