--- id: subscribe title: subscribe --- ```ts const subscribe: { (source, template): DirectiveResult; (source, selector, template): DirectiveResult; }; ``` Defined in: [packages/lit-table/src/subscribe-directive.ts:191](https://github.com/TanStack/table/blob/main/packages/lit-table/src/subscribe-directive.ts#L191) A Lit directive that subscribes to a source (Store or Atom) and efficiently updates only the wrapped template when the state or selected slice changes. ## Call Signature ```ts (source, template): DirectiveResult; ``` Subscribes to the entire source state without filtering. ### Type Parameters #### TSource `TSource` ### Parameters #### source [`SelectionSource`](../type-aliases/SelectionSource.md)\<`TSource`\> #### template `TemplateFunction`\<`TSource`\> ### Returns `DirectiveResult`\<*typeof* [`SubscribeDirective`](../classes/SubscribeDirective.md)\> ## Call Signature ```ts ( source, selector, template): DirectiveResult; ``` Subscribes to a specific slice of the source state via a selector, preventing unnecessary re-renders when other parts of the state change. ### Type Parameters #### TSource `TSource` #### TSelected `TSelected` ### Parameters #### source [`SelectionSource`](../type-aliases/SelectionSource.md)\<`TSource`\> #### selector `Selector`\<`TSource`, `TSelected`\> #### template `TemplateFunction`\<`TSelected`\> ### Returns `DirectiveResult`\<*typeof* [`SubscribeDirective`](../classes/SubscribeDirective.md)\> ## Example ```ts // Without a selector (subscribes to entire state) html`
${subscribe(myStore, (state) => html`${state.count}`)}
` * // With a selector (only updates when `count` changes) html`
${subscribe(myStore, state => state.count, (count) => html`${count}`)}
` ```