solid-js use-request hooks library
npm install solid-requestWith a strong ability to manage network requests, Hook has a flying experience
``bash`
npm i solid-request
`typescript`
import useRequest from "solid-request"
useRequest Through the plug-in organization code, the core code is easy to understand, and can be easily expanded to more advanced functions. Capacity is now available to include
- Automatic/manual request
- Support Typescript
- Polling
- Debounce
- Throttle
- Refresh on window focus
- Error retry
- Loading delay
- SWR(stale-while-revalidate)
- Caching
- Plugins
By default, the first parameter of useRequest is an asynchronous function, which is automatically executed when the component is initialized. At the same time, it automatically manages the status of loading, data, error of the asynchronous function.
`js`
const { data, error, loading } = useRequest(service)
`jsx
export async function getList({ id }: { id: number }): Promise<{
id: number
title: string
body: string
userId: number
}> {
console.log(id)
return fetch(https://jsonplaceholder.typicode.com/posts/${id}).then(
(response) => response.json()
)
}
function App() {
const [count, setCount] = createSignal(1)
const { data, loading } = useRequest(() => getList({ id: count() }), {
manual: false,
ready: true,
refreshDeps: true,
loadingDelay: 300,
})
return (
The document is under development, for more APIs, please see the vue version of useRequest
Result
| Property | Description | Type |
| ------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
| data | Data returned by service |
Accessor |Accessor \| undefined |Accessor |run(1, 2, 3), then params is equal to [1, 2, 3] | Accessor |onError(...params: TParams) => void |run, but it returns a Promise, so you need to handle the exception yourself. | (...params: TParams) => Promise |run again | () => void |runAsync again | () => Promise |data directly | (data?: TData / ((oldData?: TData) => (TData / undefined))) => void |() => void || Property | Description | Type | Default |
| ------------- | ------------------------------------------------------------ | ---------------------------------------------------- | ------- |
| initialData | Init data | TData \| undefined | |
| manual |
false. That is, the service is automatically executed during initialization.true, you need to manually call run or runAsync to trigger execution. boolean | false |TParams | - |useFormatResult | (response: TData) => any | - |(params: TParams) => void | - |(data: TData, params: TParams) => void | - |(e: Error, params: TParams) => void | - |(params: TParams, data?: TData, e?: Error) => void | - |