-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Svelte 5: asynchronous derived api #13722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Like an effect that explicitly tracks dependencies, this looks like something that can be built in userland via |
Personally, I like setting promises in $derived variables. This is a sample component I wrote less than 1 hour ago for blog article: <script lang="ts">
import type { MyData } from './my-types.js';
type Props = {
pageSize?: number;
};
let {
pageSize = 20,
}: Props = $props();
let data = $derived(fetchMyData());
async function fetchMyData() {
const response = await fetch(`/api/my-data?pageSize=${pageSize}`, { ... });
if (response.ok) {
return JSON.parse(await response.json()) as MyData[];
}
return null;
}
</script>
{#await data}
<Spinner>Loading data...</Spinner>
{:then items}
<ul>
{#each items as item (item.id)}
<li>
<!-- Render data here -->
</li>
{/each}
<ul>
{:catch reason}
<span>Oops! - {reason}</span>
{/await} The fact that the |
@webJose That is actually a great approach. Thanks for the insight |
This is not working in 5.19.9. Data is now object with $on, $state |
It seems partially working from the functionality perspective, but placing a fetching derived during the initialization cause on SSR this logs:
and since is inside a component that would require either to load the data from the serverside to the parent and pass them to che child, or use an $effect/onMount where I can't declare a derived |
@diramazioni if you're talking about my proposed approach, then yes, Sveltekit is special because when running on the server side, Node's |
Describe the problem
I love the api surrounding the derived store which also allows you to asynchronously update the value of the store and I was wondering whether we can have something like that when working with the new $derived rune. This could prevent usage of $effect rune in many scenarios.
Describe the proposed solution
In the current api of the derived stores, derived takes the dependecy as its first argument, the callback to update the derivedStore as its second and the intialValue as its third argument. The sad part is that it only accepts stores as its first argument. Maybe we can have something similar but with $state or other $derived runes as the dependency. like below
Where the first argument is the dependency $state or $derived, the second is the callback function and the third is the initial value of the delayedCondition.
Importance
nice to have
The text was updated successfully, but these errors were encountered: