Skip to content
This repository was archived by the owner on Apr 1, 2023. It is now read-only.

How to import svg as component #223

Open
rimlin opened this issue Apr 7, 2020 · 11 comments
Open

How to import svg as component #223

rimlin opened this issue Apr 7, 2020 · 11 comments

Comments

@rimlin
Copy link

rimlin commented Apr 7, 2020

Using CRA, i was able to import svg like component and inline it:
import { ReactComponent as skip } from '../../../assets/icons/skip.svg';
But if i try it, i will get an error:

Error: 'ReactComponent' is not exported by src/assets/icons/skip.svg, imported by src/components/shared/Icon/Icon.component.tsx

If i import like this:
import skip from '../../../assets/icons/skip.svg';
i will get link to svg, but i need inline svg code.

@rennerwins
Copy link

I'm having the same problem

@ChamseddineBhd
Copy link

I'm facing the same problem !!

@bohdanbirdie
Copy link

@transitive-bullshit seems like the d.ts file has this described but it fails on the build
I assume this should work somehow but don't see the options so far

@bohdanbirdie
Copy link

@transitive-bullshit deep apologies for double ping

Just wanted to bring the attention to PR above

Thank you for the great tool!

@DDDDDanica
Copy link
Contributor

Thanks a lot @bohdanbirdie you saved me ! Can't wait to test when it's released ~

@bohdanbirdie
Copy link

@DDDDDanica you can use my fork while the package is not published. (or use original git repo)
"microbundle-crl": "https://github.com/bohdanbirdie/microbundle.git",

@DDDDDanica
Copy link
Contributor

DDDDDanica commented Jun 25, 2020

Hey @bohdanbirdie
I wonder if it is babel setting, but when i run storyshots inside the /example src (aka create-react-app), i got this following error:

Screenshot 2020-06-25 at 11 12 59

Any ideas?

@bohdanbirdie
Copy link

@DDDDDanica yes, this was also my problem
You can only use this syntax

import { ReactComponent as Bell } from '../../assets/svg/bell.svg';

@DDDDDanica
Copy link
Contributor

DDDDDanica commented Jun 25, 2020

@bohdanbirdie Hi,
So in my Icon component (inside the library), i imported the svg;
and in example app, i imported Icon component along with pre-defined svgs.
which means:

import { ReactComponent as Bell } from '../../assets/svg/bell.svg';

It happens in Icon component, and in storybook i direct call

<Icon
        type={type}
        name="bell"
      />

It works to generate the stories, but failed in storyshots (jest).

@bohdanbirdie
Copy link

It should be like this only. If you want to use a single component (Icon) for that you will need to create it, and map the name prop to appropriate imported SVG component (like { 'bell': Bell }

import { ReactComponent as Bell } from '../../assets/svg/bell.svg';


const MyComp = () => {
  return <Bell />
}

@DDDDDanica
Copy link
Contributor

Thanks @bohdanbirdie Eventually i abandoned storyshots and just append snapshots to component itself. Works for me. Thanks for your help !

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

No branches or pull requests

5 participants