The Wayback Machine - https://web.archive.org/web/20200528054756/https://github.com/sveltejs/svelte/issues/4785
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

add example for reactive declaration with a function? #4785

Open
lingtalfi opened this issue May 5, 2020 · 0 comments
Open

add example for reactive declaration with a function? #4785

lingtalfi opened this issue May 5, 2020 · 0 comments
Labels

Comments

@lingtalfi
Copy link

@lingtalfi lingtalfi commented May 5, 2020

Hi, I was trying this code and it didn't work:

   let uFiles = [];
   $: totalSize = getTotalSize();

    function getTotalSize() {
        let total = 0;
        uFiles.forEach(uFile => {
            if (uFile.size) {
                total += uFile.size;
            }
        })
        return total;
    }

When I clicked a remove button which removed an uFile (by updating the uFiles array), the totalSize variable wasn't updated.
So I found that this was working:

    let uFiles = [];
    $: totalSize = getTotalSize(uFiles);

    function getTotalSize(_uFiles) {
        let total = 0;
        _uFiles.forEach(uFile => {
            if (uFile.size) {
                total += uFile.size;
            }
        })
        return total;
    }

Re-reading the docs, I saw that this was explained, but kind of hidden (https://svelte.dev/docs#3_$_marks_a_statement_as_reactive).

I believe adding an explicit example with a function call wouldn't hurt much, but would make things much easier to grab, rather than discovering through error/trial.

@Conduitry Conduitry added the docs label May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.