Lit Context with only js #4068
-
Hi! i'm curiuos in how to implement lit element context only using javascript, because i read the documentation an only found a description of the implementation in typescript. Thnks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You can use the library without TypeScript. Just don't use the decorators. The docs may have types in them, but you don't need the types. |
Beta Was this translation helpful? Give feedback.
-
I don’t know whether this would be considered best practice, but here’s my approach for using Context without decorators. Providerclass ParentElement extends LitElement {
static properties = {
data: {}
};
_dataProvider = new ContextProvider(this, dataContext);
willUpdate(changeProperties) {
if(changeProperties.has("data")) {
this._dataProvider.setValue(this.data);
}
}
render() { … }
} Consumerclass ConsumerElement extends LitElement {
static properties = {
data: {}
};
_dataConsumer = new ContextConsumer(this, dataContext, value => {
this.data = value;
}, true);
render() { … }
} I will say, it’d be nice (or at least feel somewhat more idiomatically Lit-like to me) if, when a change to a subscribed context triggered an update of a consuming component, that context key got added to the |
Beta Was this translation helpful? Give feedback.
You can use the library without TypeScript. Just don't use the decorators. The docs may have types in them, but you don't need the types.