Skip to content

Commit b16c2c1

Browse files
authored
docs(preloading): correct effect snippets (#3204)
1 parent 76fd97e commit b16c2c1

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

docs/framework/react/guide/preloading.md

+26-18
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,19 @@ function Component() {
107107
const router = useRouter()
108108

109109
useEffect(() => {
110-
try {
111-
const matches = await router.preloadRoute({
112-
to: postRoute,
113-
params: { id: 1 },
114-
})
115-
} catch (err) {
116-
// Failed to preload route
110+
async function preload() {
111+
try {
112+
const matches = await router.preloadRoute({
113+
to: postRoute,
114+
params: { id: 1 },
115+
})
116+
} catch (err) {
117+
// Failed to preload route
118+
}
117119
}
118-
}, [])
120+
121+
preload()
122+
}, [router])
119123

120124
return <div />
121125
}
@@ -128,17 +132,21 @@ function Component() {
128132
const router = useRouter()
129133

130134
useEffect(() => {
131-
try {
132-
const postsRoute = router.routesByPath['/posts']
133-
await Promise.all([
134-
router.loadRouteChunk(router.routesByPath['/']),
135-
router.loadRouteChunk(postsRoute),
136-
router.loadRouteChunk(postsRoute.parentRoute),
137-
])
138-
} catch (err) {
139-
// Failed to preload route chunk
135+
async function preloadRouteChunks() {
136+
try {
137+
const postsRoute = router.routesByPath['/posts']
138+
await Promise.all([
139+
router.loadRouteChunk(router.routesByPath['/']),
140+
router.loadRouteChunk(postsRoute),
141+
router.loadRouteChunk(postsRoute.parentRoute),
142+
])
143+
} catch (err) {
144+
// Failed to preload route chunk
145+
}
140146
}
141-
}, [])
147+
148+
preloadRouteChunks()
149+
}, [router])
142150

143151
return <div />
144152
}

0 commit comments

Comments
 (0)