Skip to content

Fix for Svelte 5 timing bug in SVAR Svelte DataGrid documentation #55

@joelparkerhenderson

Description

@joelparkerhenderson

When I run this tutorial code: https://docs.svar.dev/svelte/grid/getting_started/

Then Svelte throws a 500 error.

As far as I can tell, there's a mount timing issue in Svelte 5 that's causing the grid to fail. What I see is if I edit the page HTML then the grid loads, then if I reload in the browser, the grid fails with a 500 error.

This fix solves it for me. Is there a better way? Thank you.

<script>
    // Import the SVAR Svelte DataGrid
    import { Grid } from "@svar-ui/svelte-grid";

    // Apply the SVAR prebuilt theme named Willow.
    import { Willow } from "@svar-ui/svelte-grid";

    const columns = [{
            id: "id",
            width: 50,
            header: "Id",
            footer: "Id",
        },
        {
            id: "name",
            width: 100,
            header: "Name",
            footer: "Name",
        },
    ];

    const data = [{
            id: 1,
            name: "Alice",
        },
        {
            id: 2,
            name: "Bob",
        },
    ];

    // Fix for Svelte 5 and SVAR Svelte DataGrid.
    let mounted = $state(false);
    import { onMount } from "svelte";
    onMount(() => { mounted = true; });
</script>

<!-- Apply the SVAR prebuilt theme named Willow. -->
<Willow>
    <!-- Wait for the data to be loaded because of timing. -->
    {#if mounted}
        <!-- Render the SVAR Svelte DataGrid. -->
        <Grid {data} {columns} />
    {/if}
</Willow>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions