Created
October 10, 2022 08:08
-
-
Save PaulieScanlon/84938d3a6a96c588ec389203f9183da9 to your computer and use it in GitHub Desktop.
Gatsby Example 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
import React, { useState } from 'react'; | |
const Page = () => { | |
const [response, setResponse] = useState(null); | |
const handlePost = async () => { | |
try { | |
const response = await fetch('/api/some-endpoint?name=Paul'); | |
if (!response.ok) { | |
throw new Error(response.statusText); | |
} | |
const data = await response.json(); | |
setResponse(data); | |
} catch (error) { | |
setResponse(error.message); | |
} | |
}; | |
return ( | |
<div> | |
<button onClick={handlePost}>Post</button> | |
<pre>{JSON.stringify(response, null, 2)}</pre> | |
</div> | |
); | |
}; | |
export default Page; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment