简体   繁体   English

尝试根据屏幕宽度有条件地在包装器内部或外部呈现样式化组件时的最佳实践

[英]Best Practice when trying to conditionally render a styled component inside or outside of wrapper based on screen width

I have this code.我有这个代码。 I am trying to figure how best to have the code conditionally put inside our outside of the wrapper.我试图弄清楚如何最好地将代码有条件地放入包装器的外部。 But I am not sure what is the best practice for this.但我不确定最好的做法是什么。

在此处输入图像描述

Basically, if the screen size is lets say 600 and below, that highlighted Styled.OverlayCTA should be rendered inside the wrapper rather than outside.基本上,如果屏幕尺寸为 600 及以下,突出显示的Styled.OverlayCTA应该在包装器内部而不是外部呈现。 And vice versa.反之亦然。

Here is my code:这是我的代码:

<Styled.OverlayWrapper>
                {el.headline && (
                  <Styled.HeroHeadline as="h4">
                    {parse(el.headline)}
                  </Styled.HeroHeadline>
                )}
              </Styled.OverlayWrapper>
              <Styled.OverlayCTA
                ctaPosition={determineCtaPosition(el.heroCtaPosition)}
              >
                <CTAButton
                  colorStyle={el.ctaColor === 'green' ? 'green' : 'black'}
                  href={el?.cta?.link}
                >
                  {el?.cta?.text}
                </CTAButton>
              </Styled.OverlayCTA>

I know of the method where we can do:我知道我们可以做的方法:

<Styled.OverlayWrapper>
                    {el.headline && (
                      <Styled.HeroHeadline as="h4">
                        {parse(el.headline)}
                      </Styled.HeroHeadline>
                    )}
{screenWidth <= 600 && <Styled.OverlayCTA
                        ctaPosition={determineCtaPosition(el.heroCtaPosition)}
                      >
                        <CTAButton
                          colorStyle={el.ctaColor === 'green' ? 'green' : 'black'}
                          href={el?.cta?.link}
                        >
                          {el?.cta?.text}
                        </CTAButton>
                      </Styled.OverlayCTA>

 }
                      </Styled.OverlayWrapper>
    { screenWidth >= 601 && <Styled.OverlayCTA
                            ctaPosition={determineCtaPosition(el.heroCtaPosition)}
                          >
                            <CTAButton
                              colorStyle={el.ctaColor === 'green' ? 'green' : 'black'}
                              href={el?.cta?.link}
                            >
                              {el?.cta?.text}
                            </CTAButton>
                          </Styled.OverlayCTA>
    
     }

But that seems a little undry.但这似乎有点不干。 Is there a better practice than this?有比这更好的做法吗? Thanks!谢谢!

But that seems但这似乎

In render, store the JSX of the thing you need to move around in a variable and then use that in the same way you did in the last method you mentioned.在渲染中,将你需要移动的东西的 JSX 存储在一个变量中,然后以与你提到的最后一个方法中相同的方式使用它。

const overlay = (
  <CTAButton
    colorStyle={el.ctaColor === "green" ? "green" : "black"}
    href={el?.cta?.link}
  >
    {el?.cta?.text}
  </CTAButton>
);

// ...

return (
  <>
    <Styled.OverlayWrapper>
      {el.headline && (
        <Styled.HeroHeadline as="h4">{parse(el.headline)}</Styled.HeroHeadline>
      )}
      {screenWidth <= 600 && overlay}
    </Styled.OverlayWrapper>
    {screenWidth >= 601 && overlay}
  </>
);


Though I should add, usually with this sort of thing, it can be solved with CSS breakpoints without DOM changes needed -- which is preferable.虽然我应该补充一点,通常对于这种事情,它可以通过 CSS 断点来解决,而无需更改 DOM——这是更可取的。 But that'd be an entirely different question.但那将是一个完全不同的问题。

Notably it can't always be solved that way depending on the circumstances of the desired layout.值得注意的是,根据所需布局的情况,它不能总是以这种方式解决。 The above could be fine in your use case.以上在您的用例中可能没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM