Advanced
Render Markdown Messages
Learn how to render markdown messages using the useLopusChat
hook
This guide will walk you through the process of rendering markdown messages in your React application using the useLopusChat
hook. We’ll cover the setup, rendering logic, and best practices to ensure a smooth integration.
Prequisites
- Ensure you have a React application setup
- Complete the quickstart guide
- Install
react-markdown
package
Upgrade Your Message Component with ReactMarkdown
Return messages inside a ReactMarkdown
component instead of a div
:
Your Lopus Chat Component can now render markdown messages:
Lopus Chat Component
Best Practices
- Type Safety: Use type guards like
isReceivedMessage
to safely handle different message types. - Error Handling: Wrap
sendQuery
andsendSubmission
calls in try-catch blocks to manage errors effectively. - Component Handling: For messages containing components, ensure they are rendered correctly.
- Cleanup: The hook manages cleanup automatically, but ensure to remove any custom message handlers if added.
By following these steps, you can effectively render markdown messages in your React application using the useLopusChat
hook. This setup allows for a rich, interactive chat experience.