最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add a simple SVG path to a react app - Stack Overflow

programmeradmin4浏览0评论

I'm trying to get a simple SVG graphic in my react app. But nothing is visible.

I created the graphic in Inkscape and got rid of (what I assumed was garbage ) extra code. Any help would be appreciated I'm a noob with SVGs

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.

import testSVG from '../src/ponents/test';

function App() {
    return <testSVG />;
}
import TestSVG from '../src/ponents/test';

function App() {
    return <TestSVG />;
}

Screenshot of Chrome Browser not working Screenshot of working SVG

I'm trying to get a simple SVG graphic in my react app. But nothing is visible.

I created the graphic in Inkscape and got rid of (what I assumed was garbage ) extra code. Any help would be appreciated I'm a noob with SVGs

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.

import testSVG from '../src/ponents/test';

function App() {
    return <testSVG />;
}
import TestSVG from '../src/ponents/test';

function App() {
    return <TestSVG />;
}

Screenshot of Chrome Browser not working Screenshot of working SVG

Share Improve this question edited Aug 12, 2019 at 14:52 HichiHachi asked Aug 12, 2019 at 14:31 HichiHachiHichiHachi 4913 gold badges8 silver badges20 bronze badges 1
  • Your code is working fine. What is the issue? Any error? – ravibagul91 Commented Aug 12, 2019 at 14:35
Add a ment  | 

1 Answer 1

Reset to default 3

Your rendred element is,

<testsvg></testsvg>

it means youu have added your ponent like this,

<testsvg />

When you add element using lowercase name, React thinks it is simple HTML tag and not a React ponent.

Probably the issue with your Component name.

const test = () => {

You should use PascalCase name for your ponent, so your ponent should look like,

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 ponent like this,

import Test from './Test'   //write correct filename

And finally usage,

<Test />

Demo

发布评论

评论列表(0)

  1. 暂无评论