useSet

Credit: Re-export from react-use

React state hook that tracks a Set.

Usage

import { useSet } from "react-use";

const Demo = () => {
  const [set, { add, has, remove, toggle, reset, clear }] = useSet(
    new Set(["hello"])
  );

  return (
    <view>
      <view bindtap={() => add(String(Date.now()))}>Add</view>
      <view bindtap={() => reset()}>Reset</button>
      <view bindtap={() => clear()}>Clear</button>
      <view bindtap={() => remove("hello")}>
        Remove 'hello'
      </view>
      <view bindtap={() => toggle("hello")}>Toggle hello</view>
      <view>{JSON.stringify(Array.from(set), null, 2)}</view>
    </view>
  );
};

Type Declarations

interface StableActions<K> {
  add: (key: K) => void;
  remove: (key: K) => void;
  toggle: (key: K) => void;
  reset: () => void;
  clear: () => void;
}
interface Actions<K> extends StableActions<K> {
  has: (key: K) => boolean;
}
declare const useSet: <K>(initialSet?: Set<K>) => [Set<K>, Actions<K>];