Skip to content
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

Rendering the page results in hydration_mismatch in svelte #109

Open
tiehfood opened this issue Jan 21, 2025 · 7 comments
Open

Rendering the page results in hydration_mismatch in svelte #109

tiehfood opened this issue Jan 21, 2025 · 7 comments

Comments

@tiehfood
Copy link

NumberFlow version

@number-flow/[email protected]

Framework version

[email protected]

Describe the bug and the steps to reproduce it

I have values

let total: number = $state(0);
let flows: Flow[] = $state([]);

which could change depending on data loaded on the page.

    function setTotal() {
        let sum = flows.reduce((accumulator, currentFlow) => accumulator + currentFlow.amount, 0);
        total = Math.round((sum + Number.EPSILON) * 100) / 100
    }

Using

<NumberFlow plugins={[continuous]} format={{ style: 'currency', currency: 'EUR', trailingZeroDisplay: 'stripIfInteger' }} value={total} />

results in the hydration_mismatch warning

Minimal reproduction (greatly appreciated)

No response

@barvian
Copy link
Owner

barvian commented Jan 21, 2025

Hi, could you try adding a locales prop to your component e.g. locales="en-US"? And if that doesn't work, could you provide a reproduction URL?

@tiehfood
Copy link
Author

tiehfood commented Jan 22, 2025

It doesn't work for me. Here is a simple page:

<script lang="ts">
    import "../app.css";
    import { Card } from "flowbite-svelte";
    import { onMount } from "svelte";
    import NumberFlow from "@number-flow/svelte";

    type Item = {
        name: string;
        amount: number;
    };

    let items: Item[] = $state([]);
    let total: number = $state(0);

    onMount(async () => await getItems())

    async function getItems() {
        items = [{"name": "test", "amount": 10}];
        setTotal();
    }

    function setTotal() {
        let sum = items.reduce((accumulator, currentItem) => accumulator + currentItem.amount, 0);
        total = Math.round((sum + Number.EPSILON) * 100) / 100;
    }
</script>

<Card >
    <p>Total</p>
    <NumberFlow format={{ style: 'currency', currency: 'EUR' }} value={total} />
</Card>

@barvian
Copy link
Owner

barvian commented Jan 24, 2025

I can confirm I see this warning in newer versions of SvelteKit. I'll look into a workaround (open to suggestions). The hydration mismatch is due to NumberFlow using declarative shadow DOM with a custom element under the hood, and I try to keep all the DSD logic out of the client bundle for size reasons. React and Vue both let you suppress hydration warnings so hopefully Svelte has something similar 🤔

@llama-for3ver
Copy link

Not to be that guy, but anything on this?

@barvian
Copy link
Owner

barvian commented Feb 13, 2025

Hi, I've looked through the Svelte 5 docs but I don't see a way to disable this hydration warning. Open to suggestions! @tiehfood and @llama-for3ver, NumberFlow is still working correctly for you, other than the console warning?

@tiehfood
Copy link
Author

Despite the warning it's working fine

@llama-for3ver
Copy link

Ditto. It's no more than an annoyance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants