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
The context object enables passing state to child elements without needing to resort to passing attributes down through multiple elements.
6
+
7
+
## Set parent context
8
+
9
+
The context object is passed as a key on the state object. Add data to the context object and it will be available to child elements.
10
+
11
+
[Follow along by checking out the context demo from GitHub →](https://github.com/enhance-dev/context-demo)
12
+
13
+
Given this markup you can use the context object to pass state directly to a deeply nested child element. Consider the page structure in the example below:
14
+
15
+
<doc-codefilename="app/pages/index.html">
16
+
17
+
```html
18
+
<context-parent>
19
+
<my-container>
20
+
<my-container>
21
+
<my-container>
22
+
<context-heading></context-heading>
23
+
</my-container>
24
+
</my-container>
25
+
</my-container>
26
+
</context-parent>
27
+
```
28
+
29
+
</doc-code>
30
+
31
+
32
+
Add a heading key to the context object in the parent element:
33
+
34
+
<doc-codefilename="app/pages/index.html">
35
+
36
+
```javascript
37
+
exportdefaultfunctionContextParent({ html, state }) {
38
+
const { context } = state
39
+
context.heading='Heading set via context'
40
+
returnhtml`
41
+
<style>
42
+
:host {
43
+
display: block;
44
+
height: 100dvh;
45
+
padding-top: 3rem;
46
+
text-align: center;
47
+
font-family: sans-serif;
48
+
}
49
+
</style>
50
+
<slot></slot>
51
+
`
52
+
}
53
+
```
54
+
55
+
</doc-code>
56
+
57
+
Render the heading passed via context in the deeply nested child element:
Copy file name to clipboardExpand all lines: app/docs/md/elements/state/index.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,12 +38,14 @@ export default function MyElement ({ html, state }) {
38
38
39
39
</doc-code>
40
40
41
-
The state object contains two top level entries:
41
+
The state object contains four top level keys:
42
42
43
43
-`attrs`, which contains all the key value pairs of attributes passed into your custom element’s instance
44
44
-`store`, which contains the global state of your Enhance application
45
+
-`instanceID`, which is a unique ID per instance of Custom Element
46
+
-`context`, which is an Object that can be used to pass state to child elements to avoid prop drilling
45
47
46
-
These two different entries allow you to work with both basic and complex state in powerful ways without the need for complex abstractions or third party libraries.
48
+
These keys allow you to work with both basic and complex state in powerful ways without the need for complex abstractions or third party libraries.
`instanceID` is a unique identifier that is generated per Custom Element instance. This enables you to differentiate between multiple instances of the same element on a page. It can also be useful when using a string based diffing library like [Morphdom](https://github.com/patrick-steele-idem/morphdom) which keys on unique identifiers to know when to update versus replace an element.
6
+
7
+
```javascript
8
+
exportdefaultfunctionMyCard({ html, state }) {
9
+
const { attrs={}, instanceID='' } = state
10
+
const { content='', heading='' } = attrs
11
+
12
+
returnhtml`
13
+
<figureid="figure-${instanceID}">
14
+
<h2>${heading}</h2>
15
+
<p>${content}</p>
16
+
</figure>
17
+
`
18
+
}
19
+
```
20
+
21
+
<doc-calloutmark="ℹ️">
22
+
Pro tip: As in the example above, prefix the id with the element name in order to use the id with multiple child elements.
0 commit comments