Skip to content

Commit 1c1e311

Browse files
authored
Merge pull request #7469 from QwikDev/v2-resource-initial-state
fix: correctly handle initial resource state
2 parents 78c0cdd + acb0328 commit 1c1e311

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

.changeset/eighty-ligers-wink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@qwik.dev/core': patch
3+
---
4+
5+
fix: correctly handle initial resource state

packages/qwik/src/core/tests/use-resource.spec.tsx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,72 @@ describe.each([
519519
</Component>
520520
);
521521
});
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+
});
522590
});

packages/qwik/src/core/use/use-resource.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export const Resource = <T>(props: ResourceProps<T>): JSXOutput => {
185185

186186
function getResourceValueAsPromise<T>(props: ResourceProps<T>): Promise<JSXOutput> | JSXOutput {
187187
const resource = props.value as ResourceReturnInternal<T> | Promise<T> | Signal<T>;
188-
if (isResourceReturn(resource) && resource.value) {
188+
if (isResourceReturn(resource)) {
189189
const isBrowser = !isServerPlatform();
190190
if (isBrowser) {
191191
// create a subscription for the resource._state changes
@@ -204,10 +204,16 @@ function getResourceValueAsPromise<T>(props: ResourceProps<T>): Promise<JSXOutpu
204204
}
205205
}
206206
}
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+
}
211217
} else if (isPromise(resource)) {
212218
return resource.then(
213219
useBindInvokeContext(props.onResolved),

0 commit comments

Comments
 (0)