简体   繁体   English

如何在 React 中使用 SVG 而不使用危险的SetInnerHTML?

[英]How do I use an SVG in React without using dangerouslySetInnerHTML?

I've created a React component for the Facebook icon to be able to use in the footer.我为 Facebook 图标创建了一个 React 组件,以便能够在页脚中使用。 Unfortunately it doesn't work unless I use dangerouslySetInnerHTML , so the icon shows up - however it appears broken.不幸的是,除非我使用dangerouslySetInnerHTML ,否则它不起作用,因此图标显示 - 但它看起来已损坏。

Here is the code for the Facebook component:这是 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;

and here is the code for the original SVG这是原始 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>

Depends on which version of react your using right now 取决于当前使用的反应版本

for CRA Version 3 - 适用于CRA版本3-

you can simply do this - save your svg in different file like facebookIcon.svg 您可以简单地做到这一点-将您的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;

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

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): 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>
  )
}

Also make sure class is className instead. 还要确保classclassName

使用像这样的库svg-to-jsx

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

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