score:0

if you are using gatsby, which i assume is where usestaticquery comes from, then you can't. the static there in the name means you can't use a variable in them. sadly :(

you can probably read more about it here: https://spectrum.chat/gatsby-js/general/using-variables-in-a-staticquery~abee4d1d-6bc4-4202-afb2-38326d91bd05


alternativelly, you could create a custom "usequery", which is a fancy way to name a function that returns a value. ;)

for this you have:

const usecustomquery = () => {
  const data = usestaticquery(
    graphql`
      query {
          allgooglesheetdatarow(filter: { state: { eq: "alabama" } }) {
              edges {
                  node {
                      snap
                      tanf
                      medicaid
                      unemployment
                  }
              }
          }
      }
    `
  );
  return data
}

and in your "page" or any component you need data, you just need to use

const component = () => {
  const data = usecustomquery()

  // use data anyway you want
}

score:0

the problem is that you're using usestaticquery rather than a page query. according to the docs:

"variables can be added to page queries (but not static queries) through the context object that is an argument of the createpage api."

so, you don't need to import usestaticquery. simply import graphql. then, add this after your component:

export const query = graphql`
        query dataquery($state: string) {
            allgooglesheetdatarow(filter: { state: { eq: $state } }) {
                edges {
                    node {
                        snap
                        tanf
                        medicaid
                        unemployment
                    }
                }
            }
        }
    `

then pass data to your components parameters

export default function yourcomponent({data}) { 
      // access data object here.
}

i didn't include passing the variable within context in the gatsby-node.js file. however, you'll have to do that as well. there's an example of how to do this in the link i provided.


Related Query

More Query from same tag