Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

useLazyQuery executed multiple times #4019

Open
squarewave24 opened this issue Jun 11, 2020 · 1 comment
Open

useLazyQuery executed multiple times #4019

squarewave24 opened this issue Jun 11, 2020 · 1 comment

Comments

@squarewave24
Copy link

squarewave24 commented Jun 11, 2020

i have a simple hook component that looks like this

export function DownloadTestData(props) {
  const [getTestData, { loading, error, data }] = useLazyQuery(QUERY_TEST);
  if (data)
     saveAsCsv(data)

  return <Button onClick={getTestData}>export</Button>

it works as expected, except, when the row that contains DownloadTestData component get's re-rendered, it re-runs above function with data still available, so the save executes multiple times (once on click, and all subsequent re-renders of that component)

am i using it incorrectly?
how come data remains populated on subsequent re-renders? i need it to trigger only once when getTestData was executed

data is also there for all useEffect hooks subsequent to onClick action.

@millievn
Copy link

the saveAsCsv function should be placed in useLazyQuery's onCompleted. Search onCompleted in https://www.apollographql.com/docs/react/data/queries/#usequery-api.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants