docs: add more examples
This commit is contained in:
parent
1d11a3e793
commit
2b078a28be
@ -29,6 +29,7 @@
|
|||||||
"i18next": "^21.6.0",
|
"i18next": "^21.6.0",
|
||||||
"react-helmet": "^6.1.0",
|
"react-helmet": "^6.1.0",
|
||||||
"react-i18next": "^11.15.1",
|
"react-i18next": "^11.15.1",
|
||||||
|
"react-image-lightbox": "^5.1.4",
|
||||||
"use-deep-compare-effect": "^1.8.1"
|
"use-deep-compare-effect": "^1.8.1"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
|
@ -20,5 +20,5 @@ export default compose(...providers)(() => {
|
|||||||
action: 'get',
|
action: 'get',
|
||||||
params: {},
|
params: {},
|
||||||
});
|
});
|
||||||
return <div>{data?.name}</div>;
|
return <div>{data?.data?.name}</div>;
|
||||||
});
|
});
|
||||||
|
23
packages/client/src/api-client/demos/demo2.tsx
Normal file
23
packages/client/src/api-client/demos/demo2.tsx
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import MockAdapter from 'axios-mock-adapter';
|
||||||
|
import { APIClient, APIClientProvider, useRequest, compose } from '@nocobase/client';
|
||||||
|
|
||||||
|
const apiClient = new APIClient();
|
||||||
|
|
||||||
|
const mock = new MockAdapter(apiClient.axios);
|
||||||
|
|
||||||
|
mock.onGet('/users:get').reply(200, {
|
||||||
|
data: { id: 1, name: 'John Smith' },
|
||||||
|
});
|
||||||
|
|
||||||
|
const providers = [
|
||||||
|
[APIClientProvider, { apiClient }]
|
||||||
|
];
|
||||||
|
|
||||||
|
export default compose(...providers)(() => {
|
||||||
|
const { data } = useRequest({
|
||||||
|
url: 'users:get',
|
||||||
|
method: 'get',
|
||||||
|
});
|
||||||
|
return <div>{data?.data?.name}</div>;
|
||||||
|
});
|
@ -72,16 +72,34 @@ function useRequest<P>(
|
|||||||
支持 `axios.request(config)`,config 详情查看 [axios](https://github.com/axios/axios#request-config)
|
支持 `axios.request(config)`,config 详情查看 [axios](https://github.com/axios/axios#request-config)
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const { data, loading, refresh } = useRequest({ url: '/users' });
|
const { data, loading, refresh, run, params } = useRequest({ url: '/users' });
|
||||||
|
|
||||||
|
// useRequest 里传的是 AxiosRequestConfig,所以 run 里传的也是 AxiosRequestConfig
|
||||||
|
run({
|
||||||
|
params: {
|
||||||
|
pageSize: 20,
|
||||||
|
}
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
例子如下:
|
||||||
|
|
||||||
|
<code src="./demos/demo2.tsx" />
|
||||||
|
|
||||||
或者是 NocoBase 的 resource & action 请求:
|
或者是 NocoBase 的 resource & action 请求:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const { data } = useRequest({
|
const { data, run } = useRequest({
|
||||||
resource: 'users',
|
resource: 'users',
|
||||||
action: 'get',
|
action: 'list',
|
||||||
params: {},
|
params: {
|
||||||
|
pageSize: 20,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// useRequest 传的是 ResourceActionOptions,所以 run 直接传 action params 就可以了。
|
||||||
|
run({
|
||||||
|
pageSize: 50,
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -92,7 +110,9 @@ const { data } = useRequest({
|
|||||||
也可以是自定义的异步函数:
|
也可以是自定义的异步函数:
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const { data, loading, refresh } = useRequest(() => Promise.resolve({}));
|
const { data, loading, run, refresh, params } = useRequest((...params) => Promise.resolve({}));
|
||||||
|
|
||||||
|
run(...params);
|
||||||
```
|
```
|
||||||
|
|
||||||
更多用法查看 ahooks 的 [useRequest()](https://ahooks.js.org/hooks/use-request/index)
|
更多用法查看 ahooks 的 [useRequest()](https://ahooks.js.org/hooks/use-request/index)
|
||||||
|
12
yarn.lock
12
yarn.lock
@ -6272,7 +6272,7 @@ execa@^5.0.0:
|
|||||||
|
|
||||||
exenv@^1.2.0:
|
exenv@^1.2.0:
|
||||||
version "1.2.2"
|
version "1.2.2"
|
||||||
resolved "https://registry.npmmirror.com/exenv/download/exenv-1.2.2.tgz#2ae78e85d9894158670b03d47bec1f03bd91bb9d"
|
resolved "https://registry.npmjs.org/exenv/-/exenv-1.2.2.tgz#2ae78e85d9894158670b03d47bec1f03bd91bb9d"
|
||||||
integrity sha1-KueOhdmJQVhnCwPUe+wfA72Ru50=
|
integrity sha1-KueOhdmJQVhnCwPUe+wfA72Ru50=
|
||||||
|
|
||||||
exit@^0.1.2:
|
exit@^0.1.2:
|
||||||
@ -12355,8 +12355,8 @@ react-i18next@^11.15.1:
|
|||||||
|
|
||||||
react-image-lightbox@^5.1.4:
|
react-image-lightbox@^5.1.4:
|
||||||
version "5.1.4"
|
version "5.1.4"
|
||||||
resolved "https://registry.nlark.com/react-image-lightbox/download/react-image-lightbox-5.1.4.tgz#5b847dcb79e9efdf9d7cd5621a92e0f156d2cf30"
|
resolved "https://registry.npmjs.org/react-image-lightbox/-/react-image-lightbox-5.1.4.tgz#5b847dcb79e9efdf9d7cd5621a92e0f156d2cf30"
|
||||||
integrity sha1-W4R9y3np79+dfNViGpLg8VbSzzA=
|
integrity sha512-kTiAODz091bgT7SlWNHab0LSMZAPJtlNWDGKv7pLlLY1krmf7FuG1zxE0wyPpeA8gPdwfr3cu6sPwZRqWsc3Eg==
|
||||||
dependencies:
|
dependencies:
|
||||||
prop-types "^15.7.2"
|
prop-types "^15.7.2"
|
||||||
react-modal "^3.11.1"
|
react-modal "^3.11.1"
|
||||||
@ -12373,12 +12373,12 @@ react-is@^17.0.1:
|
|||||||
|
|
||||||
react-lifecycles-compat@^3.0.0:
|
react-lifecycles-compat@^3.0.0:
|
||||||
version "3.0.4"
|
version "3.0.4"
|
||||||
resolved "https://registry.npmmirror.com/react-lifecycles-compat/download/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
|
resolved "https://registry.npmjs.org/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
|
||||||
integrity sha1-TxonOv38jzSIqMUWv9p4+HI1I2I=
|
integrity sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==
|
||||||
|
|
||||||
react-modal@^3.11.1:
|
react-modal@^3.11.1:
|
||||||
version "3.14.4"
|
version "3.14.4"
|
||||||
resolved "https://registry.npmmirror.com/react-modal/download/react-modal-3.14.4.tgz#2ca7e8e9a180955e5c9508c228b73167c1e6f6a3"
|
resolved "https://registry.npmjs.org/react-modal/-/react-modal-3.14.4.tgz#2ca7e8e9a180955e5c9508c228b73167c1e6f6a3"
|
||||||
integrity sha512-8surmulejafYCH9wfUmFyj4UfbSJwjcgbS9gf3oOItu4Hwd6ivJyVBETI0yHRhpJKCLZMUtnhzk76wXTsNL6Qg==
|
integrity sha512-8surmulejafYCH9wfUmFyj4UfbSJwjcgbS9gf3oOItu4Hwd6ivJyVBETI0yHRhpJKCLZMUtnhzk76wXTsNL6Qg==
|
||||||
dependencies:
|
dependencies:
|
||||||
exenv "^1.2.0"
|
exenv "^1.2.0"
|
||||||
|
Loading…
Reference in New Issue
Block a user