Skip to content

Documentation / @andrew_l/context / withContext

Function: withContext()

withContext<T>(fn, detached): T

Creates a function within the injection context and returns its result. Providers/injections are only accessible within the callback function.

Type Parameters

T extends AnyFunction

Parameters

fn: T

detached: boolean = false

Returns

T

Example

ts
const main = withContext(() => {
    provide('user', { id: 1, name: 'Andrew' });
    doCoolStaff();
});

const doCoolStaff = () => {
    const user = inject('user');
    console.log(user); // { id: 1, name: 'Andrew' }
};

main();