Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/_internal/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Cache, State, GlobalState } from '../types'
import { SWRGlobalState } from './global-state'
import { isUndefined, mergeObjects } from './shared'
import { isUndefined, mergeObjects, noop } from './shared'

const EMPTY_CACHE = {}
const INITIAL_CACHE: Record<string, any> = {}
Expand All @@ -19,7 +19,7 @@ export const createCacheHelper = <Data = any, T = State<Data, any>>(
cache: Cache,
key: string | undefined
) => {
const state = SWRGlobalState.get(cache) as GlobalState
const state = SWRGlobalState.get(cache) as GlobalState | undefined
return [
// Getter
() => ((!isUndefined(key) && cache.get(key)) || EMPTY_CACHE) as T,
Expand All @@ -34,11 +34,16 @@ export const createCacheHelper = <Data = any, T = State<Data, any>>(
INITIAL_CACHE[key] = prev
}

state[5](key, mergeObjects(prev, info), prev || EMPTY_CACHE)
// Guard against undefined state - can happen in React Native New
// Architecture where SWRGlobalState.get(cache) returns undefined
// during initial renders before the provider has initialized.
if (state) {
state[5](key, mergeObjects(prev, info), prev || EMPTY_CACHE)
}
}
},
// Subscriber
state[6],
state ? state[6] : () => noop,
// Get server cache snapshot
() => {
if (!isUndefined(key)) {
Expand Down
Loading