|
1 | 1 | --- |
2 | 2 | description: >- |
3 | | - Consuming a Context via the Context API is the way to start the communication |
4 | | - with the rest of the application |
| 3 | + Learn how to consume contexts in Umbraco elements using one-time references |
| 4 | + or subscriptions to access data and functionality through the Context API. |
5 | 5 | --- |
6 | 6 | # Consume a Context |
7 | 7 | There are two ways to consume a context: get a __one-time reference__ to the context, or get a __subscription__ for handling context changes. The Context API is a flexible system where contexts can get disconnected or replaced. A subscription allows for the handling of these changes. However, subscriptions use more resources. They are typically consumed in the constructor, a time when the computer is already processing a lot. Which way to go depends on your use case. |
8 | 8 |
|
9 | | -If you need a context from the moment your element loads and you hold on to the context by putting it into a variable, you should always use the subscription. Otherwise you risk holding on to a context that could be disconnected or replaced without you knowing. If you're observing properties of a context, you want to know if the context is still available. In this case, a subscription makes the best sense. |
| 9 | +A one-time reference is great for fire-and-forget events. The key here is that the context is not needed on initialization, but is only needed when a specific criteria is met. For instance, events that occur after user interaction or when a specific function is called. In that case, you just need to get a context, do something and forget about the context after that. |
10 | 10 |
|
11 | | -A one-time reference is great for fire-and-forget events in your element. Those include events that occur after user interaction. The key here is that the context is not needed when the element is initialized. It's only needed when a specific criteria is met. We just need to get a context to do something. After that, there is no reason to keep the context. |
12 | | - |
13 | | -When you're dealing with a subscription, it's good practice to consume the context in the constructor for the following reasons: |
14 | | - |
15 | | -* The constructor runs once when the element is created. This ensures your context subscription is set up before the element connects to the DOM. This guarantees you won't miss any context updates that occur during the element's initialization. |
16 | | -* Context consumers created in the constructor are automatically connected when the element enters the DOM (`connectedCallback`). They are disconnected when it's removed (`disconnectedCallback`). You don't need to manually manage this lifecycle—Umbraco's controller system handles it for you. |
17 | | -* By establishing context subscriptions in the constructor, your element's state is consistent from the moment it's created. This prevents race conditions where the element might render or perform actions before its required contexts are available. |
18 | | -* Creating context consumers in the constructor is more efficient. This is more efficient than creating them in lifecycle methods that can be called multiple times. For example, `connectedCallback` fires every time the element is added to the DOM. |
| 11 | +If you need a context during initialization and you hold on to the context by putting it into a variable, you should always use a subscription. Otherwise you risk holding on to a context that could be disconnected or replaced without you knowing. |
19 | 12 |
|
20 | 13 | ## Consuming contexts in an element |
21 | | -An [Umbraco Element](../umbraco-element/) is **any web component** that extends `UmbLitElement` or uses `UmbElementMixin` to wrap its base class. Whether you're building with Lit, vanilla JavaScript, or any other web component framework, you can make it an Umbraco Element. This gives it full access to the Context API. |
| 14 | +An [Umbraco Element](../umbraco-element/) is **any web component** that extends `UmbLitElement` or uses the `UmbElementMixin` to wrap its base class. Whether you're building with Lit, vanilla JavaScript, or any other web component framework, you can make it an Umbraco Element. This gives it full access to the Context API. |
22 | 15 |
|
23 | 16 | Umbraco Elements provide two methods for consuming contexts: |
24 | 17 |
|
@@ -132,6 +125,13 @@ customElements.define('example-element', ExampleElement); |
132 | 125 | {% endtabs %} |
133 | 126 |
|
134 | 127 | ### Get as a subscription |
| 128 | +When you're dealing with a subscription, it's good practice to consume the context in the constructor for the following reasons: |
| 129 | +
|
| 130 | +* The constructor runs once when the element is created. This ensures your context subscription is set up before the element connects to the DOM. This guarantees you won't miss any context updates that occur during the element's initialization. |
| 131 | +* Context consumers created in the constructor are automatically connected when the element enters the DOM (`connectedCallback`). They are disconnected when it's removed (`disconnectedCallback`). You don't need to manually manage this lifecycle—Umbraco's controller system handles it for you. |
| 132 | +* By establishing context subscriptions in the constructor, your element's state is consistent from the moment it's created. This prevents race conditions where the element might render or perform actions before its required contexts are available. |
| 133 | +* Creating context consumers in the constructor is more efficient. This is more efficient than creating them in lifecycle methods that can be called multiple times. For example, `connectedCallback` fires every time the element is added to the DOM. |
| 134 | +
|
135 | 135 | The first example uses Lit and that is the way Umbraco builds their elements. If you don't want to use Lit, there is also an HTML element example. Both examples don't have any TypeScript specific code. You can use them in either a JavaScript or TypeScript file. |
136 | 136 |
|
137 | 137 | {% tabs %} |
|
0 commit comments