Installations
npm install @sinoui/use-data-api
Developer Guide
Typescript
No
Module System
CommonJS
Score
65
Supply Chain
95.5
Quality
80.6
Maintenance
50
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
7,965
Last Day
1
Last Week
7
Last Month
36
Last Year
468
GitHub Statistics
1 Stars
43 Commits
3 Watching
15 Branches
5 Contributors
Bundle Size
62.03 kB
Minified
18.99 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.3
Package Id
@sinoui/use-data-api@1.0.3
Unpacked Size
47.45 kB
Size
9.60 kB
File Count
17
Total Downloads
Cumulative downloads
Total Downloads
7,965
Last day
-66.7%
1
Compared to previous day
Last week
16.7%
7
Compared to previous week
Last month
260%
36
Compared to previous month
Last year
-79.5%
468
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Peer Dependencies
1
@sinoui/use-data-api
使用 React Hooks 实现数据加载的库。
安装
1yarn add @sinoui/use-data-api
或者
1npm i --save @sinoui/use-data-api
使用
简单示例:
1import React, { useState } from 'react'; 2import useDataApi from '@sinoui/use-data-api'; 3 4interface User { 5 userId: string; 6 userName: string; 7} 8 9function UserList() { 10 const { data, isLoading, isError, doFetch } = useDataApi<User[]>( 11 '/users', 12 [], 13 ); 14 15 return ( 16 <div> 17 {isLoading && <div>正在加载数据</div>} 18 {isError && <div>加载数据失败</div>} 19 <ul> 20 {data.map((user) => ( 21 <li key={user.id}>{user.userName}</li> 22 ))} 23 </ul> 24 </div> 25 ); 26}
查询示例:
1import React, { useState } from 'react'; 2import useDataApi from '@sinoui/use-data-api'; 3 4interface User { 5 userId: string; 6 userName: string; 7} 8 9function UserList() { 10 const [searchText, setSearchText] = useState(''); 11 const { data, isLoading, isError, doFetch } = useDataApi<User[]>( 12 '/users', 13 [], 14 ); 15 16 const handleSearch = () => { 17 doFetch(`/users?text=${searchText}`); 18 setSearchText(''); 19 }; 20 21 return ( 22 <div> 23 <form> 24 <label>姓名</label> 25 <input 26 type="text" 27 value={searchText} 28 onChange={(event) => setSearchText(event.target.value)} 29 /> 30 <button onClick={handleSearch}>查询</button> 31 </form> 32 {isLoading && <div>正在加载数据</div>} 33 {isError && <div>加载数据失败</div>} 34 <ul> 35 {data.map((user) => ( 36 <li key={user.id}>{user.userName}</li> 37 ))} 38 </ul> 39 </div> 40 ); 41}
另外一个加载 Hacker News 的例子:
方法说明
1import useDataApi from '@sinoui/use-data-api'; 2 3interface DataSource<T> { 4 /** 5 * 从API中获取到的数据 6 */ 7 data: T; 8 /** 9 * 数据加载中状态。`true`表示数据加载中。 10 */ 11 isLoading: boolean; 12 /** 13 * 数据加载失败状态。`true`表示数据加载失败。 14 */ 15 isError: boolean; 16 /** 17 * 加载数据 18 * 19 * @param {string} url 获取数据的url 20 * @param {boolean} forceFetch 当指定的`url`与上一次请求的`url`一致时,是否发送API请求。 21 * 默认为`true`,表示发送请求。 22 */ 23 doFetch: (url?: string, forceFetch?: boolean) => void; 24 25 /** 26 * 重新加载数据 27 */ 28 reload: () => void; 29 30 /** 31 * 更新数据 32 */ 33 setData: (data: T) => void; 34} 35 36/** 37 * 数据加载hook 38 * 39 * @template T 40 * @param {string} defaultUrl 默认加载数据的链接 41 * @param {T} defaultValue 默认数据 42 * @param {HttpRequestConfig} httpRequestConfig 请求配置 43 * @returns {DataSource<T>} 44 */ 45function useDataApi<T>( 46 defaultUrl: string | undefined, 47 defaultValue: T, 48 httpRequestConfig: HttpRequestConfig, 49): DataSource<T>;
默认通过GET
方法发送 API 请求,获取到的响应数据直接作为dataSource.data
。可以通过httpRequestConfig
来调整,如下所示:
1const transformResponse = (data) => data.map((item) => item.userName); // 将人员信息列表转换成人名列表 2 3const dataSource = useDataApi('/users', [], { 4 transformResponse, 5 method: 'POST', 6});
更多请求配置参见Axios Request Config。
注意:如果url
为空(''
、null
、undefined
)时,则不会发送 API 请求。
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/20 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Reason
53 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-4w2v-q235-vp99
- Warn: Project is vulnerable to: GHSA-cph5-m8f7-6c5x
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-r6rj-9ch6-g264
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-h9rv-jmmf-4pgx
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More