Skip to content

API reference / @chanx-js/client / react / useTopic

Function: useTopic() ​

ts
function useTopic<D, Ref>(
   descriptor, 
   topic, 
   options?
): UseTopicResult<Ref>;

Defined in: packages/client/src/react/useTopic.ts:46

Join one topic for the lifetime of the component:

ts
const { lastMessage, send } = useTopic(hub, hub.topics.roomTopic.with({ room_name }));

Everything is typed from that topic: lastMessage is one of its messages and send takes its outgoing ones. A new ref (say room_name changed) rejoins; the same topic on a re-render does not.

Type Parameters ​

Type Parameter
D extends ChannelDescriptor<any, any, any, any>
Ref extends TopicRef<ChanxMessage, ChanxMessage, string>

Parameters ​

ParameterType
descriptorD
topicRef
optionsUseTopicOptions<AddressOf<D>, Ref>

Returns ​

UseTopicResult<Ref>

MIT licensed