@knitui/hooks
useQueue
A bounded queue that keeps limit items visible and holds the overflow — port of Mantine's useQueue. Pure React state, so identical on web and native.
Import
import { useQueue } from "@knitui/hooks";Signature
export function useQueue<T>({ initialValues = [], limit }: UseQueueOptions<T>): UseQueueReturn<T>Exported types: UseQueueOptions, UseQueueReturn
useQueue splits one list in two: state holds at most limit items, and
everything past that lands in queue. add(...items) appends to the combined
list and re-splits it, so a burst never loses items — they wait for a slot.
update(fn) hands you the whole list, visible plus queued, and re-splits
whatever you return, which is how an item leaves the visible window and the
oldest queued one is promoted into it. cleanQueue() drops the overflow only.
The failure mode it prevents is the one you get from slice-ing a plain array in
state: items past the cap are thrown away, so five notifications against a cap of
three lose two of them permanently.
When to use it
- A notification or toast stack that shows a fixed number at a time and plays the rest in order.
- Rate-limited rendering of an incoming stream — messages, events, log lines.
- Any "show N, hold the rest" UI where the held items must survive until a slot frees up.
Notes
limitis read whenaddorupdateruns, not when it changes. Raising the limit does not promote queued items until the next mutation.- There is no
shiftorpop; removal goes throughupdate, which is also what triggers promotion fromqueueintostate. - Pure React state with no timers or platform APIs, so behaviour is identical on web and native.