question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

After using useLazyQuery hook, "called" parameter remains "true" when the component re-renders

See original GitHub issue

I’m trying to use useLazyQuery hook, in a function component, and execute the query on button click. So, i’m using the “called” parameter alongside “loading” to check if my “Navigate to Google” button was pressed and the query was executed successfully to continue executing my desired action (in this case navigate to www.google.com).

On the first execution everything works as expected but when i’m executing another action in the same screen that triggers a re-render, the “called” parameter from the previous action is still “true” so my previous action is executed again even though i didn’t pressed the “Navigate to Google” button again.

Here is my TestFunctionComponent. To reproduce the issue first press the “Navigate to Google” button and then press “Force re-render” button.

In my application i use the query to retrieve the link to navigate on button click.

const useForceUpdate = () => useState()[1];

const TestFunctionComponent = () => {
    
    const forceUpdate = useForceUpdate()
    const [executeQuery, { called, loading, data }] = useLazyQuery(MY_QUERY);

    if (called && !loading) {
        window.open(data .navigationLink, '_blank')
    }

    const handleOpenAnalysisFormPdf = () => {
        return executeQuery({
            variables: {
                medicalDocumentId: 6089249
            },
        });
    }

    return (
        <Fragment>
            <Button variant="outlined" onClick={handleOpenAnalysisFormPdf}>
                {"Navigate to Google"}
            </Button>
            <Button variant="outlined" onClick={forceUpdate}>
                {"Force re-render"}
            </Button>
        </Fragment>
    )
}

export default TestFunctionComponent;

Thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:19
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

21reactions
watchinharrisoncommented, Oct 25, 2019

So, i’m using the “called” parameter alongside “loading” to check if my “Navigate to Google” button was pressed and the query was executed successfully to continue executing my desired action (in this case navigate to www.google.com).

If you provide an onCompleted callback to useLazyQuery if will be executed once your query completes successfully. See docs for options.

I don’t think this fixes the underlying issue. Unless the called can be reset to false via an exposed method or setter, it’s set once a single query is made, regardless of it’s response.

My use case is to authenticate an input against a graphql endpoint, on “Submit” I invoke the query via useLazyQuery, check if the response is successful and then navigate away or show an error. It would be good to be able to set called to false until it’s next called via useLazyQuery.

4reactions
alexandra-ccommented, Sep 4, 2019

@pontusab This is not really my case, my problem is that after the first execution of the query was done successfully and this line window.open(data .navigationLink, '_blank') is executed, on any other action executed on this screen that triggers a re-render, that windows.open command is triggered again and again because called parameter from my if statement is never false.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useLazyQuery causing too many re-renders [Apollo
This works perfect, I use the useQuery hooks @client in the Messages.js component to fetch the channelid from the cache and it's working...
Read more >
Hooks - Apollo GraphQL Docs
If true , the in-progress query's associated component re-renders whenever the network status changes or a network error occurs. The default value is...
Read more >
Getting the Most Out of Apollo's useQuery | by Sreevisakh
Since it uses hooks it will re-render the component when there are ... the query by passing query name and variables using useLazyQuery...
Read more >
The React + Apollo Tutorial for 2020 (Real-World Examples)
useQuery Hook; useLazyQuery Hook; useMutation Hook; useSubscription Hook ... The client is provided to the entire component tree using React ...
Read more >
Redux Essentials, Part 8: RTK Query Advanced Patterns
If Component A calls useGetPostQuery(42) , that data will be fetched. ... Since we've already seen how to use the RTK Query hooks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found