简体   繁体   English

如何向 React 应用程序添加简单的 SVG 路径

[英]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 />

Demo 演示版

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在此处轻松找到内容):

  • Is the element visible in the DOM eg in the html code -> React issue 元素在DOM中可见吗,例如在html代码中-> React问题
  • Does it have the correct CSS attributes (height, width, transparancy) -> CSS issue 它是否具有正确的CSS属性(高度,宽度,透明度)-> CSS问题
  • Could it be that another item is being drawn over it? 可能是正在绘制另一个项目吗? -> CSS/HTML issue -> CSS / HTML问题

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.

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