[英]How to add a simple SVG path to a react app
I'm trying to get a simple SVG graphic in my react app.我正在尝试在我的 React 应用程序中获取一个简单的 SVG 图形。 But nothing is visible.
但什么都看不见。
I created the graphic in Inkscape and got rid of (what I assumed was garbage ) extra code.我在 Inkscape 中创建了图形并删除了(我认为是垃圾)额外的代码。 Any help would be appreciated I'm a noob with SVGs
任何帮助将不胜感激我是 SVG 的菜鸟
const test = () => {
return (
<svg viewBox="0 0 210 297">
<path
className="polymorph"
d="m 0.22287672,94.082764 c 0,0 17.63282128,-34.831303 110.42670328,-36.348656 63.66595,-1.041057 101.55017,-59.333835 101.55017,-59.333835 L 211.66521,298.51041 H 0.75741577 Z"
/>
</svg>
);
};
I got it working.我让它工作了。 Apparently my problem was importing in camelCase instead of PascalCase.
显然我的问题是在驼峰式而不是 PascalCase 中导入。
import testSVG from '../src/components/test';
function App() {
return <testSVG />;
}
import TestSVG from '../src/components/test';
function App() {
return <TestSVG />;
}
Screenshot of Chrome Browser not working Screenshot of working SVG Chrome 浏览器不工作的屏幕截图 SVG工作的屏幕截图
Your rendred element is, 您要表达的要素是,
<testsvg></testsvg>
it means youu have added your component like this, 这意味着您已经像这样添加了组件,
<testsvg />
When you add element using lowercase
name, React
thinks it is simple HTML
tag and not a React
component. 当您使用
lowercase
名称添加元素时, React
认为这是简单的HTML
标记,而不是React
组件。
Probably the issue with your Component name. 您的组件名称可能有问题。
const test = () => {
You should use PascalCase
name for your component, so your component should look like, 您应该为组件使用
PascalCase
名称,因此您的组件应如下所示:
const Test = () => { //Notice PacsalCase name
return (
<svg viewBox="0 0 210 297">
<path
className="polymorph"
d="m 0.22287672,94.082764 c 0,0 17.63282128,-34.831303 110.42670328,-36.348656 63.66595,-1.041057 101.55017,-59.333835 101.55017,-59.333835 L 211.66521,298.51041 H 0.75741577 Z"
/>
</svg>
);
};
export default Test
Then you should import your component like this, 然后,您应该像这样导入您的组件,
import Test from './Test' //write correct filename
And finally usage, 最后是用法
<Test />
Using your SVG code, I can verify the SVG works. 使用您的SVG代码,我可以验证SVG的工作原理。 Could it be related to the surrounding influences?
可能与周围的影响有关吗? Perhaps the parent HTML element has a fixed height / width and cannot show it?
父HTML元素可能具有固定的高度/宽度,无法显示吗?
You can investigate this for yourself by inspecting the DOM (Document Object Model) and going of the following checklist (You can use ctrl+f to find things easily here): 您可以通过检查DOM(文档对象模型)并检查以下清单来自己进行调查(您可以使用ctrl + f在此处轻松找到内容):
If you're not sure about these things, post some more relevant code so we can help out more. 如果您不确定这些事情,请发布一些更相关的代码,以便我们提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.