Created
October 29, 2018 21:45
-
-
Save dabit3/5f4042f3b56c2ab3a2285672868a73d0 to your computer and use it in GitHub Desktop.
Querying when component renders
This file contains 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 !