e30e75b5d4
Changesets / Create Version PR (push) Has been cancelled
Deploy Shadcn Registry / Deploy Production (push) Has been cancelled
Template Metrics / LOC + Bundle Size (push) Has been cancelled
Code Quality / Oxlint + Oxfmt (push) Has been cancelled
Code Quality / Template Sync (push) Has been cancelled
Code Quality / Build Changed Packages (push) Has been cancelled
Code Quality / Test Changed Packages (push) Has been cancelled
Deploy Expo Example / Deploy Production (push) Has been cancelled
Deploy Ink Example / Deploy Production (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-stream, 3.12) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.10) (push) Has been cancelled
Python Tests / pytest (assistant-ui-sync-server-api, 3.12) (push) Has been cancelled
56 lines
1.2 KiB
Plaintext
56 lines
1.2 KiB
Plaintext
---
|
|
title: Context
|
|
description: Pass values through resource boundaries without prop drilling.
|
|
---
|
|
|
|
<Callout type="info">
|
|
Tap context is supported as of `@assistant-ui/tap` 0.9 and is no longer deprecated.
|
|
</Callout>
|
|
|
|
Context lets you pass values through the resource tree without threading props through every level, the same idea as React's `createContext` / `useContext`.
|
|
|
|
## createContext
|
|
|
|
Create a context with a default value.
|
|
|
|
```ts
|
|
import { createContext } from "react";
|
|
|
|
const ThemeContext = createContext("light");
|
|
```
|
|
|
|
## Reading context
|
|
|
|
Read the current value of a context inside a resource with `use` imported from `"react"`.
|
|
|
|
```ts
|
|
import { use } from "react";
|
|
|
|
const useButton = () => {
|
|
const theme = use(ThemeContext);
|
|
// ...
|
|
};
|
|
|
|
const Button = resource(useButton);
|
|
```
|
|
|
|
## useContextProvider
|
|
|
|
Provide a context value to all resources rendered within the callback.
|
|
|
|
```ts
|
|
import { useContextProvider, useResource } from "@assistant-ui/tap";
|
|
|
|
const useApp = () => {
|
|
const child = useContextProvider(ThemeContext, "dark", () => {
|
|
return useResource(Button());
|
|
});
|
|
|
|
return child;
|
|
};
|
|
|
|
const App = resource(useApp);
|
|
```
|
|
|
|
Any resource rendered inside the callback (including deeply nested children) reads `"dark"` from `ThemeContext`.
|