Skip to content

Commit 1912af6

Browse files
committed
docs: restore example[skip ci]
1 parent 6f986b2 commit 1912af6

File tree

1 file changed

+8
-72
lines changed

1 file changed

+8
-72
lines changed

example/App.tsx

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,23 @@
1-
import { defineComponent, reactive, watchEffect } from 'vue';
2-
import { useLoadMore } from 'vue-request';
3-
import Mock from 'mockjs';
1+
import { defineComponent } from 'vue';
2+
import { useRequest } from 'vue-request';
43

5-
function api(current: number, total = 10) {
6-
let list: string[] = [];
7-
if (current <= total) {
8-
list = Mock.mock({
9-
'list|5': ['@name'],
10-
}).list;
11-
} else {
12-
list = [];
13-
}
14-
return {
15-
current,
16-
total,
17-
list,
18-
};
19-
}
20-
type APIReturnType = ReturnType<typeof api>;
21-
type rawDataType = {
22-
data: APIReturnType;
23-
dataList: APIReturnType['list'];
24-
};
25-
26-
function testService(rawData: rawDataType) {
27-
const current = (rawData?.data?.current || 0) + 1;
28-
console.log(`${current}`, rawData);
29-
30-
return new Promise<APIReturnType>(resolve => {
4+
function testService() {
5+
return new Promise<string>(resolve => {
316
setTimeout(() => {
32-
resolve(api(current));
7+
resolve('success');
338
}, 1000);
349
});
3510
}
3611

3712
export default defineComponent({
3813
name: 'App',
3914
setup() {
40-
const { loadMore, loadingMore, dataList, noMore, reload } = useLoadMore<
41-
APIReturnType,
42-
any,
43-
APIReturnType['list']
44-
>(testService, {
45-
isNoMore: d => {
46-
return d?.current >= d?.total;
47-
},
48-
});
49-
50-
const testReactive = reactive({ first: 'init' });
51-
52-
watchEffect(() => {
53-
console.log(testReactive);
54-
});
55-
15+
const { run, data, loading } = useRequest(testService);
5616
return () => (
5717
<div>
58-
<button
59-
disabled={noMore.value}
60-
onClick={() => {
61-
loadMore();
62-
Object.keys(testReactive).forEach(key => {
63-
testReactive[key] = new Date();
64-
});
65-
}}
66-
>
67-
run
68-
</button>
69-
70-
<button
71-
onClick={() => {
72-
reload();
73-
}}
74-
>
75-
reload
76-
</button>
18+
<button onClick={() => run()}>run</button>
7719
<br />
78-
{loadingMore.value
79-
? 'loading...'
80-
: dataList.value?.map((i, idx) => (
81-
<div key={idx}>
82-
<h4 style="display: inline-block">{idx}</h4>: {i}
83-
</div>
84-
))}
20+
{loading.value ? 'loading...' : data.value}
8521
</div>
8622
);
8723
},

0 commit comments

Comments
 (0)