How to use UseQuery in React.UseEffect? this is my simple query
const {allrecord} = useQuery(ME, {
onCompleted: ({ meTeacher}) => {
setUser(meIAM);
getRecords({
variables: {
orgId: meIAM.organization.id,
pagingArg: {},
},
}).then(({ data }) => displayRecord(data.record));
},
});
useEffect(() => {
console.log(allLocations)
}, []);
The Apollo Client useQuery hook automatically executes the corresponding query when the component renders. This makes it very similar to executing a query in useEffect with no dependencies. Like:
useEffect(() => {
const data = executeQuery()
}, [])
There's an alternative hook, useLazyQuery which can be used to execute a query in response to some event, like a button press.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With