Skip to content

Instantly share code, notes, and snippets.

@dabit3
Created October 29, 2018 21:45
Show Gist options
  • Save dabit3/5f4042f3b56c2ab3a2285672868a73d0 to your computer and use it in GitHub Desktop.
Save dabit3/5f4042f3b56c2ab3a2285672868a73d0 to your computer and use it in GitHub Desktop.
Querying when component renders
import React, { useEffect, useState } from 'react'
import { API, graphqlOperation } from 'aws-amplify'
const query = `
query {
listTodos {
items {
id
name
description
}
}
}
`
export default function() {
const [todos, updateTodos] = useState([])
useEffect(async() => {
try {
const todoData = await API.graphql(graphqlOperation(query))
updateTodos(todoData.data.listTodos.items)
} catch (err) {
console.log('error: ', err)
}
}, [])
return todos
}
@yann-yinn
Copy link

thanks for the example !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment