Created
October 29, 2018 21:45
Querying when component renders
This file contains hidden or 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, { 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for the example !