mirror of
https://github.com/yude-jp/yude.jp
synced 2024-12-23 04:30:11 +09:00
Add lib: useRequest.js
This commit is contained in:
parent
9ad8943b6d
commit
04abc7d4bf
25
pages/lib/useRequest.js
Normal file
25
pages/lib/useRequest.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import useSWR from 'swr'
|
||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
|
export default function useRequest(request, { initialData, ...config } = {}) {
|
||||||
|
return useSWR(
|
||||||
|
request && JSON.stringify(request),
|
||||||
|
() => axios(request || {}).then(response => response.data),
|
||||||
|
{
|
||||||
|
...config,
|
||||||
|
initialData: initialData && {
|
||||||
|
status: 200,
|
||||||
|
statusText: 'InitialData',
|
||||||
|
headers: {},
|
||||||
|
data: initialData
|
||||||
|
},
|
||||||
|
revalidateOnFocus: false,
|
||||||
|
revalidateOnMount:false,
|
||||||
|
revalidateOnReconnect: false,
|
||||||
|
refreshWhenOffline: false,
|
||||||
|
refreshWhenHidden: false,
|
||||||
|
refreshInterval: 0,
|
||||||
|
shouldRetryOnError: false
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user