useApiCollectionFetcher
注意
使用该hooks之前,请确保按照文档方式初始化了
import {IHttpClient,IContext} from "cyl-hooks-tools"
export interface Test {
id:number
name:string
createTime:number
}
export function getTests(ctx:IContext<IHttpClient,object>):Promise<Test[]>{
return ctx.get("https://chuyanlon.github.io/cyl-hooks-tools-doc/api/tests.json")
}
export async function getTestCasedById(
ctx:IContext<IHttpClient,object>,id:number[]
):Promise<Test[]>{
return ctx.get("https://chuyanlon.github.io/cyl-hooks-tools-doc/api/tests.json",{
id
})
}
import {useApi,useApiState,IContext,IHttpClient} from "cyl-hooks-tools"
import {useApiCollectionFetcher} from "cyl-hooks-tools/useCollector"
import {getTests,getTestCasedById} from "./test.ts"
export default function Component(){
const api = useApi<IContext<IHttpClient,object>>()
const [testId,setTestId] = useState(0)
const [tests] = useApiState(api,getTests)
const [cases,loading,error] =useApiCollectionFetcher(api,tests,t=>t?.id,getTestCasedById)
return <div>
<div>状态:{loading ? "请求中" : "请求完成"}</div>
<div>错误:{error ? error?.message : "没有错误"}</div>
<div>结果:{JSON.stringify(cases)}</div>
</div>
}
// Make sure to add code blocks to your code group
上次更新: 2023/05/08, 19:10:56