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
feat: implement fine-grained rerendering control with an async Lit directive
An async Lit directe `subscribe` is implemented to allow for fine-grained reactivity and rerendering of templates.
To support this, `@tanstack/lit-store` is integrated.
Copy file name to clipboardExpand all lines: docs/framework/lit/guide/table-state.md
+17-11Lines changed: 17 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,7 @@ There are two different questions when reading table state:
71
71
- Do you only need the current value?
72
72
- Or should the Lit host update when that value changes?
73
73
74
-
Use a direct atom or store read for the current value. Use `table.state` or `table.Subscribe` in render output when the host should reflect selected table state.
74
+
Use a direct atom or store read for the current value. Use `table.state` or `table.subscribe` in render output when the host should reflect selected table state.
These reads are current-value reads. The `TableController` handles host invalidation through its subscriptions to the table store and options store. If the UI needs to stay reactive to table state changes, use `table.state`, `table.Subscribe`, or a TanStack Store subscription.
92
+
These reads are current-value reads. The `TableController` handles host invalidation through its subscriptions to the table store and options store. If the UI needs to stay reactive to table state changes, use `table.state`, `table.subscribe`, or a TanStack Store subscription.
Use `table.Subscribe` in templates to select a slice of table state while rendering.
118
+
Use `table.subscribe` in templates to select a slice of table state while rendering.
119
119
120
120
```ts
121
-
${table.Subscribe({
122
-
selector: (state) => ({
123
-
pagination: state.pagination,
124
-
}),
125
-
children: ({ pagination }) =>html`
121
+
privatepaginationSelector= (state) => ({
122
+
pagination: state.pagination,
123
+
})
124
+
125
+
${table.subscribe(
126
+
table.store,
127
+
this.paginationSelector,
128
+
({ pagination }) =>html`
126
129
<span>Page ${pagination.pageIndex+1}</span>
127
130
`,
128
-
})}
131
+
)}
129
132
```
130
133
131
-
`table.Subscribe` can also accept a `source`, but in the current Lit adapter host invalidation is wired through the full `table.store` subscription. Treat source mode as a render-time selection convenience, not a guarantee of source-only host invalidation.
134
+
The template will only be evaluated when the selected state slice changes. The `table.subscribe` API is useful for fine-grained reactivity in templates.
135
+
It is important to provide a stable reference for the selector function to avoid unnecessary re-renders. You can define the selector as a class property or method to ensure it remains stable across renders.
This example demonstrates fine-grained state subscriptions using `SubscribeController` for optimized rendering in Lit.
4
+
5
+
**Key Features:**
6
+
7
+
-**Fine-grained subscriptions**: Each part of the table subscribes only to the state it needs
8
+
-**SubscribeController**: A new Lit ReactiveController that handles store/atom subscriptions declaratively
9
+
-**External atoms**: State management via `createAtom` from `@tanstack/store` for full control
10
+
-**Performance optimized**: Only affected UI elements re-render when their subscribed state changes
11
+
12
+
**What this shows:**
13
+
14
+
1. Global filter input that re-renders only when global filter changes
15
+
2. Table rows that re-render only when filtering/pagination state changes
16
+
3. Pagination controls that re-render only when pagination state changes
17
+
4. Row selection summary that re-renders only when selection changes
18
+
5. Full table state display for debugging
19
+
20
+
This pattern is ideal for large tables where you want to minimize unnecessary re-renders and have precise control over which components subscribe to which state slices.
0 commit comments