Gathering detailed insights and metrics for @sinoui/use-data-api
Gathering detailed insights and metrics for @sinoui/use-data-api
Gathering detailed insights and metrics for @sinoui/use-data-api
Gathering detailed insights and metrics for @sinoui/use-data-api
npm install @sinoui/use-data-api
Typescript
Module System
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
43 Commits
2 Watchers
15 Branches
5 Contributors
Updated on Feb 26, 2021
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
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
1
使用 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 请求。
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/20 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
56 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
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