use-truapi
Pocket & renderer

useRenderer

Draw product-rendered bodies (chat messages, input widgets) through the host's render slot

useRenderer(tag, handler) claims one render context ("ChatMessage", "Input", …) on the client's single render slot and draws it. The host calls handler(request, send, interrupt) whenever a body of that kind is shown: request carries the context (roomId and messageId for a chat message) and the payload, send(body) pushes a RendererNode tree and may be called again to redraw in place, and interrupt(reason) ends the stream. The return value is the cleanup to run when the body leaves the screen.

Claiming a second context leaves the first alone; claiming the same one again replaces its handler. Pocket cards have their own hook, usePocketCard, which claims PocketCard for you. Host-only: standalone nothing is registered and onError receives HostUnavailableError.

Usage

import { useRenderer } from "@use-truapi/react";

function ChatBodies() {
  useRenderer("ChatMessage", (request, send) => {
    if (request.context.tag !== "ChatMessage") return;
    send({ tag: "String", value: { text: `message ${request.context.value.messageId}` } });
  });
  return null;
}
<script setup lang="ts">
import { useRenderer } from "@use-truapi/vue";

useRenderer("ChatMessage", (request, send) => {
  if (request.context.tag !== "ChatMessage") return;
  send({ tag: "String", value: { text: `message ${request.context.value.messageId}` } });
});
</script>

API reference

React

function useRenderer(
   tag, 
   handler, 
   options?): void;

Draw one kind of product-rendered body ("ChatMessage", "Input", …) through the client's single render slot. Pocket cards have their own hook (usePocketCard); this is the escape hatch for the other contexts. Registered for the component's lifetime.

Parameters

ParameterType
tag"ChatMessage" | "InputWidget" | "PocketCard"
handlerRenderHandler
options?{ enabled?: boolean; onError?: (error) => void; }
options.enabled?boolean
options.onError?(error) => void

Returns

void

Vue

function useRenderer(
   tag, 
   handler, 
   options?): void;

Draw one kind of product-rendered body ("ChatMessage", "Input", …) through the client's single render slot. Pocket cards have their own composable (usePocketCard); this is the escape hatch for the other contexts. Registered for the scope's lifetime.

Parameters

ParameterType
tagMaybeGetter<"ChatMessage" | "InputWidget" | "PocketCard">
handlerRenderHandler
options?{ enabled?: MaybeGetter<boolean>; onError?: (error) => void; }
options.enabled?MaybeGetter<boolean>
options.onError?(error) => void

Returns

void

On this page