useSet
function useSet<T>(values?: Iterable<T>): readonly [ReadonlySet<T>, SetActions<T>];
interface SetActions<T> {
add: (value: T) => void;
remove: (value: T) => void;
toggle: (value: T) => void;
clear: () => void;
reset: () => void;
}
A Set held in state, starting with values. It is never changed in place: each change gives
a new set, so it works as an effect dependency or a memoised prop. An action that changes
nothing, such as adding a value that is already there or removing a missing one, keeps the same
set and doesn't re-render. reset goes back to the values from the first render. The actions
object and its functions never change.
Live demo
Toppings: Cheese
Example
import { useSet } from "react-honey-hooks";
function Toppings({ toppings }: { toppings: string[] }) {
const [chosen, { toggle }] = useSet<string>();
return toppings.map((topping) => (
<label key={topping}>
<input
type="checkbox"
checked={chosen.has(topping)}
onChange={() => {
toggle(topping);
}}
/>
{topping}
</label>
));
}