I've a react component named 'Header' in /src/components directory in gatsby defined as below:
import React, { Component } from "react"
import { Link } from 'gatsby'
import { StaticQuery, graphql } from 'gatsby'
import Img from "gatsby-image"
import "./header.css"
class Header extends Component {
constructor(props) {
super(props);
}
render() {
return(
<Img fixed={data.file.childImageSharp.fixed} />
)
}
}
export default Header
export const query = graphql`
query {
file(relativePath: { eq: "logo.png" }) {
childImageSharp {
# Specify the image processing specifications right in the query.
# Makes it trivial to update as your page's design changes.
fixed(width: 125, height: 125) {
base64
}
}
}
}
`
I tried to use StaticQuery with function component but it didn't work. So, I want to stick to the class component syntax.
Here, the query executes fine in http://localhost:8001/___graphql
How do I access query data in react class component ?
TypeError: Cannot read property 'file' of undefinederrorconsole.log(this)into your render function. I think you'll find the data prop has this info nested in it. Probablythis.props.data.fileI think.this.props.data.file...like @staypuftman pointed outStaticQueryand {...props} and it's working fine now. But got another issue- as I have functional component how do I add window scroll event listener. Previously I was using class component and window scroll was working fine in componentDidMount.