Skip to content

Commit d74e938

Browse files
committed
新增 useSet 的文档和示例
1 parent 15a2913 commit d74e938

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
- [`useTimeout`](./docs/useTimeout.md) — re-renders component after a timeout.
9090
- [`useTimeoutFn`](./docs/useTimeoutFn.md) — calls given function after a timeout. [![][img-demo]](https://niqingyang.github.io/vue-next-use/?path=/story/animation-usetimeoutfn--demo)
9191
- [`useTween`](./docs/useTween.md) — re-renders component, while tweening a number from 0 to 1. [![][img-demo]](https://niqingyang.github.io/vue-next-use/?path=/story/animation-usetween--easing-demo)
92-
- [`useUpdate`](./docs/useUpdate.md) — returns a callback, which re-renders component when called.
92+
<!-- - [`useUpdate`](./docs/useUpdate.md) &mdash; returns a callback, which re-renders component when called. -->
9393
<br/>
9494
<br/>
9595
- [**Side-effects**](./docs/Side-effects.md)
@@ -109,6 +109,7 @@
109109
- [`usePermission`](./docs/usePermission.md) &mdash; query permission status for browser APIs.
110110
<br/>
111111
<br/>
112+
<!--
112113
- [**Lifecycles**](./docs/Lifecycles.md)
113114
- [`useEffectOnce`](./docs/useEffectOnce.md) &mdash; a modified [`useEffect`](https://reactjs.org/docs/hooks-reference.html#useeffect) hook that only runs once.
114115
- [`useEvent`](./docs/useEvent.md) &mdash; subscribe to events.
@@ -123,6 +124,7 @@
123124
- [`useDeepCompareEffect`](./docs/useDeepCompareEffect.md), [`useShallowCompareEffect`](./docs/useShallowCompareEffect.md), and [`useCustomCompareEffect`](./docs/useCustomCompareEffect.md) &mdash; run an `effect` depending on deep comparison of its dependencies
124125
<br/>
125126
<br/>
127+
-->
126128
- [**State**](./docs/State.md)
127129
- [`createMemo`](./docs/createMemo.md) &mdash; factory of memoized hooks.
128130
- [`createReducer`](./docs/createReducer.md) &mdash; factory of reducer hooks with custom middleware.

docs/useSet.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# `useSet`
2+
3+
Vue state hook that tracks a [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
4+
5+
## Usage
6+
7+
```jsx
8+
import {useSet} from 'vue-next-use';
9+
10+
const Demo = {
11+
setup(){
12+
const [set, { add, has, remove, toggle, reset }] = useSet(new Set(['hello']));
13+
14+
return () => (
15+
<div>
16+
<button onClick={() => add(String(Date.now()))}>Add</button>
17+
<button onClick={() => reset()}>Reset</button>
18+
<button onClick={() => remove('hello')} disabled={!has('hello')}>
19+
Remove 'hello'
20+
</button>
21+
<button onClick={() => toggle('hello')}>Toggle hello</button>
22+
<pre>{JSON.stringify(Array.from(set), null, 2)}</pre>
23+
</div>
24+
);
25+
}
26+
};
27+
```

stories/useSet.stories.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {useSet} from "../src/index";
2+
import {ShowDemo, ShowDocs} from './util/index';
3+
4+
export default {
5+
title: 'State/useSet',
6+
argTypes: {},
7+
};
8+
9+
export const Docs = ShowDocs(require('../docs/useSet.md'));
10+
11+
export const Demo = ShowDemo({
12+
setup() {
13+
const [set, {add, has, remove, reset, toggle}] = useSet(new Set(['hello']));
14+
15+
return () => (
16+
<div>
17+
<button onClick={() => add(String(Date.now()))}>Add</button>
18+
<button onClick={() => reset()}>Reset</button>
19+
<button onClick={() => remove('hello')} disabled={!has('hello')}>
20+
Remove 'hello'
21+
</button>
22+
<button onClick={() => toggle('hello')}>Toggle 'hello'</button>
23+
<pre>{JSON.stringify(Array.from(set.value), null, 2)}</pre>
24+
</div>
25+
);
26+
},
27+
});
28+
29+
30+
31+

0 commit comments

Comments
 (0)