Skip to content

Instantly share code, notes, and snippets.

@dmateos
Created February 18, 2024 08:02
Show Gist options
  • Select an option

  • Save dmateos/f11dcc86652a6086bdc54e03faecc51b to your computer and use it in GitHub Desktop.

Select an option

Save dmateos/f11dcc86652a6086bdc54e03faecc51b to your computer and use it in GitHub Desktop.
import React from 'react';
import logo from './logo.svg';
import './App.css';
import { useState, useEffect } from 'react';
import { Amplify } from 'aws-amplify';
import { generateClient } from 'aws-amplify/api';
import { listPersons } from './graphql/queries';
import { Person } from "./API";
Amplify.configure({
API: {
GraphQL: {
endpoint: 'xxx',
region: 'ap-southeeast-2',
defaultAuthMode: 'apiKey',
apiKey: 'xxx',
}
}
});
function PersonRenderer() {
const [persons, setPersons] = useState<(Person | null)[]>([]);
const client = generateClient();
useEffect(() => {
fetchPersons();
}, []);
async function fetchPersons() {
try {
const data = await client.graphql({ query: listPersons });
setPersons(data.data.listPersons!);
} catch (error) {
console.error(error);
}
}
return (
<div>
{
persons.map((person) => (
<p key={person?.PersonID}>
<div> {person?.FirstName} </div>
<div> {person?.LastName} </div>
<div> {person?.Address} </div>
</p>
))
}
</div>
);
}
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
</header>
<div>
<PersonRenderer />
</div>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment