首頁  >  問答  >  主體

為什麼我不能在樣式化元件中使用元件作為重複背景圖像?

嘗試將元件設定為 div 的重複背景圖片我有一個在整個網站中重複使用的模式。

精簡的模式元件:

import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'

const Container = styled.svg`
  height: ${({ height }) => height};
`

const Path = styled.path`
  fill: ${({ color }) => color};
`

export default function Pattern({ height, color }) {
  return (
    <Container height={height} viewBox="0 0 20 20" data-cy="svg-pattern">
      <Path
        color={color}
        d="M0.5,0.4v19.2h19V0.4H0.5z M13.5,15.5L7,15.9l-3.5-5.5l3-5.9L13,4.1l3.5,5.5L13.5,15.5z"
      />
    </Container>
  )
}

Pattern.propTypes = {
  height: PropTypes.string,
  color: PropTypes.string,
}

Pattern.defaultProps = {
  height: '10px',
  color: 'blue',
}

我引入模式元件並傳遞它。

精簡元件:

import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'

// Components
import { Pattern } from '../atoms'

const Container = styled.div`
 // Removed
`

// HERE
const Repeat = styled.div`
  margin-bottom: ${({ theme }) => theme.space.normal};
  width: 100%;
  background-image: url(${({ background }) => background});
  background-repeat: repeat-x;
`

export default function SoQues(props) {
  return (
    <Container>
      <Repeat background={<Pattern color={'red'} />}/>
    </Container>
  )
}

SoQues.propTypes = {
  // props
}

但由於某種原因,它不會渲染。

研究

我做錯了什麼以及如何引入組件並將其設置為水平重複背景圖像?

P粉909476457P粉909476457375 天前465

全部回覆(1)我來回復

  • P粉715304239

    P粉7153042392023-09-13 12:50:58

    您可以將 SVG 渲染為靜態標記,將其編碼為 Base64 並將其用作 URL:

    即時測試

    import * as React from 'react';
    import { renderToStaticMarkup } from 'react-dom/server';
    
    export default function App() {
      const mySvgBase64Encoded = btoa(renderToStaticMarkup(<MySvg />));
      return (
        <div
          style={{
            width: '100%',
            height: '100vh',
            background: `url(data:image/svg+xml;base64,${mySvgBase64Encoded})`,
          }}
        />
      );
    }
    
    const MySvg = () => (
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120">
        <style>{`.st0{fill:#bcbbbb}.st1{fill:#f48023}`}</style>
        <path className="st0" d="M84.4 93.8V70.6h7.7v30.9H22.6V70.6h7.7v23.2z" />
        <path
          className="st1"
          d="M38.8 68.4l37.8 7.9 1.6-7.6-37.8-7.9-1.6 7.6zm5-18l35 16.3 3.2-7-35-16.4-3.2 7.1zm9.7-17.2l29.7 24.7 4.9-5.9-29.7-24.7-4.9 5.9zm19.2-18.3l-6.2 4.6 23 31 6.2-4.6-23-31zM38 86h38.6v-7.7H38V86z"
        />
      </svg>
    );

    使用您的程式碼,它可能類似於以下內容(我沒有測試它)

    const Repeat = styled.div`
      margin-bottom: 20px;
      width: 100%;
      background-image: ${({ background }) => `url(data:image/svg+xml;base64,${background})`};
      background-repeat: repeat-x;
    `
    
    
    export default function App() {
      const background = btoa(renderToStaticMarkup(<Pattern height="10px" color="red" />))
      return (
        <div>
          <Repeat background={background}/>
        </div>
      )
    }

    回覆
    0
  • 取消回覆