Skip to content

Instantly share code, notes, and snippets.

@jericbas
Last active November 9, 2023 09:12
Show Gist options
  • Save jericbas/4e7758d1b7949daab87b5183bbfff01f to your computer and use it in GitHub Desktop.
Save jericbas/4e7758d1b7949daab87b5183bbfff01f to your computer and use it in GitHub Desktop.
json preview component
import React, { useState } from 'react';
interface JsonDisplayProps {
data: any; // You can use a more specific type based on what JSON you expect
}
const JsonDisplay: React.FC<JsonDisplayProps> = ({ data }) => {
const [isCollapsed, setIsCollapsed] = useState(true);
const jsonString = JSON.stringify(data, null, 2);
const truncateLimit = 100; // You can adjust the truncate limit as needed
const toDisplay = isCollapsed && jsonString.length > truncateLimit
? `${jsonString.slice(0, truncateLimit)}...`
: jsonString;
return (
<div>
<pre>{toDisplay}</pre>
<button onClick={() => setIsCollapsed(!isCollapsed)}>
{isCollapsed ? 'Show More' : 'Show Less'}
</button>
</div>
);
};
export default JsonDisplay;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment