Create-react-app: Cannot import svg as component

Created on 4 Oct 2018  Â·  4Comments  Â·  Source: facebook/create-react-app

Is this a bug report?

Yes.

Did you try recovering your dependencies?

Yes

Which terms did you search for in User Guide?

svg

Environment

I cannot provide you environment info since I am getting the following error:

npx create-react-app --info
npx: installed 1 in 2.366s
Path must be a string. Received undefined
C:\Users\dxh06AppData\Roaming\npm\node_modulescreate-react-app\index.js

Environment Info:
(node:12112) UnhandledPromiseRejectionWarning: Error: The system cannot find the path specified.

Steps to Reproduce

Importing a svg file as a React component generates an error at run-time. See code below.

Expected Behavior

The svg should become a standard Rect Component like stated by svgr.

Actual Behavior

When I import a svg and use it as a React component I am getting the following error at run-time:

InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/logo.5d5d9eef.svg') is not a valid name.
â–¶ 20 stack frames were collapsed.
Module../src/index.js
C:/Git/web-react-components-library/src/index.js:7

   4 | import App from './App';
   5 | import * as serviceWorker from './serviceWorker';
   6 | 
>  7 | ReactDOM.render(<App />, document.getElementById('root'));
   8 | 
   9 | // If you want your app to work offline and load faster, you can change
  10 | // unregister() to register() below. Note this comes with some pitfalls.

Reproducible Demo

import React, { Component } from 'react';
import Logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <Logo></Logo>
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    );
  }
}

export default App;

Most helpful comment

You wrote:

import Logo from './logo.svg';

The User Guide says:

import { ReactComponent as Logo } from './logo.svg';

Don't forget the curly braces in the import! The ReactComponent import name is special and tells Create React App that you want a React component that renders an SVG, rather than its filename.

Hope this helps!

All 4 comments

May be related to #5276.

You wrote:

import Logo from './logo.svg';

The User Guide says:

import { ReactComponent as Logo } from './logo.svg';

Don't forget the curly braces in the import! The ReactComponent import name is special and tells Create React App that you want a React component that renders an SVG, rather than its filename.

Hope this helps!

My bad I am sorry. Thanks.

FWIW, Currently, this issue was the first place I found a code example to get things working. The new CRA site + thinned out README had me lost.

I can now see it's all at https://facebook.github.io/create-react-app/docs/adding-images-fonts-and-files. Maybe since it wasn't searchable from the initial page, I moved on to try and find my solution elsewhere. That was a nice thing about the monolithic README.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alleroux picture alleroux  Â·  3Comments

onelson picture onelson  Â·  3Comments

barcher picture barcher  Â·  3Comments

oltsa picture oltsa  Â·  3Comments

dualcnhq picture dualcnhq  Â·  3Comments