using useStaticQuery in custom hook breaks build #38916
-
Having a similar issue as described in issue 29416 here I am going through the gatsby tutorials, and in section 4 it suggests moving the metadata query into a custom hook to avoid repetition.
import * as React from "react"
import { graphql, useStaticQuery } from "gatsby"
const useSiteMetadata = () => {
const data = useStaticQuery(graphql`
query {
site {
siteMetadata {
title
}
}
}
`)
return data.site.siteMetadata
}
export default useSiteMetadata When I do anything other than inline queries inside my component, I get the following error when building (locally and on netlify):
here is my import { useStaticQuery, graphql } from "gatsby"
export const useMetadata = () => {
const data = useStaticQuery(graphql`
query {
site {
siteMetadata {
title
}
}
}`
)
return data
} And here is the implementation in the SEO component (used the same way in the layout component) import React from 'react';
import { useMetadata } from '../api/useMetadata';
const Seo = ({title}) => {
const data = useMetadata()
return (
<title>{title} | {data.site.siteMetadata.title}</title>
)
}
export default Seo If I do inline queries and remove the If I write the hook inside each file like below, it succeeds as well , but only if the import React from 'react';
import { useStaticQuery, graphql } from 'gatsby';
const useMetadata = () => useStaticQuery(graphql`
query {
site {
siteMetadata {
title
}
}
}`
)
const Seo = ({title}) => {
const data = useMetadata()
return (
<title>{title} | {data.site.siteMetadata.title}</title>
)
}
export default Seo Why can I not get a custom hook to work? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Update: I got the build to succeed by renaming the |
Beta Was this translation helpful? Give feedback.
Update: I got the build to succeed by renaming the
src/api
folder tosrc/hooks
. Is API a protected folder name similar topages
in gatsby?