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

145
Views
Gatsby useStatciQuery react hook errors

I'm a newer developer and just diving right into gatsby. I have a working db and the graphql endpoint works fine in the sandbox page, but when I moved the query into a hook that is called by a component, I'm running into an error

React Hook "useStaticQuery" is called in function "getCoinsByBuzz" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use" react-hooks/rules-of-hooks

The code is very similar to the examples on Gatsby's docs.


import { graphql, useStaticQuery } from "gatsby";

export const getCoinsByBuzz = () => {
    const { dataset } = useStaticQuery(graphql`
    query coinsByBuzz {
        coins {
            coinByBuzzChange {
            coinName
            timestamp
            symbol
            value
            buzzScore
            buzzScoreDelta6
            timestampDelta6  
            }
        }
    }`);
  
    return dataset.coinsByBuzz;
};

import * as React from "react"
// import { Line } from "react-chartjs-2";
import {
    Chart as ChartJS,
    CategoryScale,
    LinearScale,
    PointElement,
    LineElement,
    Title,
    Tooltip,
    Legend,
} from "chart.js";
import { getCoinsByBuzz } from "../hooks/getCoinsByBuzz"


ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend);

export const LineChart = () => {
    const { dataset } = getCoinsByBuzz();
    console.log(dataset);
    return (
        <div style={{ width: '750px' }}>
          <p>{dataset}</p>
        </div> 
    );
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The code is very similar to the examples on Gatsby's docs.

I don't know anything specific about the docs, but from what I can see it seems like your getCoinsByBuzz function is intended to be used as a custom React hook. React hook names (must) start with a "use-" prefix.

Custom Hooks

Do I have to name my custom Hooks starting with “use”? Please do. This convention is very important. Without it, we wouldn’t be able to automatically check for violations of rules of Hooks because we couldn’t tell if a certain function contains calls to Hooks inside of it.

There's nothing intrinsically special about React hooks, they are plain Javascript functions with some special usage rules applied against them.

Suggested update:

import { graphql, useStaticQuery } from "gatsby";

export const useGetCoinsByBuzz = () => {
  const { dataset } = useStaticQuery(graphql`
    query coinsByBuzz {
      coins {
        coinByBuzzChange {
          coinName
          timestamp
          symbol
          value
          buzzScore
          buzzScoreDelta6
          timestampDelta6  
        }
      }
    }`
  );

  return dataset.coinsByBuzz;
};

...

import { useGetCoinsByBuzz } from "../hooks/getCoinsByBuzz";

export const LineChart = () => {
  const { dataset } = useGetCoinsByBuzz();
  
  return (
    <div style={{ width: '750px' }}>
      <p>{dataset}</p>
    </div> 
  );
};
about 4 years ago · Juan Pablo Isaza Report

0

Turns out I had issues because apparently rules are different for hooks in typescript and I'm using regular javascript.

Regardless I decided this component needs to be dynamic so I'm going to use axios to fetch data on page render

about 4 years ago · Juan Pablo Isaza 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!