首页 文章

以下Gatsby教程卡在了意想不到的位置上

提问于
浏览
0

我在盖茨比应用程序中收到此错误 . 我的代码似乎与我所遵循的教程相同,我似乎无法弄清楚出了什么问题 . 错误是 50:10 error Unexpected use of 'location' no-restricted-globals ,代码如下 . 如有必要,我可以添加更多代码 . 提前致谢 .

还有其他页面,可能正在导入的内容导致冲突 . 我正在使用节点版本 v11.0.0 好的再次感谢 .

src/components/layout.js

import React from 'react'
import PropTypes from 'prop-types'
import Img from 'gatsby-image'
import Helmet from 'react-helmet'
import styled from 'styled-components'
import { StaticQuery, graphql } from 'gatsby'

import Header from './header'
import Archive from './archive'
import './layout.css'

const MainLayout = styled.main`
  max-width: 90%;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 6fr 1fr;
  grid-gap: 40px;
`

const Layout = ({ children }) => (
  <StaticQuery
    query={graphql`
      query SiteTitleQuery {
        site {
          siteMetadata {
            title
          }
        }
        file(relativePath: { regex: "/bg/" }) {
          childImageSharp {
            fluid(maxWidth: 1000) {
              ...GatsbyImageSharpFluid
            }
          }
        }
      }
    `}
    render={data => (
      <>
        <Helmet
          title={data.site.siteMetadata.title}
          meta={[
            { name: 'description', content: 'Sample' },
            { name: 'keywords', content: 'sample, something' },
          ]}
        >
          <html lang="en" />
        </Helmet>
        <Header siteTitle={data.site.siteMetadata.title} />
        //**HERE IS WHERE THE ERROR IS HAPPENING**
        {location.pathname === '/' && (
          <Img fluid={data.file.childImageSharp.fluid} />
        )}
        <MainLayout>
          <div>{children}</div>
          <Archive />
        </MainLayout>
      </>
    )}
  />
)

Layout.propTypes = {
  children: PropTypes.node.isRequired,
}

export default Layout

1 回答

  • 1

    您需要将 location 添加到组件声明中:

    const Layout = ({ children, location }) => ( ... )
    

相关问题