question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange SVG Component Duplication

See original GitHub issue

Given:

// icons/index.js
export alerts from "./alerts.svg";
export apm from "./apm.svg";
export data from "./data.svg";
export infrastructure from "./infrastructure.svg";
export support from "./support.svg";
// some react component
import * as icons from "./icons/index.js";
//...
class Nav extends React.Component {
  render() {
    return (
      <div>
        {Object.keys(icons).map(k => React.createElement(icons[k], { key: k }))}
      </div>
    )
  }
}

What is rendered is something like: messages image 646538110 But those should all be different SVGs.

I’m using react@15.5.4, webpack@2.6.1, webpack-dev-server@2.4.5, react-svg-loader@2.0.0-alpha.1.

// webpack.config.js
//...
    module: {
      rules: [
        {
          enforce: "pre",
          test: /\.js$/,
          use: [{ loader: "babel-loader" }],
          include: [/src/],
        },
        {
          test: /\.svg$/,
          use: [
            { loader: "babel-loader" },
            {
              loader: "react-svg-loader",
              query: {
                jsx: true,
                es5: false,
                svgo: {
                  pretty: true,
                  plugins: [{ removeTitle: true }],
                  floatPrecision: 2,
                },
              },
            },
          ],
        },
      ],
    },
//...

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jaisonjjamescommented, Feb 28, 2020

In my case, it was a problem with the SVG icons, icons are duplicated with the ID, I have changed it with a unique id by opening them into an editor.

1reaction
and-whocommented, Sep 13, 2018

Workaround, generating unique Ids in the cleanupIDs Plugin of svgo:

      {
        test: /\.svg$/,
        use: [
          {
            loader: "babel-loader"
          },
          {
            loader: "react-svg-loader",
            options: {
              jsx: true,
              svgo: {
                plugins: [
                  { 
                    cleanupIDs: {
                      prefix: {
                        toString() {
                          this.counter = this.counter || 0;
                          return `id-${this.counter++}`;
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        ]
      }

Found this Solution in this Issue: https://github.com/svg/svgo/issues/674#issuecomment-324193597

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix duplicate SVG ID collision in React | by Anton Ball - Medium
A gotcha with SVG fill ids that can mean unexpected results when inline SVGs are rendered onto the page.
Read more >
Multiple SVG Components not working or render as expected
I'm having a strange problem; and haven't been able to get a solution for it. Context: NextJS App. I have 5 different svgs,...
Read more >
If You're Inlining SVG Icons, How Do You Deal With Unique ...
It's not only bad, it's simply not valid HTML to have duplicate IDs — and sure won't pass the w3c validator, so i...
Read more >
What's happening with my SVG components? : r/Frontend
It probably has to do with the contents of the SVG. Do they make use of id fields and there are duplicate IDs...
Read more >
SVG Tutorial: How to Code SVG Icons by Hand
All SVG shapes are defined within the SVG element itself, like this: ... but there's a weird fill in between the two that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found