File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ function getTestComponentModule() {
1515 return {
1616 isLoaded : ( ) => loaded ,
1717 loadCalls : ( ) => loadCalls ,
18+ OriginalComponent : TestComponent ,
1819 TestComponent : async ( ) => {
1920 loaded = true ;
2021 loadCalls ++ ;
@@ -138,4 +139,13 @@ describe("lazy", () => {
138139 await waitFor ( ( ) => expect ( screen . queryByText ( "bar baz" ) ) . toBeTruthy ( ) ) ;
139140 expect ( ref ?. current ?. textContent ) . toBe ( "bar baz" ) ;
140141 } ) ;
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+ } ) ;
141151} ) ;
Original file line number Diff line number Diff line change 11import { ComponentType , createElement , forwardRef , lazy } from "react" ;
22
33export type PreloadableComponent < T extends ComponentType < any > > = T & {
4- preload : ( ) => Promise < void > ;
4+ preload : ( ) => Promise < T > ;
55} ;
66
77export default function lazyWithPreload < T extends ComponentType < any > > (
88 factory : ( ) => Promise < { default : T } >
99) : PreloadableComponent < T > {
1010 const LazyComponent = lazy ( factory ) ;
11- let factoryPromise : Promise < void > | undefined ;
11+ let factoryPromise : Promise < T > | undefined ;
1212 let LoadedComponent : T | undefined ;
1313
1414 const Component = ( forwardRef ( function LazyWithPreload ( props , ref ) {
@@ -22,6 +22,7 @@ export default function lazyWithPreload<T extends ComponentType<any>>(
2222 if ( ! factoryPromise ) {
2323 factoryPromise = factory ( ) . then ( ( module ) => {
2424 LoadedComponent = module . default ;
25+ return LoadedComponent ;
2526 } ) ;
2627 }
2728
You can’t perform that action at this time.
0 commit comments