-
We have a number of components that get re-used in different areas of our app. Each different area requires that these same components are subtly different, visually. Presently, we are manually passing some kind of 'context' down, for example: In our 'sidebar' view we might have... <higher-component
.context=${"sidebar"}
>
</higher-component> And then in the render function for that 'higher-component': render() {
return html`
<nested-component
data-context=${this.context}
</nested-component>
`
} However, is there any built in means to access the ancestor tree for components? So it would be possible to do something like (pseudo code): data-context=${component.ancesters[2]} To get say the 3rd ancestor up the tree of components that 'created' it? So given:
If I was in 'nested-component' here and I used that, it would render: data-context="sidebar" ?? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Within the component, you can do See https://lit.dev/playground/#gist=e3478be76bbaf8935d3a7b1c5846dcf9 |
Beta Was this translation helpful? Give feedback.
Within the component, you can do
this.getRootNode().host
to get the parent, orthis.getRootNode().host.getRootNode().host
to get the grand parent, and use.tagName
for to get the string tag name.See https://lit.dev/playground/#gist=e3478be76bbaf8935d3a7b1c5846dcf9