Skip to content

Commit c6b01f9

Browse files
committed
[useApi] skip option
1 parent ddf7812 commit c6b01f9

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-api-fetching",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Make fetching API easier with React's hooks + context",
55
"source": "src/index.tsx",
66
"main": "dist/main.js",

src/index.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ const defaultResult: ApiResult = {
7777

7878
interface UseLazyApiOptions<TData, TError, TVariables> {
7979
variables?: TVariables
80-
onFetch?: () => void
81-
onCompleted?: (params: { data: TData | null, error: TError | null }) => void
80+
onFetch?: () => Promise<any>
81+
onCompleted?: (params: { data: TData | null, error: TError | null }) => Promise<any>
8282
}
8383

8484
function createUseLazyApi<
@@ -106,7 +106,7 @@ function createUseLazyApi<
106106
variables = {}
107107
} = deepMerge(defaultOptsRef.current, opts)
108108

109-
if (onFetch) onFetch()
109+
if (onFetch) await onFetch()
110110

111111
setResult({ loading: true })
112112

@@ -117,9 +117,9 @@ function createUseLazyApi<
117117
error = err as TApiError
118118
}
119119

120-
setResult({ loading: false, data, error })
120+
if (onCompleted) await onCompleted({ data, error })
121121

122-
if (onCompleted) onCompleted({ data, error })
122+
setResult({ loading: false, data, error })
123123

124124
return { data, error }
125125
}, [fetcher, api, setResult])
@@ -145,6 +145,10 @@ function createUseMutationApi<
145145
}
146146
}
147147

148+
interface UseApiOptions<TData, TError, TVariables> extends UseLazyApiOptions<TData, TError, TVariables> {
149+
skip?: boolean
150+
}
151+
148152
function createUseApi<
149153
T,
150154
TData extends Record<keyof T, any>,
@@ -157,22 +161,36 @@ function createUseApi<
157161
TApiData extends TData[K],
158162
TApiError extends TError[K],
159163
TApiVariables extends TVariables[K]
160-
>(key: K, opts: UseLazyApiOptions<TApiData, TApiError, TApiVariables> = {}) {
161-
const [loading, setLoading] = useReducer((_: boolean, s: boolean) => s, false)
162-
const onFetch = useCallback(() => setLoading(true), [])
163-
const onCompleted = useCallback(() => setLoading(false), [])
164+
>(key: K, opts: UseApiOptions<TApiData, TApiError, TApiVariables> = {}) {
165+
const { skip = false, ...lazyOpts } = opts
166+
167+
const calledRef = useRef(!skip)
168+
const loadingRef = useRef(!skip)
169+
170+
const onFetch = useCallback(async () => {
171+
calledRef.current = true
172+
loadingRef.current = true
173+
}, [])
174+
const onCompleted = useCallback(async () => {
175+
loadingRef.current = false
176+
}, [])
164177
const [fetch, result] = useLazyApi<K, TApiData, TApiError, TApiVariables>(key, {
165-
...opts,
178+
...lazyOpts,
166179
onFetch,
167180
onCompleted
168181
})
182+
169183
const latestVariables = useMemoValue(opts.variables)
170184

171-
useEffect(() => { fetch() }, [fetch, latestVariables])
185+
useEffect(() => {
186+
if (skip) return
187+
fetch()
188+
}, [skip, fetch, latestVariables])
172189

173190
return {
174191
...result,
175-
loading
192+
loading: loadingRef.current,
193+
called: calledRef.current
176194
}
177195
}
178196
}

0 commit comments

Comments
 (0)