Replies: 4 comments
-
I'm not sure what you mean by "supplies a pointer to the root element in the constructor of the context". Updating the provided value can be done by setting the So maybe something like.. @customElement('my-provider)
class MyProvider extends LitElement {
@provide({context})
data;
// or with a controller
provider = new ContextProvider({context});
socket: WebSocket;
connectedCallback() {
this.socket = new WebSocket('...');
this.socket.addEventListener('message', (event) => {
this.data = event.data;
// or with a controller
this.provider.setValue(event.data);
});
}
disconnectedCallback() {
this.socket.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
Thank you, But now I run into a new problem: consumers never get the updates from I assume the provider and consumers are "linked" via the context variable, declared in a shared file that and imported by both (following the documentation):
This works with decorators (but then I do not know what to call Is there a working example, to either
Many thanks! |
Beta Was this translation helpful? Give feedback.
-
For reference: Found this: #3685. Way over my head, but if I understand this correctly, it's difficult use setValue in lit, presently. |
Beta Was this translation helpful? Give feedback.
-
Got it working:
|
Beta Was this translation helpful? Give feedback.
-
My app receives state updates from a websocket. I am trying to use lit context to make this state available to custom components.
Question: how do I update the context provider (declared in the root element) with new data?
Presently I am using an ugly workaround which supplies a pointer to the root element in the constructor of the context which the latter calls whenever updates are received. Is there a "cleaner" way to do this?
Beta Was this translation helpful? Give feedback.
All reactions