-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add reference to the parent component in component's instance. #1539
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
Comments
I've avoided adding this so far because I think that a component having knowledge of its parents is something of an anti-pattern — it makes it easy to tie yourself in knots instead of encouraging good design practices. Generally I think most things you'd use Reasonable people can disagree on that; I would be interested to hear what other people think. #1538 could be solved in other ways, e.g. explicitly passing |
I understand that, but how about having knowledge about I think
Hm, I can't imagine that. Usually, I use components in declarative way (html-like tags). I'm not creating instances of sub-components manually. Could you please explain your idea? |
I agree with @Rich-Harris on this, but I also question passing down @PaulMaly you can still pass the store to the html like tag, isn't the store just a data object anyway? you would pass it the same way you pass data to an |
What I mean is that the code Svelte generates could pass the store down — // code generated by Svelte
var nested = new Nested({
root: component.root,
store: this.store
}); In most cases I take the point about |
Ok, at least we don't need to do that manually.
Actually, my concern becomes from my understanding of Svelte's Store idea - it was very clear for me that store inheriting for the hierarchy of components. In this case, component-specific stores should pass its store down to the hierarchy or we just need to fully deprecate this way. Seems, some of your replies confirm this understanding:
But that code looks like it's design intent rather than a bug:
I think, we should make a final decision how stores should works. Should it be a single root component store or subtree-stores also available. I prefer second variant, for me it's more flexible in future, but not breaks ability to use single store. Maybe I'm wrong. Also, too little sense to have
Nope, it's not a part of |
I vote no. In Ractive, this approach led to spaghetti code. The constraints force people to code in a more reactive way, imo. |
@arxpoetica I never used it in Ractive, because Ractive too powerful to use such dumb methods. Usually, It's not necessary at all. Actually, I don't need to have
to
If @Rich-Harris knows the better way to do that, so, move on, I'm not opposite that. |
Having to keep track of instances (multiple stores) leads to incidental complexity. Having an singleton store removes this burden. I'm happy working with the constraints of a singleton store. Even deeply nested data is accessible in When you work with a singleton store, you can see all of the page's state in one object. Redux calls this the "Single Source of Truth" principle. https://redux.js.org/introduction/three-principles. Note that I'm a fan of Redux's first principle, but don't agree with 2 & 3. |
@btakita Having multiple stores per components hierarchy will not force you to use them. It still will be up to you. If you like to use redux-like single store attached to root component, you still be able to do that. |
I agree. I'd rather think of a Store as an aggregated data service instead of a Model object. That way, I don't need to worry about bookkeeping the instances since there's only one instance to be concerned about. I have also passed instantiated store instances into a component (with mixins being applied to the instantiated store). It's, at times, convenient to do so. However, in doing so, I now have to track multiple instances & the pages' state is fragmented in multiple stores. I'm in favor of treating instantiated stores as a property & adding methods to |
@btakita We already have a discussion about mixins and all that stuff in another issue. It definitely makes sense, but in my work, I usually use more than one source of data and don't like a concept of the single store from redux. It's my choice, and for me, the current implementation of Store is great in all matters except inheritance of the store. I believe if a store will be inherited from the nearest parent component in the tree with a redefined store, it will give more advantages for me. I'll have a choice - use single root store with, for example, aggregation, or use multiple-stores on different levels in the hierarchy. |
I agree with @PaulMaly in some parts:
I suppose we can just send store to children in a hidden manner instead of introducing "parent" and getting store from his. |
Mixins would be an even better candidate for making complicated svelte components for the market for a few reasons:
Inheritance hierarchies tend to be familiar, however inheritance is limited in it's applicability vs a composition-based approach. |
I think Mixins solve another problem. We need a solution for isolation tree of components includes his own store copy. I don't want to give access to the whole project for a component which installed by npm. |
@stalkerg yep, read my mind |
Closed with a related issue #1538 |
@paulocoghi in your case nothing change. |
Seems, it'll be very nice, if we'll be able to have a reference to the parent component, e.g.
this.parent
just like we already have with a root,this.root
.Also, it could helps with #1538 issue.
The text was updated successfully, but these errors were encountered: