Last active
November 9, 2023 09:12
-
-
Save jericbas/4e7758d1b7949daab87b5183bbfff01f to your computer and use it in GitHub Desktop.
json preview component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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