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

Make an assignment reactive without $: if it has reactive dependencies #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bomzj
Copy link

@bomzj bomzj commented Jan 1, 2023

  • Start Date: 2023-01-01
  • RFC PR: (leave this empty)
  • Svelte Issue: (leave this empty)

Make an assignment reactive without $: if it has reactive dependencies

Summary

It would be nice to have plain/concise assignment without redundant $: for variables that have reactive dependencies.

Motivation

This is how we do now

let count = 1
$: doubled = count * 2

And this is how it can be

let count = 1
let doubled = count * 2

In case if we need one time doubled to be non reactive we could define it like

const doubled = count * 2

@brunnerh
Copy link
Member

brunnerh commented Jan 2, 2023

  • This hides the fact that the variable will change automatically, potentially causing confusion
  • It may not always be intended that a (mutable) variable updates automatically, so this would make that case impossible

@leandrohsilveira
Copy link

By doing this, Svelte will be changing a standard javascript behavior.

The migration from v3 may be kinda painful because devs will need to review all existing components to see if the reactive approach is intended, and if not, he won't be able to have a mutable non-reactive variable.

I think this change has little to add to compensate the tradeoff mentioned above.

@longnguyen2004
Copy link

No, it'll break a code pattern I've seen used in several different libraries.

<script>
    let value = ...;
    let oldValue = value;

    $: {
        // do something with value and oldValue
        oldValue = value;
    }
</script>

Your proposal breaks the assumption that oldValue won't suddenly change, which breaks any code that uses this pattern. Now, I'd love for Svelte to have an API to tap into the update and do stuff before the variable changes. However, when the API for that doesn't exist (which is now), that pattern works well enough, and your proposal will break it.

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

Successfully merging this pull request may close these issues.

4 participants