Inter-Component Communication
January 2025
Work In Progress
This is a draft post. Any feedback or ideas for improvement are welcome.
Introduction
This page presents a method to allow multiple instances of a web component to communicate with each other.
Code Example
Here's a full example of a
component.js
file designed
to allow instances of web components
to communicate with each other.
(It's also what powers the examples
on this page.)
static
static
static
}
customElements.
The script is included on the page via:
Example
Four instances of the component are included here. Clicking one of them increments a global counter and displays the number. All the other instances of the component are set to display a dash.
Code
Result
Details
The Static Variables And Methods
This approach works by creating two static variables that act as a global counter and list of associated elements.
}
= 0
= {
Three static methods provide the core functionality.
The registerInstance()
method receives an instance
of the component and adds it to the instances
object using its UUID as the key.
static
The increment
method increments a counter
then searches through the instances
object to find one with a matching UUID of the
instance it received. An update
method
is called on each instance with either a dash or
the count if the UUID matches.
static
Finally, the removeInstance()
method
provides the way to remove instances from
the instances
object.
static
The Instance Methods
The constructor()
method preps new
instances by generating a UUID, and attaching
the shadowRoot.
The connectedCallback()
method is called
when new instances are added to the page. It
registers the component by calling the
static registerInstance()
method
through this.constructor
. Next, it
calls the instances addContent()
and addEventListeners()
methods
to set up the element.
The addContent()
method is responsible
for populating the shadowRoot's HTML.
The addEventListeners()
method
attaches the instance's handleClick()
method to the HTML button. It's set up to use
call
so the instance itself can
be accessed in the static method to get its UUID.
The handleClick()
method sends
the instances UUID to the static increment()
method through this.constructor
.
The update
method receives a new
value and updates the button text to display it
Finally, the disconnectedCallback()
method
fires when an instance is removed from the DOM. It calls
the static removeInstance
method via
this.constructor
.
Conclusion
When I first started looking into this I thought
coordinating between instances of a component
would require adding a singleton or other
object into the mix. Using the static
abilities of a class turns out to be enough.