Skip to content

Svelte 5: Global reactive states declared in a context="module" vs. a normal module do not behave the same #9924

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
AdrianGonz97 opened this issue Dec 15, 2023 · 3 comments
Milestone

Comments

@AdrianGonz97
Copy link
Member

Describe the bug

I've previously brought this up in the svelte-5-alpha chat and thought that it'd be worth raising an issue for a discussion.

Currently, as shown in this REPL, declaring global reactive state in context="module" behaves more similarly to local state that's declared in a component, rather than global state that's declared in an external module.

I find this difference in behavior to be a bit confusing as I would expect the global state declared in both, the external module and in context="module", to behave the same.

Reproduction

REPL

Logs

No response

System Info

n/a

Severity

annoyance

@dummdidumm dummdidumm assigned dummdidumm and unassigned dummdidumm Jan 9, 2024
@dummdidumm
Copy link
Member

One could argue it should be the other way around - the imported state should also throw an error if accessed like this. The reason is that shared state should be imported at the usage locations and modified there, rather than being passed around as props and then modified, which creates indirection and makes reasoning about the code more confusing.

@AdrianGonz97
Copy link
Member Author

One could argue it should be the other way around - the imported state should also throw an error if accessed like this.

I would be okay with this too, maybe even prefer it! My main concern is consistency in the context of a module. As long as shared state behaves the same when passing it as a prop, I'd be happy with either approach.


I will say though, having both throw an error when passed around as a prop (without being bound too) would be my preference as it jives better with my mental model.

I think it's easier to understand the rule: this prop must be bound if the child component mutates it

instead of: this prop must be bound if the child component mutates it, _unless_ it's shared state from an external module, but not shared state declared in context='module'.

@dummdidumm dummdidumm added this to the 5.0 milestone Jan 10, 2024
@dummdidumm
Copy link
Member

Consistently works as of #10464 , the last two don't throw an error now

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

2 participants