runWithContext
Runs fn with value provided for context (as a Provider does), without a render effect: the value is set for the duration of fn only, and what fn creates sees it.
Runs fn with value provided for context (as a Provider does), without a render effect: the
value is set for the duration of fn only, and what fn creates sees it. Used where a renderer
builds a subtree that must not be re-run by a Provider's effect. fn runs untracked, in a new
child owner of the current owner that is disposed with it.
Import#
ts
import { runWithContext } from "@shadow-js/core/extend";
Signature#
ts
function runWithContext<T, R>(context: Context<T>, value: T, fn: () => R): R
Parameters#
| Name | Description |
|---|---|
context |
The context to provide. |
value |
The value useContext(context) returns inside fn. |
fn |
Runs once, synchronously. |
Returns#
What fn returns.
Examples#
ts
import { createContext, useContext } from "@shadow-js/core";
import { runWithContext } from "@shadow-js/core/extend";
const Theme = createContext("light");
const theme = runWithContext(Theme, "dark", () => useContext(Theme));
console.log(theme); // "dark"