Skip to content

Instantly share code, notes, and snippets.

@PaulieScanlon
Created October 10, 2022 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulieScanlon/84938d3a6a96c588ec389203f9183da9 to your computer and use it in GitHub Desktop.
Save PaulieScanlon/84938d3a6a96c588ec389203f9183da9 to your computer and use it in GitHub Desktop.
Gatsby Example Page
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