ThreadContext
ThreadContext holds the thread a component renders in. Thread provides it through ThreadProvider, and useThreadContext() reads it. Components that work in both a channel and a thread, such as MessageList and MessageComposer, use it to find the thread's message list and composer.
Best practices
- Read it with
useThreadContext(). It returnsundefinedoutside a thread instead of throwing, so one component can render in a channel and in a thread. - Let
Threadprovide it.Threadalso loads the thread, checks that replies are on and gives the thread its own audio playback.ThreadProviderdoes none of these. - Read the thread's state with
useStateStore(thread?.state, selector). The context value is the thread itself, and it changes only when the thread does. - Inside a thread, read the channel with
useChannel()and the composer withuseMessageComposerController(). Both return the thread's. - In a
ThreadListrow, useuseThreadListItemContext(). Rows aren't inside aThread, souseThreadContext()returnsundefinedthere.
Value
| Value | Type | Description |
|---|---|---|
| context value | Thread | undefined |
The Thread instance from stream-chat, or undefined outside a thread. It isn't wrapped in an object. |
ThreadContextValue is the type of the value. ThreadContext itself is exported too, so useContext(ThreadContext) returns the same as useThreadContext().
ThreadProvider
ThreadProvider sets the thread for its children. Render it yourself to give a thread to components outside its Thread. This toolbar shows the header of the open thread in the app's top bar:
import type { Thread as StreamThread } from "stream-chat";
import { ThreadHeader, ThreadProvider } from "stream-chat-react";
export const ThreadToolbar = ({ thread }: { thread: StreamThread }) => (
<ThreadProvider thread={thread}>
<ThreadHeader />
</ThreadProvider>
);ThreadHeader reads the thread for its reply count and its close button, and its channel through useChannel().
| Prop | Type | Default | Description |
|---|---|---|---|
thread |
Thread |
none | The thread to provide. Without it, the context is undefined. |
children |
ReactNode |
none | The components that read the thread. |
Related hooks
| Hook | Returns |
|---|---|
useThreadContext() |
the thread in context, or undefined |
useThreadListItemContext() |
the thread of a ThreadList row, a separate context |
See useThreadContext. The fields of thread.state are on Thread and thread manager.