Skip to content

Manual sync state invalidation does not force update in Svelte 5 #13821

Open
@brunnerh

Description

@brunnerh

Describe the bug

In Svelte 4 you can do something like this to force a DOM update based on state:

value = null;
value = valueToSet;

In Svelte 5, this does not force the update in cases where value == valueToSet.

Admittedly, this is a bit of a hack.
Workaround:

value = null;
await tick();
value = valueToSet;

Reproduction

Svelte 5 (Runes mode seems to behave the same)
Svelte 4

Code
<script>
    let value = 0;
    let min = 0;
    let max = 100;
    let step = 1;

    function onInputChanged(event) {
        const inputElement = event.target;
        const newValue = inputElement.value;
        const valueToSet = computeValueToSet(newValue);

        value = null; // Does nothing in Svelte 5
        value = valueToSet;
    }

    function computeValueToSet(newValue) {
        if (newValue === "")
            return value;

        const newInt = parseInt(newValue);

        const isInRange = min <= newInt && newInt <= max;
        if (!isInRange)
            return value;

        return newInt;
    }
</script>

<div>
    <input type=range {min} {max} {step} bind:value /> <br>
		Type a value outside [{min}, {max}]
    <input
        type=number
        {value}
        on:input={onInputChanged}
        {min}
        {max}
        {step}
    />
</div>

Logs

No response

System Info

REPL

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions