我正在使用Reaction构建一个网站,并已决定使用样式组件.我有一个组件,这是与样式组件样式,并从我的主页,我正在使用这个组件的一个实例的props .我有样式被拉到主页,但它没有拉在组件的内容.如果我只是拉入整个组件,它会很好地拉入内容,但不允许我通过props ,但如果我导入组件样式,它不会拉入组件的内容,但props 会正确通过.因此,我需要一些帮助,如何拉在内容和风格,并让props 通过.

我对Gatsby和样式组件非常陌生,所以谢谢您的帮助.

My Component Code

import React from 'react'
import styled from "styled-components"
import { useStaticQuery, graphql } from 'gatsby'

export const WhyChooseSection = styled.section`
  border-bottom: 1px solid var(--charcoal);
`

export const WhyChooseH2 = styled.h2`
  display: ${(props) => props.displayH2};
  text-align: center;
`

export const WhyChooseContent = styled.div`
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  width: 100%;
  max-width: 1160px;
  margin: 0 auto;
  padding-top: 40px;
  padding-bottom: 40px;
  column-gap: 100px;
  row-gap: 30px;

  @media(min-width: 992px) {
    justify-content: space-between;
    column-gap: 0;
  }

  .why-icon {
    width: 180px;
    height: 145px;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;

    p {
      margin-bottom: 0;
      font-size: 19px;
      font-weight: var(--font-bold);
    }
  }
`

export function WhyChooseIcons() {
  const data = useStaticQuery(graphql`
    {
      whyChooseIcons: allWp {
        nodes {
          acfOptionsOfficeSettings {
            whyChooseInteriorDetails {
              whyChooseIcons {
                whyChooseIconGraphic {
                  altText
                  id
                  sourceUrl
                }
                whyChooseIconTitle
              }
            }
          }
        }
      }
    }
  `)

  return (
    <>
    {data.whyChooseIcons.nodes.map((node, index) => (
      <WhyChooseSection key={index}>
        <WhyChooseH2>Why Choose Interior Details?</WhyChooseH2>
        <WhyChooseContent>
          {node.acfOptionsOfficeSettings.whyChooseInteriorDetails.whyChooseIcons && node.acfOptionsOfficeSettings.whyChooseInteriorDetails.whyChooseIcons.map((item) => (
            <div className='why-icon' key={item.whyChooseIconGraphic.id}>
              <img src={item.whyChooseIconGraphic.sourceUrl} alt={item.whyChooseIconGraphic.altText} />
              <p>{item.whyChooseIconTitle}</p>
            </div>
          ))}
        </WhyChooseContent>
      </WhyChooseSection>
    ))}
    </>
  )
}

export default WhyChooseIcons

My Homepage Code

import * as React from "react"
// import styled from "styled-components"
import { graphql } from 'gatsby'

import Seo from "../components/seo"
import Hero from '../components/heroComponent'
import { WhyChooseSection } from '../components/whyChooseIcons'
import { WhyChooseH2 } from '../components/whyChooseIcons'
import { WhyChooseContent } from '../components/whyChooseIcons'
import HomeProducts from '../components/homeProducts'
import HomeTestimonials from '../components/testimonials'
import HomeNewHomes from '../components/homeNewHomes'
import HomeFamilyTreatment from '../components/homeFamilyTreatment'
import HomeSolutions from '../components/homeSolutions'
import HomeMotorization from '../components/motorizationComps'
import HomeBrands from '../components/homeBrands'
import CTAOne from '../components/ctaOne'

const IndexPage = (hero) => {
  const heroData = hero.data.homeHero

  return (
    <>
      {heroData.edges.map(({ node }, index) => (
        <Hero key={index} heroTitle={node.template.homePage.heroH1} heroSubTitle={node.template.homePage.heroParagraph} heroBg={node.template.homePage.heroBackground.gatsbyImage} heroBtnOneText={node.template.homePage.heroButton1Text} heroBtnOneURL={node.template.homePage.heroButton1Url} heroBtnTwoText={node.template.homePage.heroButton2Text} heroBtnTwoURL={node.template.homePage.heroButton2Url} />
      ))}
      <WhyChooseSection>
        <WhyChooseH2 displayH2="none"></WhyChooseH2>
        <WhyChooseContent></WhyChooseContent>
      </WhyChooseSection>
      <HomeProducts />
      <HomeTestimonials />
      <HomeNewHomes />
      <HomeFamilyTreatment />
      <HomeSolutions />
      <HomeMotorization />
      <HomeBrands />
      <CTAOne />
    </>
  )
}

export default IndexPage

export const Head = () => <Seo title="Home" />

export const HeroContent = graphql`
  {
    homeHero: allWpPage(filter: {template: {templateName: {eq: "Home Template"}}}) {
      edges {
        node {
          template {
            ... on WpHomeTemplate {
              homePage {
                heroH1
                heroParagraph
                heroButton1Text
                heroButton1Url
                heroButton2Text
                heroButton2Url
                heroBackground {
                  gatsbyImage(placeholder: BLURRED, width: 1920)
                  altText
                }
              }
            }
          }
        }
      }
    }
  }
`

推荐答案

由于您只想将props 传递到<WhyChooseIcons>组件中,并且组件本身呈现其自己的内容,因此不需要在使用父组件中为其提供任何子 node .

只需更新您的<WhyChooseIcons>组件以接受一个props ,该props 将传递给它的内部子组件.让我们将其命名为displayH2,然后我们可以通过执行以下操作将其简单地传递到<WhyChooseH2>组件:<WhyChooseH2 displayH2={displayH2}>.因此,您的组件定义现在如下所示:

export function WhyChooseIcons({ displayH2 }) {
  // Rest of the component logic here

  return (
    <>
    {data.whyChooseIcons.nodes.map((node, index) => (
      <WhyChooseSection key={index}>
        <WhyChooseH2 displayH2={displayH2}>Why Choose Interior Details?</WhyChooseH2>
        {/* Other content here */}
      </WhyChooseSection>
    ))}
    </>
  )l
};

然后,在使用该组件的文件中,只需将"none"传递给props 即可:

<WhyChooseSection displayH2="none">

HOWEVER这对我来说是个XY问题,因为你想要的只是控制这个组件的条件呈现.您可以做的是在您的组件上创建一个props ,以确定是否应该呈现<WhyChosseH2>.

export const WhyChooseH2 = styled.h2`
  text-align: center;
`

export function WhyChooseIcons({ shouldHideH2 }) {

  return (
    <>
    {data.whyChooseIcons.nodes.map((node, index) => (
      <WhyChooseSection key={index}>
        {!shouldHideH2 && <WhyChooseH2>Why Choose Interior Details?</WhyChooseH2>}
        {/* Other content here */}
      </WhyChooseSection>
    ))}
    </>
  )
}

然后,您只需使用真/假布尔值控制h2渲染:

{/* To show it, just don't set the prop */}
<WhyChooseIcons />

{/* To hide it, both ways are valid */}
<WhyChooseIcons shouldHideH2 />
<WhyChooseIcons shouldHideH2={true} />

Reactjs相关问答推荐

为什么Next.js 14无法解析页面路由路径?

react 副作用循环

如何通过浏览器路由在单独的.jsx文件中使用MUI抽屉?

如何在重新图表中更改悬停时的条形填充 colored颜色 ?

如何将图像(在代码中称为自己)定位在蓝色圈内的点上?我正在使用material UI和Reaction

Next.js:提交表单时状态尚未就绪

在Reaction中单击并显示子组件延迟/动画

MUiv5和TSS-Reaction SSR问题:无法可靠地处理样式定义.CSSprops 中的ArrowFunctionExpression

如何使我的代码可以接受我想要的每一个链接

useEffect 和 ReactStrictMode

显示我是否加入聊天应用程序时出现问题

React Native 背景动画反转

React - 将一个充满空值的无限大小的数组存储在 ref 中是否可以,或者我应该重置数组吗?

Firebase Storage: 对象不存在图片上传路径错误

使用登录保护 React 中的 SPA 应用程序 - 为什么这种方法不起作用?

如何使用 redux 和 react-plotly.js

导入样式化组件时未导入组件内容

如何在 JSX 中使用 -webkit- 前缀?

使用 useRef(uuid()) 的目的是什么?

NextJs - 如何从站点 map 生成器中删除重复的 URL?