[英]React svg image not rendering with use tag
I have a simple component here which takes in any svg image and stroke color and renders the styled svg accordingly.我这里有一个简单的组件,它接收任何 svg 图像和描边颜色,并相应地呈现样式化的 svg。 There are no error messages, just not rendering.
没有错误消息,只是没有呈现。 I tried replacing the SVG and use tag with a simple image, and it renders the component fine - meaning that the SVG and use tag are the issues here.
我尝试用一个简单的图像替换 SVG 和 use 标签,它可以很好地渲染组件——这意味着 SVG 和 use 标签是这里的问题。 Not sure why.
不知道为什么。 Any suggestions?
有什么建议么?
import React from 'react
import styled from 'styled-components'
import Truck from './Truck.svg'
const SVG = styled.svg`
stroke: ${props => props.stroke || '#66CC00'};
`;
const Icon = props => {
const { src } = props;
return (
<SVG xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" {...props}>
<use xlinkHref={src} />
</SVG>
);
};
const RenderIcon = () => <Icon src={Truck} stroke={red} />
For reference, this was what I was trying to duplicate: https://stackblitz.com/edit/react-6cr17y作为参考,这是我试图复制的内容: https://stackblitz.com/edit/react-6cr17y
Using the image
SVG element, I could see the svg image.使用
image
SVG 元素,我可以看到 svg 图像。
Updated code - updated code to use use
and href
.更新代码- 更新代码以使用
use
和href
。 Also updated the sandbox.还更新了沙箱。 This should apply the
stroke
property correctly.这应该正确应用
stroke
属性。
import React from "react";
import styled from "styled-components";
import { ReactComponent as Truck } from "./Settings.svg";
const SVG = styled.svg`
stroke: ${(props) => props.stroke || "#66CC00"};
`;
const Icon = ({ stroke }) => {
return (
<SVG viewBox="0 0 512 512" stroke={stroke}>
<Truck />
<use href={Truck} />
</SVG>
);
};
const RenderIcon = () => <Icon stroke={"red"} />;
export default RenderIcon;
Working example here - https://codesandbox.io/s/vibrant-clarke-73g8g?file=/src/App.js这里的工作示例 - https://codesandbox.io/s/vibrant-clarke-73g8g?file=/src/App.js
OR,要么,
import React from "react";
import styled from "styled-components";
import { ReactComponent as Truck } from "./Settings.svg";
const SVG = styled.svg`
stroke: ${(props) => props.stroke || "#66CC00"};
`;
const Icon = ({ src, stroke }) => {
return (
<SVG viewBox="0 0 512 512" stroke={stroke}>
{src()}
<use href={src} />
</SVG>
);
};
const RenderIcon = () => <Icon src={Truck} stroke={"red"} />;
export default RenderIcon;
If you are using Create React App you can do this:如果您使用的是 Create React App,您可以这样做:
import React from "react";
import { ReactComponent as Truck } from "./truck.svg";
export default function App() {
return (
<div className="App">
<MySvgComponent stroke="blue" svgToRender={() => <Truck />} />
</div>
);
}
function MySvgComponent({ strokeColor = "red", svgToRender }) {
return (
<>
{svgToRender()}
<svg>
<use href="#mytruck" fill="green" stroke={strokeColor} />
</svg>
</>
);
}
If not, you would need to install SVGR and use it as a webpack loader.如果没有,您需要安装SVGR并将其用作 webpack 加载程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.