Skip to content

Files

Latest commit

477614f Β· Mar 3, 2020

History

History
25 lines (20 loc) Β· 695 Bytes

useSet.md

File metadata and controls

25 lines (20 loc) Β· 695 Bytes

useSet

React state hook that tracks a Set.

Usage

import {useSet} from 'react-use';

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

  return (
    <div>
      <button onClick={() => add(String(Date.now()))}>Add</button>
      <button onClick={() => reset()}>Reset</button>
      <button onClick={() => remove('hello')} disabled={!has('hello')}>
        Remove 'hello'
      </button>
      <button onClick={() => toggle('hello')}>Toggle hello</button>
      <pre>{JSON.stringify(Array.from(set), null, 2)}</pre>
    </div>
  );
};