It's hard to get data between components if they have a bit complex structure. Yes there's a plenty of state management libraries out there. But I wonder what if all I need to do is just to set a different value for a component that I can't reach from where I want to set it.
for example, I have a tabs component (there will be only 1).
I also added a button that sets a different tab but I can't reach my-tabs
from it.
so can't I just
class MyTabs extends LitElement {
static properties = {
active: {}
}
constructor() {
super()
this.active = 0
window.tabs = this
}
}
class MyButton extends LitElement {
render(){
return html`
<button @click=${e => window.tabs.active = 2}>back to tab 2</button>
`
}
}
It has a rule tho - 1 instance at most && you can't get. only to set cuz:
- when used get:
you get the actual value but when an update occurs for
my-tabs
it won't updatemy-button
leaving old data in display. - when used set:
the new value will be considered in
my-tabs
and rendered accordingly. nothing affected.
now I think about array of components being stored in a global array with onconnect/disconnected callbacks)
I know it's a big no-no for reusability but I build my project using lit and never plan to publish the components. most of them are already very specific for the project. so yeah, I know it's bad for reusability but what if I don't care about it. can it give some other side effects? or is it simply ok to? what about the array one? thanks in advance <3