Skip to content

provideRootContext ​

ts
provideRootContext<T>(key: ContextKey<T>, create: () => T): T

Provides a value on document.documentElement, making the page-wide case the outermost scope of the same mechanism.

Usage ​

ts
// Scoped or page-wide, resolved the same way, nearest first.
const 
channels
=
injectContextSync
(
el
,
DataChannels
) ??
provideRootContext
(
DataChannels
, () => new
Map
());

Parameters

  • key — a ContextKey.
  • create — a factory, run at most once per key.

Return value

  • T — the value, created or already there.

Why it is the same mechanism ​

Because the value sits on document.documentElement, a request from anywhere reaches it by bubbling, and a nearer provider still wins through stopPropagation. There is no separate global registry and no second lookup path.

Lazy, and once ​

create runs at most once per key, and nothing is created at import time. A page that never asks for a key never builds its value.

It cannot be disposed ​

A root provider outlives the instance that asked for it first, because it is page state. That is the point: the first component to need a page-wide channel should not own its lifetime.

This is what replaces withGroup, which is not ported. For a scoped set of peers, use createGroup() with an ordinary provideContext().

MIT Licensed