Answer the question
In order to leave comments, you need to log in
How to apply a graphQL object inside a component?
Need help until I catch up with what I'm doing wrong with the query object.
there is index.js
import React from "react"
import Layout from "../components/layout"
import Work from "../components/Work"
const IndexPage = ({data}) => (
<Layout>
<Work></Work>
</Layout>
)
export default IndexPage
import React from "react"
import { Link, graphql } from "gatsby"
const Work = ({query}) => {
return (
{query.allWpPost.edges.map(({ post }) => (
<div>
<Link to={post.slug}>
<p>{post.title}</p>
</Link>
<div dangerouslySetInnerHTML={{ __html: post.excerpt }} />
</div>
))}
)
}
export default Work
export const WorkQuery = graphql`
query WP_Posts {
allWpPost {
edges {
post: node {
id
title
slug
excerpt
uri
}
}
}
}
}
`
Answer the question
In order to leave comments, you need to log in
You can use Gatsby's useStaticQuery hook instead of workquery to get the following
const WorkQuery = graphql`
query WP_Posts {
allWpPost {
edges {
post: node {
id
title
slug
excerpt
uri
}
}
}
}
}
`
const Work = () => {
const query = useStaticQuery()
return (
{query.allWpPost.edges.map(({ post }) => (
<div>
<Link to={post.slug}>
<p>{post.title}</p>
</Link>
<div dangerouslySetInnerHTML={{ __html: post.excerpt }} />
</div>
))}
)
}
export default Work
- export const WorkQuery = graphql`
query WP_Posts {
allWpPost {
edges {
post: node {
id
title
slug
excerpt
uri
}
}
}
}
}
`
-
+ export const query = graphql`
query WP_Posts {
allWpPost {
edges {
post: node {
id
title
slug
excerpt
uri
}
}
}
}
}
`
It turned out to be solved with the help of a hook, as advised by vıtalıyırtlach
, but it worked when I moved the WorkQuery query to the hook parameters
const query = useStaticQuery(graphql`
query WP_Posts {
allWpPost {
edges {
post: node {
id
title
slug
excerpt
uri
}
}
}
}
}
`)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question