@livestd/react-ticker-hook
v1.1.0
Published
Simple ticker model for pooling requests.
Downloads
9
Maintainers
Readme
Simple ticker model for pooling requests and other timing functions. Each component can subscribe for some ticker event, that defined previously. If the ticker event have not listeners, he does not work.
Installation
npm install --save @livestd/react-ticker-hook
Usage
import Tickers, { useTicker, SubscriberConfig, TickerCallback } from '@livestd/react-ticker-hook';
const IntervalId = 'mainEvent';
const IntervalEvent = () => {
return "value"
}
const SubscriberId = "id"
// Same events must not be duplicated
_ticker.add({ id: IntervalId, callback: IntervalEvent });
const Component = () => {
// Let activate ticker only on top of the page
const [ontop, setOntop] = useState(true);
const [counter, setCounter] = useState(0);
const callback = (value) => {
setCounter(counter + 1);
};
// Call main event each 5 seconds
useTicker(
{
tickerId: IntervalId,
// Id is need if you want to make uniq subscriber
id: SubscriberId,
active: ontop,
// Ticker calls callback by the least interval of subscribers
interval: 5000,
// Subscriber callback is need if you wait some response from the ticker event
callback: callback
},
// Update subscriber only when "ontop" is updated
[ontop]
);
return (<>content</>);
}