Kind: Function
Source: src/jsx/context.ts
Part of: Jsx
Read the current value of a context created with createContext.
Safe to call from async components after await. See createContext
for the per-runtime isolation guarantees.
useContext reads the current value from a context created with createContext. It can be called in synchronous or asynchronous components, including after an await, while keeping context values isolated per runtime.
Signature
tsfunction useContext(context: Context<T>): T
Parameters
| Name | Type |
|---|---|
context | Context<T> |
Returns: T
Diagram
mermaidgraph LR A[createContext] --> B[Context] B --> C[Context.Provider] C --> D[Component] D --> E[useContext] E --> F[Current context value]
Usage
tsximport { createContext, useContext } from 'hono/jsx'
const ThemeContext = createContext('light')
const ThemeLabel = async () => {
await Promise.resolve()
const theme = useContext(ThemeContext)
return <span>Theme: {theme}</span>
}
const App = () => (
<ThemeContext.Provider value="dark">
<ThemeLabel />
</ThemeContext.Provider>
)
AI Coding Instructions
- Create contexts with
createContextbefore reading them withuseContext. - Pass the context object returned by
createContextdirectly touseContext. - Read context inside a component that is rendered below the matching
Context.Provider. useContextmay run afterawaitin an async component.
Used by
7 references from 7 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (7)
Props—src/jsx/base.ts:27childrenToString—src/jsx/components.ts:14FormContext—src/jsx/dom/hooks/index.ts:24clearCache—src/jsx/dom/intrinsic-element/components.ts:18HasRenderToDom—src/jsx/dom/render.ts:30title—src/jsx/intrinsic-element/components.ts:121StreamingContext—src/jsx/streaming.ts:30
Was this page helpful?