useSortBy
Purpose
The useSortBy hook exposes methods and data for sorting results, internally it is used by the SortBy component.
Usage
import React from "react";
import { useSortBy } from "@usereactify/search";
export const ExampleHookUseSortBy: React.FC = () => {
const sortByHook = useSortBy();
const handleSelectChange = React.useCallback((event: React.ChangeEvent<HTMLSelectElement>) => {
sortByHook.setSortOption(event.target.value);
}, [sortByHook.setSortOption]);
return (
<select
value={sortByHook.sortOption?.handle}
onChange={handleSelectChange}
>
{sortByHook.sortOptions.map((sortOption) => (
<option
key={sortOption.handle}
value={sortOption.handle}
>
{sortOption.name}
</option>
))}
</select>
);
};
Signature
Last updated
Was this helpful?