Skip to content

Reactivity not applying to action parameters #7429

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

Closed
johnnysprinkles opened this issue Apr 7, 2022 · 5 comments
Closed

Reactivity not applying to action parameters #7429

johnnysprinkles opened this issue Apr 7, 2022 · 5 comments

Comments

@johnnysprinkles
Copy link

Describe the bug

Sorry if this is a dupe or expected behavior...

I know that reactivity isn't supposed to transcend function calls, so people add extra params to the function calls (needed or not) to indicate they should be reactive. It's just about the name of the variable being present at the original site. So here I have an example of an action that mutates a local state variable, and I was expecting reactivity to trigger based on my referencing the name of the variable in the use:recordWidth={container} part. See repl here:

https://svelte.dev/repl/2128a5163144488285449cbf4514ffbf?version=3.46.6

I'd expect the see the width of the element printed out. If I interrogate the value later it is correct, it's just not triggering reactivity like I'd expect.

Reproduction

See above.

Logs

No response

System Info

System:
    OS: macOS 11.6.2
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 3.33 GB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node
    Yarn: 1.22.17 - /opt/brew/bin/yarn
    npm: 8.3.1 - ~/.nvm/versions/node/v16.14.0/bin/npm
  Browsers:
    Chrome: 100.0.4896.75
    Firefox: 99.0
    Safari: 15.2
    Safari Technology Preview: 15.4
  npmPackages:
    svelte: ^3.44.0 => 3.46.4

Severity

annoyance

@Conduitry
Copy link
Member

This is the intended behavior. There's no invalidation of a top-level variable in the component that Svelte can see at compile time, and so nothing is invalidated. If you need to do something that is (effectively) the invalidation of variables not known until runtime, you can pass in a store to the action.

@johnnysprinkles
Copy link
Author

johnnysprinkles commented Apr 7, 2022

Cool, but I am interested in why this can't be seen at compile time...

My understanding is if you had code like this:

<button on:click={() => doSomethingWith(foo)}></button>

At compile time it sees the foo in there and add something like $$invalidate(foo).

For actions, if you had:

function myAction(node, params) { ... }
<button use:myAction={foo}></button>

Wouldn't the compiler be able to see the "foo" in just the same way, and add $$invalidate(foo)?

@mrkishi
Copy link
Member

mrkishi commented Apr 7, 2022

Top-level variables are invalidated when they are directly assigned to—it doesn't work if you modify them indirectly (eg. through another name/reference).

Your on:click example will also not trigger an update to foo, even if doSomethingWith changes it through its argument.

@johnnysprinkles
Copy link
Author

Hmm, maybe this doesn't work like I think it does, even event handlers don't seem to be reactive in this way, i.e.

https://svelte.dev/repl/769fa87bc8464fdaa7f56fcb8414a846?version=3.46.6

@johnnysprinkles
Copy link
Author

Ah I'm dumb, I was confusing upstream reactive dependencies with direct reactivity, which needs un unaliased lvalue, sorry for the noise.

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