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

Closed
lingtalfi opened this issue May 5, 2020 · 1 comment
Closed

add example for reactive declaration with a function? #4785

lingtalfi opened this issue May 5, 2020 · 1 comment

Comments

@lingtalfi
Copy link

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.

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

No branches or pull requests

5 participants