File tree 1 file changed +26
-18
lines changed
docs/framework/react/guide
1 file changed +26
-18
lines changed Original file line number Diff line number Diff line change @@ -107,15 +107,19 @@ function Component() {
107
107
const router = useRouter ()
108
108
109
109
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
+ }
117
119
}
118
- }, [])
120
+
121
+ preload ()
122
+ }, [router ])
119
123
120
124
return <div />
121
125
}
@@ -128,17 +132,21 @@ function Component() {
128
132
const router = useRouter ()
129
133
130
134
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
+ }
140
146
}
141
- }, [])
147
+
148
+ preloadRouteChunks ()
149
+ }, [router ])
142
150
143
151
return <div />
144
152
}
You can’t perform that action at this time.
0 commit comments