简体   繁体   English

ReactJS中的SVG图标

[英]SVG icons in ReactJS

I have to move an SVG icon from a regular website to a ReactJS website. 我必须将SVG图标从常规网站移至ReactJS网站。 The SVG has to be modified a little to make it compatible with JSX, so I've removed all the ':'s and replaced them with camel case attribute names for JSX compatibility. SVG必须进行一些修改以使其与JSX兼容,因此我删除了所有的':',并用驼峰式案例属性名称替换了它们以实现JSX兼容性。 The only issue is the d attribute. 唯一的问题是d属性。

The SVG now looks like: SVG现在看起来像:

<svg id="Logo" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="100px" height="100px" viewBox="0 0 900 800" enable-background="new 0 0 1024 1024" xmlSpace="preserve">
    <g id="Layer_1"></g>
    <g id="Logo">
        <g>
            <g id="Fish">
                <g>
                    <path style={{fill:"#8DC046"}} d="M430.249,525.415c0,64.563-58.349,136.165-58.349,136.165l0.434,0.435l214.046-136.867L374.238,388.279
                        l-0.594,0.596C373.644,388.875,430.249,458.241,430.249,525.415z"></path>
                </g>
                <g>
                    <path style={{fill="#B8CD43"}} d="M586.381,525.147L374.238,388.279l-0.594,0.596c0,0,56.605,69.366,56.605,136.54L586.381,525.147z"></path>
                </g>
                <g>
                    <path style={{fill="#15AADB"}} d="M430.249,253.264c0,64.145-56.444,136.163-56.444,136.163l0.433,0.435l212.143-136.868L372.334,116.125
                        l-0.595,0.598C371.739,116.723,430.249,188.18,430.249,253.264z"></path>
                </g>
                <g>
                    <path style={{fill="#4AC5ED"}} d="M586.381,252.994L372.334,116.125l-0.595,0.598c0,0,58.51,71.457,58.51,136.541L586.381,252.994z"></path>
                </g>
                <g>
                    <path style={{fill="#F88F2D"}} d="M596.473,389.394c0,216.894-135.035,388.081-135.035,388.081l0.789,0.795L889.707,388.9L462.227-0.467
                        l-1.095,1.097C461.132,0.629,596.473,177.202,596.473,389.394z"></path>
                </g>
            </g>
        </g>
    </g>
</svg>

This gives me the following error: 这给了我以下错误:

Module build failed: SyntaxError: Unexpected token (90:58) 模块构建失败:SyntaxError:意外令牌(90:58)

This makes sense, but in the d attributes in my case there are parts like M596.473,389.394c0 . 这是有道理的,但在我的情况下,d属性中有M596.473,389.394c0类的M596.473,389.394c0 As you can imagine, the letter c between the 4 and 0 cause issues as the letter is not an integer. 可以想象,介于4和0之间的字母c会引起问题,因为该字母不是整数。

How can I make this SVG work without using a library or something else? 如何在不使用库或其他工具的情况下使此SVG正常工作? I just want to convert this SVG to valid JSX. 我只想将此SVG转换为有效的JSX。

Change style={{fill="#B8CD43"}} to style={{fill: "#B8CD43"}} every place, it will work. 在每个位置将style={{fill="#B8CD43"}}更改为style={{fill: "#B8CD43"}}使用。

There is no need of dangerouslySetInnerHTML. 无需危险地设置内部HTML。 Here is the working Demo 这是工作中的演示

React does not play nice with SVG yet. React在SVG上还不能很好地发挥作用。 If your svg is static I personally find it much easier to do it this way: 如果您的svg是静态的,那么我个人觉得这样做很容易:

var img = `<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 252.5 148.7" xml:space="preserve">
<g>
    <polygon points="252.5,0 252.5,52.6"/>
</g>
</svg>`

In string you can put your svg exactly how you read it from file/DB without any extra conversions. 使用字符串,您可以将svg准确地放在从文件/数据库中读取的方式中,而无需进行任何额外的转换。

And then later use it in render like this: 然后像下面这样在渲染中使用它:

<div dangerouslySetInnerHTML={{__html: img}}/>

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

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