Is it possible to use @wire in classes that do not inherit from LightningElement?


 export default class A {



What I want to achieve:
I want to create a Mixin that will encapsulate logic for subscribing and unsubscribing Lightning Message Service context.

  • why this shouldn't be possible? what have you tried to implement this so far?
    – Patlatus
    Commented Sep 22, 2021 at 10:11

1 Answer 1


Is it possible to use @wire in classes that do not inherit from LightningElement?

No. You can save/deploy the file, but it won't work. Dragging it onto a page (even without a HTML file) will throw the following error:

class n{yourWiredFunction({data:e,error:t}){}} is not a valid component, or does not extends LightningElement from "lwc". You probably forgot to add the extend clause on the class declaration.

And there's no way to export a wired function to share with other components.

You can see the Lightning Web Components Engine/Compiler and specifically, see wire and base-lightning-element. Since wired modules seem tied to component creation, it makes sense it can't be used without extending LightningElement which is a part of that and allows you to leverage the lifecycle methods (ex. connectedCallback(), renderedCallback(), etc).

However, your scenario (messageContext) has specific mentions for creating a service component under the message-service:



Returns a MessageContext object.

Call this function in a service component that doesn't extend LightningElement. In a service component, you can’t use @wire(MessageContext) to create a MessageContext object

And, with more details, in a note at the bottom of Subscribe and Unsubscribe from a Message Channel

NOTE If you create a service component that isn’t a LightningElement, you can’t use @wire(MessageContext) to create the MessageContext object. Instead, import createMessageContext() and releaseMessageContext() methods from lightning/messageService. Use createMessageContext() to create the Context object and assign it to a field, like messageContext. Then, pass messageContext into the subscribe() method. The context isn’t automatically released for service components. Call releaseMessageContext(messageContext) to remove any subscriptions associated with your component’s message context

It seems, while the answer to your question is "no", a wire adapter might not be necessary for your use case and you can still create a service component that does not extend LightningElement.

  • Eh... I wanted to avoid using super in connectedCallback and disconnectedCallback in the subclass to manually run parent's methods that are responsible for subscribing and unsubscribing from a message channel. But as I can see, there is no other approach there.
    – Rechu
    Commented Sep 23, 2021 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .