react-use/docs/useSet.md
Octave Raimbault 477614f9ef
feat(useSet): add toggle a method (#968)
Co-authored-by: Ward <wardoosterlijnck@gmail.com>
2020-03-03 14:00:40 +11:00

26 lines
695 B
Markdown

# `useSet`
React state hook that tracks a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
## Usage
```jsx
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>
);
};
```