0
0
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:
yude 2021-05-28 10:42:42 +09:00
parent 9ad8943b6d
commit 04abc7d4bf
Signed by: yude
GPG Key ID: EB0FE5D925C4A968

25
pages/lib/useRequest.js Normal file
View 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
},
)
}