如何在React中使用SVG而不使用危险地设置InnerHTML? [英] How do I use an SVG in React without using dangerouslySetInnerHTML?

查看:63
本文介绍了如何在React中使用SVG而不使用危险地设置InnerHTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Facebook图标创建了一个React组件,以便可以在页脚中使用.不幸的是,除非我使用dangerouslySetInnerHTML,否则它将无法正常工作,因此该图标将显示-但它似乎已损坏.

I've created a React component for the Facebook icon to be able to use in the footer. Unfortunately it doesn't work unless I use dangerouslySetInnerHTML, so the icon shows up - however it appears broken.

这是Facebook组件的代码:

Here is the code for the Facebook component:

import React from 'react';
import ReactDOM from 'react-dom';

class Facebook extends React.Component{
 render() {
   var svgString = '<?xml version="1.0" encoding="iso-8859-1"?><!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  --><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><div><span id="facebook"><svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="49.652px" height="49.652px" viewBox="0 0 49.652 49.652" style="enable-background:new 0 0 49.652 49.652;" xml:space="preserve" class="icon"><g><circle cx="25" cy="25" r="20" class="circle" fill="white"/><path d="M24.826,0C11.137,0,0,11.137,0,24.826c0C49.652,11.137,38.516,0,24.826,0z M31,25.7h-4.039c0,6.453,0,14.396,0,14.396h-5.985c0,0,0-7.866,0-14.396h-2.845v-5.088h2.845v-3.291c0-2.357,1.12-6.04,6.04-6.04l4.435,0.017v4.939c0,0-2.695,0-3.219,0c-0.524,0-1.269,0.262-1.269,1.386v2.99h4.56L31,25.7z" /></g></svg></span>'
   return (
     <div>
    <div dangerouslySetInnerHTML={{ __html: svgString }} />
     </div>
   )
 }
};


export default Facebook;

这是原始SVG的代码

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<div>
  <span id="facebook">
  <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="49.652px" height="49.652px" viewBox="0 0 49.652 49.652" style="enable-background:new 0 0 49.652 49.652;" xml:space="preserve" class="icon">
    <g>
      <circle cx="25" cy="25" r="20" class="circle" fill="white"/>
        <path d="M24.826,0C11.137,0,0,11.137,0,24.826c0,13.688,11.137,24.826,24.826,24.826c13.688,0,24.826-11.138,24.826-24.826
            C49.652,11.137,38.516,0,24.826,0z M31,25.7h-4.039c0,6.453,0,14.396,0,14.396h-5.985c0,0,0-7.866,0-14.396h-2.845v-5.088h2.845
            v-3.291c0-2.357,1.12-6.04,6.04-6.04l4.435,0.017v4.939c0,0-2.695,0-3.219,0c-0.524,0-1.269,0.262-1.269,1.386v2.99h4.56L31,25.7z
            " />
    </g>

  </svg>
</span>
</div>

推荐答案

SVG应该可以在React中本地运行,无需设置DOCTYPE和XML编码,版本或名称空间(JSX不是XML,它类似于XML ):

SVGs should work natively in React, there's no need to set DOCTYPE and the XML encoding, version, or namespace (JSX is not XML, it is XML-like):

render() {
  return (
    <div>
      <svg version="1.1" id="Capa_1" x="0px" y="0px" width="49.652px" height="49.652px" viewBox="0 0 49.652 49.652" style="enable-background:new 0 0 49.652 49.652;" className="icon">
        <g>
          <circle cx="25" cy="25" r="20" className="circle" fill="white"/>
          <path d="M24.826,0C11.137,0,0,11.137,0,24.826c0,13.688,11.137,24.826,24.826,24.826c13.688,0,24.826-11.138,24.826-24.826
            C49.652,11.137,38.516,0,24.826,0z M31,25.7h-4.039c0,6.453,0,14.396,0,14.396h-5.985c0,0,0-7.866,0-14.396h-2.845v-5.088h2.845
            v-3.291c0-2.357,1.12-6.04,6.04-6.04l4.435,0.017v4.939c0,0-2.695,0-3.219,0c-0.524,0-1.269,0.262-1.269,1.386v2.99h4.56L31,25.7z
          " />
        </g>
      </svg>
    </div>
  )
}

还要确保classclassName.

这篇关于如何在React中使用SVG而不使用危险地设置InnerHTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆