withQuery
is a higher-order component that is used to decouple Gatsby's static queries.
npm i gatsby-with-query
yarn add gatsby-with-query
Create the following:
- your GraphQL query using Gatsby's
graphql()
- a React hook which invokes Gatsby's
useStaticQuery()
and returns either all or a subset of your component'sprops
// App.query.tsx
import { graphql, useStaticQuery } from 'gatsby'
const query = graphql`
query GetSiteMetadata {
site {
siteMetadata {
title
description
}
}
}
`
const useGetSiteMetadataQuery = () => {
const queriedProps = useStaticQuery(query)
return queriedProps.site.siteMetadata
}
export default useGetSiteMetadataQuery
Then, import the React hook and withQuery()
into your component's file:
// App.tsx
import React, { FunctionComponent } from 'react'
import withQuery from 'gatsby-with-query'
import useGetSiteMetadataQuery from './App.query'
interface AppProps {
title: string
description: string
}
export const App: FunctionComponent<AppProps> = ({ title, description }) => (
<main>
<h1>{title}</h1>
<p>{description}</p>
</main>
)
export default withQuery<AppProps>(App, useGetSiteMetadataQuery)
Alternatively, you can have your React component, GraphQL query, and React query hook in the same file:
// App.tsx
import React, { FunctionComponent } from 'react'
import { graphql, useStaticQuery } from 'gatsby'
import withQuery from 'gatsby-with-query'
interface AppProps {
title: string
description: string
}
export const App: FunctionComponent<AppProps> = ({ title, description }) => (
<main>
<h1>{title}</h1>
<p>{description}</p>
</main>
)
const query = graphql`
query GetSiteMetadata {
site {
siteMetadata {
title
description
}
}
}
`
export default withQuery<AppProps>(App, () => {
const queriedProps = useStaticQuery(query)
return queriedProps.site.siteMetadata
})