为什么我不能在样式化组件中使用组件作为重复背景图像?
P粉909476457
P粉909476457 2023-09-12 16:55:41
0
1
464

尝试将组件设置为 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
}

但由于某种原因,它不会渲染。

研究

  • 使用样式组件更改背景图像
  • 将背景 url 作为 prop 传递给样式组件
  • 使用样式组件中的道具设置背景图像
  • 样式组件中的背景图像发生反应
  • 在 React 17 中使用属性设置样式组件背景图像

我做错了什么以及如何引入组件并将其设置为水平重复背景图像?

P粉909476457
P粉909476457

全部回复(1)
P粉715304239

您可以将 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>
  )
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!