如何在不使用dangerouslySetInnerHTML的情况下在React中使用SVG?

时间:2017-05-17 21:58:00

标签: reactjs svg react-jsx

我已经为Facebook图标创建了一个React组件,可以在页脚中使用。不幸的是,除非我使用dangerouslySetInnerHTML,否则它不起作用,因此图标会显示 - 但它看起来很破碎。

以下是Facebook组件的代码:

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>

2 个答案:

答案 0 :(得分:0)

SVG应该在React中本地工作,不需要设置DOCTYPE和XML编码,版本或命名空间(JSX不是XML,它是类似XML的):

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

答案 1 :(得分:0)

取决于您现在使用的是哪个版本

对于CRA版本3-

您可以简单地执行此操作-将svg保存在其他文件中,例如facebookIcon.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.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>
import React from 'react';
import ReactDOM from 'react-dom';
import { ReactComponent as fbIcon } from './facebookIcon.svg';

class Facebook extends React.Component{
 render() {
   return (
     <div>
      <fbIcon />
     </div>
   )
 }
};


export default Facebook;

对于CRA版本2- 您可以检查此链接-https://egghead.io/lessons/react-add-svgs-as-react-components-with-create-react-app-2-0