You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 16/umbraco-cms/customizing/foundation/context-api/consume-a-context.md
+17-17Lines changed: 17 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,21 +4,21 @@ description: >-
4
4
with the rest of the application
5
5
---
6
6
# Consume a Context
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 disonnected or replaced. A subscription allows for the handling of these changes. However, subscriptions use more resources and 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.
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. Also if you're observing properties of a context, you want know if the context is still available and a subscription also makes the best sense.
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.
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, but only needed then a specific criteria is met. We just need to get a context to do something, but after that, there is no reason to keep the context.
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
12
13
13
When you're dealing with a subscription, it's good practice to consume the context in the constructor for the following reasons:
14
14
15
-
* The constructor runs once when the element is created, ensuring 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`) and disconnected when it's removed (`disconnectedCallback`). You don't need to manually manage this lifecycle—Umbraco's controller system handles it for you.
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
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 efficientthan creating them in lifecycle methods that can be called multiple times (like `connectedCallback`, which fires every time the element is added to the DOM).
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.
19
19
20
20
## 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 with full access to the Context API.
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.
22
22
23
23
Umbraco Elements provide two methods for consuming contexts:
24
24
@@ -28,7 +28,7 @@ Umbraco Elements provide two methods for consuming contexts:
28
28
Both methods accept a Context Token (or string alias) to identify which context to consume.
29
29
30
30
### Get as one-time reference
31
-
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 example using vanilla Javascript. Both examples don't have any TypeScript specific code, so you either use them in a JavaScript or TypeScript file.
31
+
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 example using vanilla Javascript. Both examples don't have any TypeScript specific code. You can use them in either a JavaScript or TypeScript file.
32
32
33
33
{% tabs %}
34
34
{% tab title="Lit element" %}
@@ -42,7 +42,7 @@ import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
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 a Lit-less example. Both examples don't have any TypeScript specific code, so you either use them in a JavaScript or TypeScript file.
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.
Not all code that needs contexts lives in UI elements (web components). Services, managers, repositories, and helper classes often need access to contexts like notifications, workspaces, or application state, but they don't exist as elements in the DOM.
207
+
Not all code that needs contexts lives in UI elements (web components). Services, managers, repositories, and helper classes often need access to contexts. These may include notifications, workspaces, or application state. However, they don't exist as elements in the DOM.
208
208
209
-
For these non-UI classes, extend `UmbControllerBase` to gain the same context consumption capabilities as elements. This base class provides `getContext()` and `consumeContext()` methods, allowing any class with a controller host to access the Context API.
209
+
For these non-UI classes, extend `UmbControllerBase` to gain the same context consumption capabilities as elements. This base class provides `getContext()` and `consumeContext()` methods. This allows any class with a controller host to access the Context API.
210
210
211
211
### Get as one-time reference
212
212
This example creates an example service that can show a notification in the backoffice of Umbraco based on the given text.
@@ -268,16 +268,16 @@ export class ExampleService extends UmbControllerBase {
268
268
```
269
269
270
270
## Manual context control
271
-
In rare cases, you may need complete manual control over context consumption without extending `UmbControllerBase` or using element mixins. This is typically necessary when:
271
+
In rare cases, you may need complete manual control over context consumption. This means not extending `UmbControllerBase` or using element mixins. This is typically necessary when:
272
272
273
273
- Integrating with third-party libraries or frameworks
274
274
- Working with legacy code that can't be refactored
275
275
- Building custom architectural patterns outside Umbraco's standard controller system
276
276
277
-
For these scenarios, use `UmbContextConsumer` directly. This low-level API gives you full control but requires manual lifecycle management (`hostConnected()`, `hostDisconnected()`, and `destroy()`).
277
+
For these scenarios, use `UmbContextConsumer` directly. This low-level API gives you full control but requires manual lifecycle management. You must call `hostConnected()`, `hostDisconnected()`, and `destroy()`.
278
278
279
279
{% hint style="warning" %}
280
-
**Use this approach only when necessary.** The methods shown in previous sections (`UmbLitElement`, `UmbElementMixin`, `UmbControllerBase`) handle lifecycle management automatically and suitable for most use cases.
280
+
**Use this approach only when necessary.** The methods shown in previous sections handle lifecycle management automatically. These include `UmbLitElement`, `UmbElementMixin`, and `UmbControllerBase`. They are suitable for most use cases.
281
281
{% endhint %}
282
282
283
283
### Get as one-time reference
@@ -322,7 +322,7 @@ export class NotificationService {
322
322
```
323
323
324
324
### Get as a subscription
325
-
In contrast to the one-time reference, a callback is provided. This makes it a subscription. You need to disconnect and destroy the context consumer yourself. This example creates a custom `DocumentService` that consumes the `Document Workspace Context`.
325
+
In contrast to the one-time reference, a callback is provided. This makes it a subscription. You need to disconnect and destroy the context consumer yourself. This example creates a custom `DocumentService` that consumes the Document Workspace Context.
0 commit comments