Skip to content

Commit 0411269

Browse files
committed
Reload lazy props in WhenVisible component when they become undefined
1 parent c76f35b commit 0411269

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

packages/react/src/WhenVisible.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReloadOptions, router } from '@inertiajs/core'
2-
import { createElement, ReactElement, useCallback, useEffect, useRef, useState } from 'react'
2+
import { createElement, ReactElement, useCallback, useEffect, useMemo, useRef } from 'react'
3+
import usePage from './usePage'
34

45
interface WhenVisibleProps {
56
children: ReactElement | number | string
@@ -16,15 +17,29 @@ const WhenVisible = ({ children, data, params, buffer, as, always, fallback }: W
1617
as = as ?? 'div'
1718
fallback = fallback ?? null
1819

19-
const [loaded, setLoaded] = useState(false)
20+
const loaded = useRef<boolean>(false)
2021
const hasFetched = useRef<boolean>(false)
2122
const fetching = useRef<boolean>(false)
2223
const ref = useRef<HTMLDivElement>(null)
24+
const pageProps = usePage().props
25+
const keys = useMemo(() => (params ? (params.only ?? []) : Array.isArray(data) ? data : [data]), [data, params])
26+
27+
loaded.current = useMemo(() => {
28+
if (fetching.current || !loaded.current) return loaded.current
29+
30+
const propsLoaded = !keys.length ? true : keys.every((key) => pageProps[key] !== undefined)
31+
32+
if (!propsLoaded) {
33+
hasFetched.current = false
34+
}
35+
36+
return propsLoaded
37+
}, [pageProps, keys])
2338

2439
const getReloadParams = useCallback<() => Partial<ReloadOptions>>(() => {
2540
if (data) {
2641
return {
27-
only: (Array.isArray(data) ? data : [data]) as string[],
42+
only: keys,
2843
}
2944
}
3045

@@ -33,10 +48,10 @@ const WhenVisible = ({ children, data, params, buffer, as, always, fallback }: W
3348
}
3449

3550
return params
36-
}, [params, data])
51+
}, [params, data, keys])
3752

3853
useEffect(() => {
39-
if (!ref.current) {
54+
if (!ref.current || loaded.current) {
4055
return
4156
}
4257

@@ -66,7 +81,7 @@ const WhenVisible = ({ children, data, params, buffer, as, always, fallback }: W
6681
reloadParams.onStart?.(e)
6782
},
6883
onFinish: (e) => {
69-
setLoaded(true)
84+
loaded.current = true
7085
fetching.current = false
7186
reloadParams.onFinish?.(e)
7287

@@ -86,20 +101,20 @@ const WhenVisible = ({ children, data, params, buffer, as, always, fallback }: W
86101
return () => {
87102
observer.disconnect()
88103
}
89-
}, [ref, getReloadParams, buffer])
104+
}, [ref, getReloadParams, buffer, loaded.current])
90105

91-
if (always || !loaded) {
106+
if (always || !loaded.current) {
92107
return createElement(
93108
as,
94109
{
95110
props: null,
96111
ref,
97112
},
98-
loaded ? children : fallback,
113+
loaded.current ? children : fallback,
99114
)
100115
}
101116

102-
return loaded ? children : null
117+
return loaded.current ? children : null
103118
}
104119

105120
WhenVisible.displayName = 'InertiaWhenVisible'

0 commit comments

Comments
 (0)