Skip to content

Commit 0bd5497

Browse files
committed
fix cacheKey is not updated when default variables changed
1 parent 1eaf9e2 commit 0bd5497

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
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.5.2",
3+
"version": "0.5.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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ function createUseLazyApi<
210210
const prevVariablesRef = useRef<TApiVariables>()
211211
const cachedRef = useRef(defaultOpts.cached ?? true)
212212

213+
// update cacheKey when default variables changed
214+
const defaultVariables = useEnhancedMemo(defaultOpts.variables)
215+
useEnhancedEffect(() => {
216+
if (deepEqual(variablesRef.current, defaultVariables)) return
217+
setVariables((defaultVariables || {}) as TApiVariables)
218+
}, [defaultVariables, setVariables])
219+
213220
const fetch = useCallback(async (opts: Pick<UseLazyApiOptions<T, TData, TError, TVariables, K>, 'variables'> = {}) => {
214221
const api = apis[key]
215222
const {

0 commit comments

Comments
 (0)