-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I use onComplete with useQuery ? #106
Comments
I'm facing the same issue. I'm using this with AWS Amplify. Before my code looked like this: async function fetchContacts() {
try {
const contactData = await API.graphql(
graphqlOperation(queries.listContacts)
);
setContacts(
replaceNillInArrayWithEmptyString(contactData.data.listContacts.items)
);
} catch (err) {
console.log('error: ', err);
}
}
useEffect(() => {
fetchContacts();
}, []); Now, I would need . something like const { data, error, loading } = useQuery(queries.listContacts);
useEffect(() => {
if(!error && !loading) {
setContacts(
replaceNillInArrayWithEmptyString(data.listContacts.items)
);
}
}, [data, error, loading]) Is this okay? |
I just stumbled over this too. @janhesters, your useEffect looks good. The react-apollo Following the same logic: const { loading, data, error } = useQuery(query);
useEffect(() => {
const onCompleted = (data) => { /* magic */ };
const onError = (error) => { /* magic */ };
if (onCompleted || onError) {
if (onCompleted && !loading && !error) {
onCompleted(data);
} else if (onError && !loading && error) {
onError(error);
}
}
}, [loading, data, error]); If react-apollo-hooks included this I think there should either be a call to the callback to the specified in the props or a https://github.com/trojanowski/react-apollo-hooks/blob/master/src/useQuery.ts#L134-L154 |
@leoc If I add useState logic in the onCompleted function I get caught in a loop. I can get fix this by adding state to the dependency array but then I get a warning about missing dependency: state. If I put the state in the dependency I get the loop again. Is there a work around for this or can we not use useState in the onCompleted function? |
@pak11273 Could you post example code? Generally you can only use hooks (e.g. |
This is the functional react component I'm using. I'm also using react-apollo-hooks but I don't think that is affecting anything.
|
I haven't worked much with useEffect yet, but I think you want to push the useQuery inside the useEffect callback. Assuming that you want useEffect at all. -- Are you sure you need useEffect? You could instead simply load the data and render it. (Remove the useEffect entirely!) |
@fbartho I need useEffect to mimic componentDidMount. The useQuery function is basically another react hook, so it can't be used inside of a callback, it needs to be inside of a react functional component. |
@pak11273, Does the useQuery triggered twice in the snippet? I have similar implementation and observe at least double execution of the query. `const ClientRiskSummaryPanel = () => {
... |
@vmptk No. It only fires once. The code I have works, it just bugs me that I have to silence an eslint warning. |
Normally I put @pak11273 I don't understand why you would need another state ( I needed the |
@leoc Maybe I can just use the data from useQuery, I'll try that. I was putting data into useState because that data is a list of books which will be manipulated by users. I generally put data from api calls into state. I don't know if that's a good practice or not. |
Can you use |
Hi @Phavor, no you can't, since react-hooks cannot be used in the callbacks of react-hooks. I suggest you read up on using react-hooks with functional components. This helps a lot. The concept might be confusing otherwise :) |
The |
it works for me like this:
|
Probably this is already outdated, but what I did find is that you can use this is from the docs:
|
I'm also failing what's the idiomatic way to use state management with I've been told recently that using Being fluent in React, I find it frustrating that the documentation is a bit all over the place, and there's no single FAQ on good practices for the React Apollo hooks. Anyone please? |
Hi guys,
I want to do something after each polling but couldn't find how to use onCompleted:
onCompleted: (data: TData | {}) => void
A callback executed once your query successfully completes.
Any idea how to use this with useQuery ? Thanks !
The text was updated successfully, but these errors were encountered: