Skip to content

Conversation

@dummdidumm
Copy link
Member

closes #14300
closes #14261

@changeset-bot
Copy link

changeset-bot bot commented Nov 15, 2024

⚠️ No Changeset found

Latest commit: 3972524

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-svelte-14311-svelte.vercel.app/

this is an automated message

@github-actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@14311


> [!NOTE] When you update properties of proxies, the original object is _not_ mutated.
Since `$state` stops at boundaries that are not simple arrays or objects, the following will not trigger any reactivity:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boundaries that are not simple arrays or objects

I'm having a bit of a hard time parsing this. What does it include besides classes? Does it mean that closures don't work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does closures have to do with this? This is about what gets proxified when you pass it to $state

Copy link
Member

@benmccann benmccann Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh. I didn't read it that way at all. I thought it was saying $state that has been created can't be created across a boundary. But you're talking about the $state(...) initializer? Maybe something like this then:

Suggested change
Since `$state` stops at boundaries that are not simple arrays or objects, the following will not trigger any reactivity:
Since the `$state` initializer deeply creates reactive state only when encountering simple arrays or objects, the following will not trigger any reactivity:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this whole block should go in the next section. I'm generally a bit annoyed when docs show me what not to do as a way of teeing up what I should do — just show me the right way in the first place!

In this case, we can demonstrate the Svelte way of using classes, and then reiterate the point that's made at the top of the 'Deep state' section...

If $state is used with an array or a simple object

...adjusted to emphasise that things that aren't arrays or simple objects won't be proxified.

todo.log(); // logs "'right', true"
```

Notice how we didn't use _any_ Svelte specifics, this is just regular JavaScript semantics. `$state` and `$state.raw` (and [`$derived`]($derived)) don't change these, they just add reactivity on top, so that when you change a variable something can happen in reaction to it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to split the second part into a separate sentence to avoid it being a run-on and realized we probably don't need the second part at all

Suggested change
Notice how we didn't use _any_ Svelte specifics, this is just regular JavaScript semantics. `$state` and `$state.raw` (and [`$derived`]($derived)) don't change these, they just add reactivity on top, so that when you change a variable something can happen in reaction to it.
Notice how we didn't use _any_ Svelte specifics, this is just regular JavaScript semantics. `$state`, `$state.raw`, and [`$derived`]($derived) doesn't change this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to emphasize what the runes actually do instead.

@Rich-Harris
Copy link
Member

I'm sorry, but I really don't think this is the way to go. In this PR literally over half the page is taken up with how-to-JavaScript examples that make it sound as though wrangling state in Svelte is far more intimidating than it actually is. Far from easing people's minds, this will increase anxiety.

Looking at the referenced issues, I see a handful of tangible concerns that can be dealt with directly:

  • why isn't my class method working?
  • why isn't reactivity preserved when destructuring?
  • why isn't my reassigned context magically appearing in another component?

I've opened #14514 which tackles the first two. Not really sure how to untangle the misunderstanding that led to the third, but I don't think it's a common one.

@dummdidumm
Copy link
Member Author

dummdidumm commented Dec 3, 2024

The thing that people have the most problems with is how to preserve reactivity across contexts. Destructuring is the easier part. It's "how do I keep this alive" what needs a good explanation, which includes explaining closures via arrow functions, getters/setters, and object mutation, which I tried here, and which is completely missing in #14514. I'm ok with condensing the knowledge, but just not having it appear in the docs means we pretend this repeating question doesn't exist.

@Rich-Harris
Copy link
Member

I'm uncomfortable with us taking a lot of space in the Svelte docs to teach remedial JavaScript concepts. But I've opened #14528 which tries to explain pass-by-value as concisely as possible

@dummdidumm
Copy link
Member Author

It just shows that many people learn frameworks rather than the language, so it's unavoidable that we explain some of those concepts. I like the pair of PRs you opened so I'm gonna close this one

@dummdidumm dummdidumm closed this Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarity around using Runes in .svelte.(js|ts) files in docs Context not working with array

4 participants