通过SVG与SVG与React失败

时间:2019-09-08 16:50:56

标签: reactjs svg

在webpack配置中具有此功能:

  module: {
    rules: [
      {
        test: /\.svg$/,
        use: ['@svgr/webpack'],
      }
    ]
  },

还有一个简单的导入组件

import Globe from './Globe.svg'

(...)
<div>
  <Globe />
</div>

导致以下错误,并导致整个页面呈现硬停止。

Warning: <data:image/svg+xml;base64,ZnVuY3Rpb24gX2V... (more here)0.=87…
...BkZWZhdWx0IFN2Z0NvbXBvbmVudDs= /> is using incorrect casing. 
Use PascalCase for React components, or lowercase for HTML elements.

1 个答案:

答案 0 :(得分:1)

似乎是已知问题https://github.com/smooth-code/svgr/issues/83

我建议包括url-loaderhttps://www.smooth-code.com/open-source/svgr/docs/webpack/#using-with-url-loader-or-file-loader

  

在您的webpack.config.js中:

{   test: /\.svg$/,   use: ['@svgr/webpack', 'url-loader'], }
     

在您的代码中:

import starUrl, { ReactComponent as Star } from './star.svg' 
const App = () => (
  <div>
    <img src={starUrl} alt="star" />
    <Star />
  </div>
)