Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Doesn't work with imported SVGs #63

Open
OwenMelbz opened this issue Mar 22, 2020 · 4 comments
Open

[Bug] Doesn't work with imported SVGs #63

OwenMelbz opened this issue Mar 22, 2020 · 4 comments

Comments

@OwenMelbz
Copy link

Hi,

I'm having trouble getting the plugin to work with imported SVGs e.g.

import Logo from './images/logo.svg';

However it works when the SVG is inlined.

You can view a demo of the problem here:

Edit react-svg-unique-id

@elderapo
Copy link
Owner

Seems like it's not possible to iterate over react children (nested elements of svg) for imported svgs. Not sure what can be done about that.

@MacDue
Copy link

MacDue commented Mar 29, 2020

It's a hack but I've found a workaround for this.

import MySVG from '-!react-svg-loader!./mysvg.svg';

// Little hack around the imported svg to get at the SVG react/jsx elements.
const UniqueSVG = ({svg}) => (
  <SVGUniqueID>
    {React.createElement(svg).type({ styles: {} })}
  </SVGUniqueID>)

// ...Then somewhere in your render:
<UniqueSVG svg={MySVG}/>

That's it.

@elderapo
Copy link
Owner

I don't use this library anymore and have no interest in fixing/updating it. If someone creates a pull request with a working fix I will be willing to merge it.

@rdourado
Copy link

I have another “hack” suggestion that worked for me.
In my case, I'm using @svgr/webpack to import the SVG files, and it looks like its default template protects SVG children somehow.
To get around it, just call the component as a function instead of using JSX. Like this:

import { SVGUniqueID } from 'react-svg-unique-id'
import myicon from './myicon.svg'

const MyIcon = (props) => {
  return <SVGUniqueID>{myicon(props)}</SVGUniqueID>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants