简体   繁体   English

与多个反应组件一起使用时,svg 无法正确呈现

[英]svg not rendering correctly when used with by multiple react components

Context上下文

I have an SVG that is used by two different React components (A and B) on the same page.我有一个 SVG,它在同一页面上被两个不同的 React 组件(A 和 B)使用。

Problem问题

When component A is assigned 'display: none' css property, the svg in component B does not render correctly.当组件 A 被分配 'display: none' css 属性时,组件 B 中的 svg 无法正确呈现。

Example示例

componentA {
 display: none;
}
componentB {
 display: block;
}

SVG does not render correctly SVG 无法正确呈现

componentA {
 display: block;
}
componentB {
 display: block;
}

SVG renders correctly SVG 正确呈现

I suspect it may be an issue with my svg but I am not sure as I am new to react.我怀疑这可能是我的 svg 的问题,但我不确定,因为我是新手。 Below is the svg code.下面是svg代码。

<svg
  id={id}
  data-name={id}
  xmlns="http://www.w3.org/2000/svg"
  width={width}
  height={height}
  viewBox="0 0 498 305.84">
  <defs>
    <clipPath id="clip-path">
      <path
        fill="none"
        d="M354.47 137.84c1.75-22.18-9.65-52.24-34-73.15-15.94 51.41 30.59 56.14 34 73.15" />
    </clipPath>
    <linearGradient
      id="linear-gradient"
      x1="-146.72"
      y1="416.08"
      x2="-143.8"
      y2="416.08"
      gradientTransform="matrix(7.8 0 0 -7.8 1469.73 3349.9)"
      gradientUnits="userSpaceOnUse">
      <stop offset="0%" stopColor="#62bb46" />
      <stop offset="100%" stopColor="#a2d28a" />
    </linearGradient>
  </defs>
  <g id="Leaf">
    <path
      clipPath="url(#clip-path)"
      fill="url(#linear-gradient)"
      d="M286.058 69.72l66.622-18.076 22.032 81.205-66.622 18.074z" />
    <path
      fill="#62bb46"
      d="M320.48 64.69c23.64 2.71 53.94 33.25 34 73.15 1.76-22.18-9.65-52.24-34-73.15" />
  </g>
</svg>

I had a simple workaround that worked for me at least.我有一个简单的解决方法,至少对我有用。

The only change I would do is append the unique id to each of the elements with an id attribute,我要做的唯一更改是将唯一 id 附加到每个具有 id 属性的元素,

    <svg
      id={id}
      data-name={id}
      ...
      <defs>
        <clipPath id={`clip-path-${id}`}>
          ...
        </clipPath>
        <linearGradient
          id={`linear-gradient-${id}`}
         ...
        </linearGradient>
      </defs>
      <g id={`Leaf-${id}`}>
        <path
          clipPath={`url(#clip-path-${id})`}
          fill={`url(#linear-gradient-${id})`}
          ...
         />
      </g>
    </svg>

By doing so, it is always assured that SVG will have unique id on different components.通过这样做,始终可以确保 SVG 在不同的组件上具有唯一的 id。

As of now there is a feature request in svgo to uniquely allocate id for each svg instance.截至目前,svgo 中有一个功能请求,可以为每个 svg 实例唯一分配 id。

Best work around could be not to use clip-path in svg最好的解决方法可能是不在 svg 中使用剪辑路径

Another way can be to use following css to hide element另一种方法是使用以下 css 来隐藏元素

 componentA { display: block; visibility: hidden; height: 0; width: 0; } componentB { display: block; }

Another work around can be to pass unique id as a porp and replacing it with id required for clip-path as answered earlier .另一种解决方法是将唯一 id 作为 porp 传递,并将其替换为之前回答的剪辑路径所需的 id 。

eg例如

 export const svg = (id) => <svg xmlns="http://www.w3.org/2000/svg" width={width} height={height} viewBox="0 0 498 305.84"> <defs> <clipPath id={`clip-path-&{id}`}> <path fill="none" d="M354.47 137.84c1.75-22.18-9.65-52.24-34-73.15-15.94 51.41 30.59 56.14 34 73.15" /> </clipPath> <linearGradient id={`linear-gradient-${id}`} x1="-146.72" y1="416.08" x2="-143.8" y2="416.08" gradientTransform="matrix(7.8 0 0 -7.8 1469.73 3349.9)" gradientUnits="userSpaceOnUse"> <stop offset="0%" stopColor="#62bb46" /> <stop offset="100%" stopColor="#a2d28a" /> </linearGradient> </defs> <g id="Leaf"> <path clipPath={`url(#clip-path-&{id})`} fill={`url(#linear-gradient-${id})`} d="M286.058 69.72l66.622-18.076 22.032 81.205-66.622 18.074z" /> <path fill="#62bb46" d="M320.48 64.69c23.64 2.71 53.94 33.25 34 73.15 1.76-22.18-9.65-52.24-34-73.15" /> </g> </svg>

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

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