Displaying Chat Messages
Learn how to render and format chat messages from the v0 Platform API using @v0-sdk/react
Learn how to properly render chat messages from the v0 Platform API using the @v0-sdk/react headless React library. This guide covers handling different message types, implementing streaming updates, and customizing message rendering.
Overview
The @v0-sdk/react library provides headless React components and hooks for rendering rich chat messages from the v0 Platform API. It handles complex message formats including:
- Text content - Plain text and markdown with custom styling
- Code blocks - Syntax-highlighted code snippets with copy functionality
- Code projects - Multi-file code projects with collapsible sections
- Math expressions - LaTeX math rendering (inline and block)
- Thinking sections - AI reasoning process with animated indicators
- Task sections - Task execution steps with status tracking
- Streaming content - Real-time message updates with loading states
Installation
First, install the required packages:
Import the required CSS in your app:
Quick Start
Basic Message Component
The simplest way to render a message is using the Message
component:
Fetching and Parsing Messages
First, retrieve messages from a chat and parse the content:
If you need custom configuration (API key, base URL, etc.), use createClient
:
Message Binary Format
The v0 Platform API returns messages in a special binary format that encodes rich content:
Customizing Message Rendering
Using Custom Components
The Message
component accepts a components
prop to customize how different elements are rendered:
Custom Code Block Component
Create a custom code block with additional features:
Custom Math Component
Customize math rendering with additional styling:
Complete Chat Interface
Here's a complete chat interface using @v0-sdk/react:
Streaming Messages
The StreamingMessage
component handles real-time message updates from the v0 Platform API:
Headless Streaming Hook
For more control, use the headless useStreamingMessageData
hook:
Best Practices
1. Performance Optimization
Use React.memo and proper key props for message lists:
2. Error Handling
Wrap message rendering in error boundaries:
3. Accessibility
Ensure proper ARIA labels and keyboard navigation:
4. Theme Support
Create reusable theme configurations:
Utilities
Time Formatting
Message Processing
Custom Hook for Message Management
Headless Architecture
The @v0-sdk/react library follows a headless architecture pattern, giving you complete control over styling and behavior:
Using Headless Hooks
Available Hooks
useMessage
- Process static message content into renderable datauseStreamingMessageData
- Handle streaming messages with real-time updatesuseCodeBlock
- Process code block data for custom renderinguseMath
- Process LaTeX math for custom renderinguseThinkingSection
- Handle AI thinking sectionsuseTaskSection
- Process task execution data
Next Steps
- Explore the v0-sdk GitHub repository for more examples
- Check out the React Components Example for a complete implementation
- Learn about Lock Files from AI Changes to handle concurrent editing
- Try different themes in the v0-sdk-react-example directory