-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Explain difference between contexts and stores #3338
Conversation
I had a problem understanding why you would want to use a context when you can just use a store. This is what I've gathered after reading a bit about them.
I'm not sure the tutorial is the best place for a discussion about the benefits of stores vs context, it is mainly there to show how to use these APIs. I appreciate that context is a more difficult API to get to grips with and we need to do a better job of explaining it. This kind of information should definitely be somewhere, it might fit better in the API Docs although the API Docs are in danger of not being API Docs at all. Maybe they just need renaming. I am unsure about where this should live. |
You may be right, @pngwn, and I may be the only guy asking myself that question at this point. Perhaps the API docs are a better place for it, or perhaps some other tutorial page. I'm arranging a meetup at work about Svelte this coming Monday, so I may test it out on my colleagues to see if they understand the difference without my addition to the tutorial. However, I really appreciate how the Svelte tutorial compares similar concepts and quickly explains the benefits of using one vs the other. For example, the subchapter on readable stores:
I thought that the context chapter would really benefit from such an explanation. If its the length of my addition that bothers you, I'll gladly revise it! 🙂 PS: I actually gathered my understanding from an answer of yours on discord, @pngwn, so thanks for that! 👍 |
Having thought about it, I actually think that the first paragraph is a really good summary of stores vs. context, I don't really think the code example or additional tasks add much (I was a little confused about them). So, I'd be happy with this if we just kept that paragraph, it doesn't add too much length but it does clear up a question that we get a lot in a very succinct way. |
I think we can combine the 'Remember that context is not inherently reactive' bit with this. I've updated the branch |
> Remember that context is not inherently reactive. If you need context values to be reactive then you can pass a store into context, which *will* be reactive. | ||
## Contexts vs. stores | ||
|
||
Contexts and stores seem similar. They differ in that stores are available to *any* part of an app, while a context is only *available to a component and its descendants*. This can be helpful if you want to use several instances of a component without the state of one interfering with the state of the others. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds good, but it's gonna bug me that the 'available to' is not italicized the first time it appears in the sentence but it is the second time in a parallel construction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oof. mad that i missed that 😅
thanks @Vages! |
I had a problem understanding why you would want to use a context when you can just use a store. This is what I've gathered after reading a bit about them.
Before submitting the PR, please make sure you do the following
npm run lint
!)Tests
npm test
oryarn test
)