File tree Expand file tree Collapse file tree 3 files changed +84
-5
lines changed Expand file tree Collapse file tree 3 files changed +84
-5
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @qwik.dev/core ' : patch
3
+ ---
4
+
5
+ fix: correctly handle initial resource state
Original file line number Diff line number Diff line change @@ -519,4 +519,72 @@ describe.each([
519
519
</ Component >
520
520
) ;
521
521
} ) ;
522
+
523
+ it ( 'should render array from resource' , async ( ) => {
524
+ const Cmp = component$ ( ( ) => {
525
+ const resource = useResource$ ( ( ) => {
526
+ return [
527
+ {
528
+ id : 1 ,
529
+ name : 'John Doe' ,
530
+ age : 30 ,
531
+ } ,
532
+ {
533
+ id : 2 ,
534
+ name : 'Jane Smith' ,
535
+ age : 25 ,
536
+ } ,
537
+ ] ;
538
+ } ) ;
539
+
540
+ return (
541
+ < Resource
542
+ value = { resource }
543
+ onResolved = { ( res ) => {
544
+ return res . map ( ( val , index ) => (
545
+ < div key = { index } >
546
+ < p > { val . id } </ p >
547
+ < p > { val . name } </ p >
548
+ < p > { val . age } </ p >
549
+ </ div >
550
+ ) ) ;
551
+ } }
552
+ />
553
+ ) ;
554
+ } ) ;
555
+
556
+ const { vNode } = await render ( < Cmp /> , { debug } ) ;
557
+ expect ( vNode ) . toMatchVDOM (
558
+ < Component ssr-required >
559
+ < InlineComponent >
560
+ < Fragment >
561
+ < Awaited >
562
+ < div >
563
+ < p >
564
+ < Signal > 1</ Signal >
565
+ </ p >
566
+ < p >
567
+ < Signal > John Doe</ Signal >
568
+ </ p >
569
+ < p >
570
+ < Signal > 30</ Signal >
571
+ </ p >
572
+ </ div >
573
+ < div >
574
+ < p >
575
+ < Signal > 2</ Signal >
576
+ </ p >
577
+ < p >
578
+ < Signal > Jane Smith</ Signal >
579
+ </ p >
580
+ < p >
581
+ < Signal > 25</ Signal >
582
+ </ p >
583
+ </ div >
584
+ </ Awaited >
585
+ </ Fragment >
586
+ </ InlineComponent >
587
+ </ Component >
588
+ ) ;
589
+ } ) ;
522
590
} ) ;
Original file line number Diff line number Diff line change @@ -185,7 +185,7 @@ export const Resource = <T>(props: ResourceProps<T>): JSXOutput => {
185
185
186
186
function getResourceValueAsPromise < T > ( props : ResourceProps < T > ) : Promise < JSXOutput > | JSXOutput {
187
187
const resource = props . value as ResourceReturnInternal < T > | Promise < T > | Signal < T > ;
188
- if ( isResourceReturn ( resource ) && resource . value ) {
188
+ if ( isResourceReturn ( resource ) ) {
189
189
const isBrowser = ! isServerPlatform ( ) ;
190
190
if ( isBrowser ) {
191
191
// create a subscription for the resource._state changes
@@ -204,10 +204,16 @@ function getResourceValueAsPromise<T>(props: ResourceProps<T>): Promise<JSXOutpu
204
204
}
205
205
}
206
206
}
207
- return resource . value . then (
208
- useBindInvokeContext ( props . onResolved ) ,
209
- useBindInvokeContext ( props . onRejected )
210
- ) ;
207
+ const value = resource . value ;
208
+ if ( value ) {
209
+ return value . then (
210
+ useBindInvokeContext ( props . onResolved ) ,
211
+ useBindInvokeContext ( props . onRejected )
212
+ ) ;
213
+ } else {
214
+ // this is temporary value until the `runResource` is executed and promise is assigned to the value
215
+ return Promise . resolve ( undefined ) ;
216
+ }
211
217
} else if ( isPromise ( resource ) ) {
212
218
return resource . then (
213
219
useBindInvokeContext ( props . onResolved ) ,
You can’t perform that action at this time.
0 commit comments