Hi, I am using useLazyQuery
in a useEffect
and I noticed a weird behavior which I am not sure if its intentional or not.
I have a schema that requires me to pass a valid string in order to be executed.
I defined my query handle like this
const [getProject,{ data: projectData, variables, called, error }] = useLazyQuery(query);
If I do the following, I get an error (which is fine):
useEffect(() => {
getProject({ variables: {
_id: ""
}})
},[]);
console.log(projectData, variables, called, error);
If I do this, I get the result of the 2nd query and the 1st one does not return an error:
This isn’t intentional, it was just for testing purposes.
useEffect(() => {
getProject({ variables: {
_id: ""
}})
getProject({ variables: {
_id: "60e3213dc0f730137ba8f1b6"
}})
},[]);
console.log(projectData, variables, called, error);
The third one, is where I get a weird result, I have the following code which receives an _id
and fetches the data, on the first render, the data is an empty string, but the 2nd render (subsequent and instant), it has a value, but data returned is always undefined. The only way I found to fix it, was to add an if condition to make sure the _id
given is not empty before executing the getProject
. But I don’t understand why it didn’t return any error to indicate why it failed at all.
useEffect(() => {
getProject({ variables: {
_id: props._id
}})
},[props._id]);
console.log(projectData, variables, called, error);
I was simply wondering why it behave like this and if it was intentional or a bug.
I hope I was being clear enough.
Thanks!