Skip to content

Instantly share code, notes, and snippets.

@ScriptBytes
Created November 16, 2023 22:36
Show Gist options
  • Save ScriptBytes/0b3c0a0780ac6b2c461a60045e287184 to your computer and use it in GitHub Desktop.
Save ScriptBytes/0b3c0a0780ac6b2c461a60045e287184 to your computer and use it in GitHub Desktop.
A working, but ugly, way to show JSON in a web page
'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