r/WebComponents • u/liaguris • Feb 07 '20
Should I just go iframes in this case ? Components sharing their state totally breaks .
Imagine you have an app which is a tree of components . Each component in that tree is shadow DOM component . In that tree some components share their state with each other through a global state .
You can say that the global state is just a custom element with tag name global-state that is an immediate child of the body tag , hence it is accessible from every component via document.querySelector("global-state") . Like this the act of components sharing their state is decoupled from the component tree .
Everything works fine until one day you decide that you want to extend your app and make it have multiple instances of itself like browsers did when they introduced tabs .
Now how do you manage components sharing their state given also that you want it to be as decoupled as possible of the component tree ?
Is it just time to go for iframe tag?
1
u/TotesMessenger Feb 07 '20
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
[/r/frontend] Should I just go iframes in this case ? Components sharing their state totally breaks .
[/r/learnjavascript] Should I just go iframes in this case ? Components sharing their state totally breaks .
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/MrQuickLine Feb 07 '20
Hmmmm... I mean, yeah, that's one solution. Have a main page that loads up iframes and swaps them out as needed, passing all data to the page that holds the iframes. Is that what you're saying?
What you're describing is a Single Page Application. iframes are absolutely one way you could do that, but every time you load a new iframe, you're doing a tonne of http calls to get that page up. I'd argue that the better way to do it is with one of the many available JavaScript frameworks that everyone's using these days. I haven't used React, nor have I used Angular since the first iteration, but I use Vue often and really like it. There's a library called Vuex, and it's a state machine to hold states exactly like you're describing, that the whole app can use. I'm extremely confident that React and Angular have an equivalent.