1 回答
TA貢獻1815條經驗 獲得超13個贊
我會將您的組件結構更改為:
import React, { useState } from 'react'
import { graphql } from 'gatsby'
?const YourPage = ({data}) => {
? ?console.log('data is', data)
? ?const [filters, setFilters] = useState({
? ? type: "",
? ? category: ""
? });
//your calculations
? return (
? ? <div>
? ? ? Your stuff?
? ? </div>
? )
}
export const query = graphql`
? query yourQueryName{
? ? ? allStrapiHomes {
? ? ? ? nodes {
? ? ? ? ? type
? ? ? ? ? category
? ? ? ? }
? ? ? }
? }
`
export default YourPage
在您的代碼中,在進行一些關鍵導入時,您丟失了 Gatsby 的一些內容。如果您使用 a?staticQuery,則需要為其添加渲染模式。可能看起來有點老套,最好使用useStaticQueryGatsby 提供的鉤子或者添加頁面查詢(我的方法)。
我添加了頁面查詢。您的數據位于props.data.allStrapiHomes.nodes,解構道具您省略了第一步,因此您的數據將位于data.allStrapiHomes.nodes。如果在 Strapi 后端中這樣設置,則和type都將是一個數組。category
添加回答
舉報
