使用 v3 gatsby-image 的 gatsby-image-background
gatsby-image-background using v3 gatsby-image
我正在尝试让 gatsby-background-image 与 v3 的 gatsby-plugin-image 一起工作。我遵循了文档,发现我应该使用 gbimage-bridge。
出于某种原因,它似乎不起作用。在 ide 中测试时,我的查询工作正常。我尝试以各种方式更改我的查询和常量,但似乎无法正常工作。
现在它只输出文本测试,但没有显示背景。
我的代码:
import { graphql, useStaticQuery } from "gatsby"
import { getImage } from "gatsby-plugin-image"
import { BgImage } from "gbimage-bridge"
const GbiBridged = () => {
const { backgroundImage123 } = useStaticQuery(graphql`
query {
backgroundImage123: allWpPage {
nodes {
ACFforside {
heroimg {
localFile {
childImageSharp {
gatsbyImageData(
width: 2000
quality: 50
placeholder: BLURRED
formats: [AUTO, WEBP, AVIF]
)
}
}
}
}
}
}
}
`)
const pluginImage = getImage(backgroundImage123)
return (
<BgImage image={pluginImage}>Test</BgImage>
)
}
export default GbiBridged
我认为您的代码段应如下所示:
import React from 'react'
import { graphql, useStaticQuery } from 'gatsby'
import { getImage, GatsbyImage } from "gatsby-plugin-image"
import { convertToBgImage } from "gbimage-bridge"
import BackgroundImage from 'gatsby-background-image'
const GbiBridged = () => {
const { backgroundImage123 } = useStaticQuery(
graphql`
query {
backgroundImage123: allWpPage {
nodes {
ACFforside {
heroimg {
localFile {
childImageSharp {
gatsbyImageData(
width: 2000
quality: 50
placeholder: BLURRED
formats: [AUTO, WEBP, AVIF]
)
}
}
}
}
}
}
}
`
)
const image = getImage(backgroundImage123.nodes[0].ACFforside.heroimg.localFile)
// Use like this:
const bgImage = convertToBgImage(image)
return (
<BackgroundImage
Tag="section"
// Spread bgImage into BackgroundImage:
{...bgImage}
preserveStackingContext
>
<div style={{minHeight: 1000, minWidth: 1000}}>
<GatsbyImage image={image} alt={"testimage"}/>
</div>
</BackgroundImage>
)
}
export default GbiBridged
我假设您的查询正在获取正确的节点,否则,请在 localhost:8000/___graphql
游乐场
中对其进行测试
我正在尝试让 gatsby-background-image 与 v3 的 gatsby-plugin-image 一起工作。我遵循了文档,发现我应该使用 gbimage-bridge。
出于某种原因,它似乎不起作用。在 ide 中测试时,我的查询工作正常。我尝试以各种方式更改我的查询和常量,但似乎无法正常工作。
现在它只输出文本测试,但没有显示背景。
我的代码:
import { graphql, useStaticQuery } from "gatsby"
import { getImage } from "gatsby-plugin-image"
import { BgImage } from "gbimage-bridge"
const GbiBridged = () => {
const { backgroundImage123 } = useStaticQuery(graphql`
query {
backgroundImage123: allWpPage {
nodes {
ACFforside {
heroimg {
localFile {
childImageSharp {
gatsbyImageData(
width: 2000
quality: 50
placeholder: BLURRED
formats: [AUTO, WEBP, AVIF]
)
}
}
}
}
}
}
}
`)
const pluginImage = getImage(backgroundImage123)
return (
<BgImage image={pluginImage}>Test</BgImage>
)
}
export default GbiBridged
我认为您的代码段应如下所示:
import React from 'react'
import { graphql, useStaticQuery } from 'gatsby'
import { getImage, GatsbyImage } from "gatsby-plugin-image"
import { convertToBgImage } from "gbimage-bridge"
import BackgroundImage from 'gatsby-background-image'
const GbiBridged = () => {
const { backgroundImage123 } = useStaticQuery(
graphql`
query {
backgroundImage123: allWpPage {
nodes {
ACFforside {
heroimg {
localFile {
childImageSharp {
gatsbyImageData(
width: 2000
quality: 50
placeholder: BLURRED
formats: [AUTO, WEBP, AVIF]
)
}
}
}
}
}
}
}
`
)
const image = getImage(backgroundImage123.nodes[0].ACFforside.heroimg.localFile)
// Use like this:
const bgImage = convertToBgImage(image)
return (
<BackgroundImage
Tag="section"
// Spread bgImage into BackgroundImage:
{...bgImage}
preserveStackingContext
>
<div style={{minHeight: 1000, minWidth: 1000}}>
<GatsbyImage image={image} alt={"testimage"}/>
</div>
</BackgroundImage>
)
}
export default GbiBridged
我假设您的查询正在获取正确的节点,否则,请在 localhost:8000/___graphql
游乐场