Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Calling React hook in a function

Is there a chance I can run a query with a useQuery hook only if the button is clicked?

const handleClick = () => {
  const [, { data }]  = useLazyQuery(GET_ORDER_INVOICE, {
    variables: { order_Id: orderNumber }
  });

  return '';
};

<Button
  priority={ButtonPriority.SECONDARY}
  onClick={ () => {
    handleClick();
  } }
/>

However, this breaks with the React Hook "useLazyQuery" is called in function "handleClick" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. Is there a chance I can bind calling a query to a button click?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to call useLazyQuery in the body of your component. It will then return to you a function that you can call in handleClick:

const Example = () => {
  const [executeQuery, { data }] = useLazyQuery(GET_ORDER_INVOICE);

  const handleClick = () => {
    executeQuery({
      variables: { order_Id: orderNumber }
    })
  }

  // ...
  <Button
    priority={ButtonPriority.SECONDARY}
    onClick={handleClick}
  />
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!