useFrecencySorting
Hook to sort an array by its frecency and provide methods to update the frecency of its items.
Frecency is a measure that combines frequency and recency. The more often an item is visited, and the more recently an item is visited, the higher it will rank.
Signature
Arguments
data
is the array to sort
With a few options:
options.namespace
is a string that can be used to namespace the frecency data (if you have multiple arrays that you want to sort in the same extension).options.key
is a function that should return a unique string for each item of the array to sort. By default, it will useitem.id
. If the items do not have anid
field, this option is required.options.sortUnvisited
is a function to sort the items that have never been visited. By default, the order of the input will be preserved.
Return
Returns an object with the sorted array and some methods to update the frecency of the items.
data
is the sorted array. The order will be preserved for items that have never been visitedvisitItem
is a method to use when an item is visited/used. It will increase its frecency.resetRanking
is a method that can be used to reset the frecency of an item.
Example
Last updated