Created
November 16, 2023 22:36
-
-
Save ScriptBytes/0b3c0a0780ac6b2c461a60045e287184 to your computer and use it in GitHub Desktop.
A working, but ugly, way to show JSON in a web page
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
'use client'; | |
import React, { useEffect, useState } from 'react'; | |
function JsonTester() { | |
const [json, setJson] = useState(''); | |
useEffect(() => { | |
const getJson = async () => { | |
var response = await fetch('https://swapi.dev/api/people/1'); | |
if (response.ok) { | |
setJson(JSON.stringify(await response.json())); | |
} | |
}; | |
getJson(); | |
}, []); | |
return ( | |
<div> | |
{json.length > 0 && ( | |
<code> | |
<pre>{JSON.stringify(json)}</pre> | |
</code> | |
)} | |
</div> | |
); | |
} | |
export default JsonTester; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment