File tree Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ function getTestComponentModule() {
15
15
return {
16
16
isLoaded : ( ) => loaded ,
17
17
loadCalls : ( ) => loadCalls ,
18
+ OriginalComponent : TestComponent ,
18
19
TestComponent : async ( ) => {
19
20
loaded = true ;
20
21
loadCalls ++ ;
@@ -138,4 +139,13 @@ describe("lazy", () => {
138
139
await waitFor ( ( ) => expect ( screen . queryByText ( "bar baz" ) ) . toBeTruthy ( ) ) ;
139
140
expect ( ref ?. current ?. textContent ) . toBe ( "bar baz" ) ;
140
141
} ) ;
142
+
143
+ it ( "returns the preloaded component when the preload promise resolves" , async ( ) => {
144
+ const { TestComponent, OriginalComponent } = getTestComponentModule ( ) ;
145
+ const LazyTestComponent = lazy ( TestComponent ) ;
146
+
147
+ const preloadedComponent = await LazyTestComponent . preload ( )
148
+
149
+ expect ( preloadedComponent ) . toBe ( OriginalComponent ) ;
150
+ } ) ;
141
151
} ) ;
Original file line number Diff line number Diff line change 1
1
import { ComponentType , createElement , forwardRef , lazy } from "react" ;
2
2
3
3
export type PreloadableComponent < T extends ComponentType < any > > = T & {
4
- preload : ( ) => Promise < void > ;
4
+ preload : ( ) => Promise < T > ;
5
5
} ;
6
6
7
7
export default function lazyWithPreload < T extends ComponentType < any > > (
8
8
factory : ( ) => Promise < { default : T } >
9
9
) : PreloadableComponent < T > {
10
10
const LazyComponent = lazy ( factory ) ;
11
- let factoryPromise : Promise < void > | undefined ;
11
+ let factoryPromise : Promise < T > | undefined ;
12
12
let LoadedComponent : T | undefined ;
13
13
14
14
const Component = ( forwardRef ( function LazyWithPreload ( props , ref ) {
@@ -22,6 +22,7 @@ export default function lazyWithPreload<T extends ComponentType<any>>(
22
22
if ( ! factoryPromise ) {
23
23
factoryPromise = factory ( ) . then ( ( module ) => {
24
24
LoadedComponent = module . default ;
25
+ return LoadedComponent ;
25
26
} ) ;
26
27
}
27
28
You can’t perform that action at this time.
0 commit comments