forked from alibaba/hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(useRequest): add
refreshDepsAction
options and update demo (al…
…ibaba#2334) * docs: update refresh demo * chore: fix typo of filename * docs: update ready doc * chore: restore filename for better diff
- Loading branch information
Showing
6 changed files
with
72 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 15 additions & 26 deletions
41
packages/hooks/src/useRequest/doc/refreshDeps/demo/refreshDeps.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,34 @@ | ||
import React, { useState } from 'react'; | ||
import Mock from 'mockjs'; | ||
import { useRequest } from 'ahooks'; | ||
|
||
const userSchool = (id: string) => { | ||
switch (id) { | ||
case '1': | ||
return 'Tsinghua University'; | ||
case '2': | ||
return 'Beijing University'; | ||
case '3': | ||
return 'Zhejiang University'; | ||
default: | ||
return ''; | ||
} | ||
}; | ||
function getUsername(id: number): Promise<string> { | ||
console.log('use-request-refresh-deps-id', id); | ||
|
||
async function getUserSchool(userId: string): Promise<string> { | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(userSchool(userId)); | ||
resolve(Mock.mock('@name')); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
export default () => { | ||
const [userId, setUserId] = useState('1'); | ||
const { data, loading } = useRequest(() => getUserSchool(userId), { | ||
const [userId, setUserId] = useState<number>(); | ||
const { data, loading, run } = useRequest((id: number) => getUsername(id), { | ||
refreshDeps: [userId], | ||
}); | ||
|
||
if (loading) { | ||
return <div>loading...</div>; | ||
} | ||
|
||
return ( | ||
<div> | ||
<select | ||
onChange={(e) => setUserId(e.target.value)} | ||
value={userId} | ||
style={{ marginBottom: 16, width: 120 }} | ||
> | ||
<option value="1">user 1</option> | ||
<option value="2">user 2</option> | ||
<option value="3">user 3</option> | ||
</select> | ||
<p>School: {loading ? 'Loading' : data}</p> | ||
<p>Username: {data}</p> | ||
<button style={{ marginRight: '8px' }} onClick={() => setUserId(Math.random())}> | ||
Use previous id to refresh | ||
</button> | ||
<button onClick={() => run(Math.random())}>Use latest id to refresh</button> | ||
</div> | ||
); | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/hooks/src/useRequest/doc/refreshDeps/demo/refreshDepsAction.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { useState } from 'react'; | ||
import Mock from 'mockjs'; | ||
import { useRequest } from 'ahooks'; | ||
|
||
function getUsername(id: number): Promise<string> { | ||
console.log('use-request-refresh-deps-id', id); | ||
|
||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(Mock.mock('@name')); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
export default () => { | ||
const [userId, setUserId] = useState<number>(); | ||
const { data, loading, run } = useRequest((id: number) => getUsername(id), { | ||
refreshDeps: [userId], | ||
refreshDepsAction: () => run(userId), | ||
}); | ||
|
||
if (loading) { | ||
return <div>loading...</div>; | ||
} | ||
|
||
return ( | ||
<div> | ||
<p>Username: {data}</p> | ||
<button style={{ marginRight: '8px' }} onClick={() => setUserId(Math.random())}> | ||
Use latest id to refresh | ||
</button> | ||
<button onClick={() => run(Math.random())}>Use latest id to refresh</button> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters