Skip to content

API reference > @kontsedal/olas-react > createOlasContext

createOlasContext() function ​

Mint an independent context bound to a specific Api type. Use when:

  • You have two or more Olas roots in the same React tree and need to route consumers to the right one (the default useRoot<Api>() casts unchecked across them). - You want the api type baked in so call sites don't have to repeat useRoot<MyApi>().
ts
type AuthApi = { user: ReadSignal<User|null>; signIn: ... }
const { Provider, useRoot } = createOlasContext<AuthApi>('AuthRoot')

<Provider root={authRoot}><App /></Provider>

function Header() {
  const { user } = useRoot()      // user is ReadSignal<User|null>
}

Each call returns a *new* React context. The default <OlasProvider> /useRoot() remain available for single-root apps.

Signature:

typescript
export declare function createOlasContext<Api>(displayName?: string): OlasContext<Api>;

Parameters ​

Parameter

Type

Description

displayName

string

(Optional)

Returns:

OlasContext<Api>

Released under the MIT License.