---
url: https://lit-ui-router.dev/reference/lit-ui-router/core/provideContext.md
---
[lit-ui-router](../index.md) / provideContext

# Variable: provideContext

```ts
const provideContext: <T>(root, key, value) => () => void;
```

Defined in: [packages/lit-ui-router/src/context.ts:378](https://github.com/simshanith/lit-ui-router/blob/740888c2b2ba358c4dfa52a122e927df6a604b12/packages/lit-ui-router/src/context.ts#L378)

Answers `context-request`s for `key` that reach `root`, so code with no
element of its own is still handed a value.

The provider is the element-free twin of a provider element: it listens for
`context-request`, guards with [isContextRequest](isContextRequest.md), calls
`stopImmediatePropagation()` so an outer provider does not answer twice, and
delivers the value synchronously. A `subscribe: true` request gets a no-op
unsubscribe — the provider holds one value for its whole lifetime.

`root` is any `EventTarget`. Under `@lit-labs/ssr` that is
`globalThis.litServerRoot`, the bottom of the renderer's event-target stack,
which every element event on the server reaches.

Installing twice installs two listeners; the first one still wins, because it
stops immediate propagation. Uninstalling is exact — each call's returned
function removes only the listener that call added.

## Type Parameters

| Type Parameter | Description |
| ------ | ------ |
|  `T` *extends* [`UnknownContext`](../types/UnknownContext.md) | the context key's type, branded with the value it references |

## Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `root` | `EventTarget` | the event target requests travel to |
| `key` | `T` | the context key to answer for, matched by identity |
| `value` | [`ContextType`](../types/ContextType.md)<`T`> | the value to answer with, as the key's brand promises |

## Returns

a function that uninstalls this provider

() => `void`

## Example

```ts
import { provideContext, routerContext } from 'lit-ui-router/context';
import type { UIRouterLit } from 'lit-ui-router';

declare const router: UIRouterLit;
// installed on globalThis by the @lit-labs/ssr DOM shim
declare const litServerRoot: EventTarget;

const uninstall = provideContext(litServerRoot, routerContext, router);
try {
  // …render…
} finally {
  uninstall();
}
```
