Skip to content

Instantly share code, notes, and snippets.

@edisdev
Created February 21, 2021 02:37
Show Gist options
  • Save edisdev/2aa75ed4ce7ab6e44147237137a64dde to your computer and use it in GitHub Desktop.
Save edisdev/2aa75ed4ce7ab6e44147237137a64dde to your computer and use it in GitHub Desktop.
Amplify example with React
import Head from 'next/head'
import { API } from 'aws-amplify'
import styles from '../styles/Home.module.css'
import { listTodos } from '../graphql/queries'
import { useEffect, useState } from 'react'
export default function Home() {
let [todoList, setTodoList] = useState([])
async function fetchTodos () {
try {
const data = await API.graphql({
query: listTodos
})
await setTodoList(data.data.listTodos.items)
} catch (err) { console.log('error fetching', err) }
}
useEffect(() => {
fetchTodos()
}, [])
const TodoItems = () => {
return <div className={styles.grid}>
{
todoList.map(t => {
return <div
className={styles.card}
key={t.id}
>{t.name}</div>
})
}
</div>
}
return (
<div className={styles.container}>
<Head>
<title>Todo App With Next And Amplify</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>Todos</h1>
<TodoItems/>
</main>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment