Reputation: 6856
I get the following error when trying to download svg as a React Component.
SyntaxError: unknown: Namespace tags are not supported by default. React's JSX doesn't support namespace tags. You can turn on the 'throwIfNamespace' flag to bypass this warning.
import React from "react";
import { ReactComponent as LO } from "../a/Logo.svg"
import { NavLink } from "react-router-dom";
const Logo = () => (
<>
<NavLink to={"/"}>
<LO width={"40px"} height={"40px"} />
</NavLink>
</>
);
export default Logo;
What is the reason ?
Upvotes: 135
Views: 137483
Reputation: 2765
I recommend checking out SVGO - it's a command-line tool that automatically strips out the offending tags.
npm install -g svgo
svgo *.svg
Upvotes: 3
Reputation: 3994
In the SVG file, try changing:
sketch:type TO sketchType
xmlns:xlink TO xmlnsXlink
xlink:href TO xlinkHref
xmlns:bx TO xmlnsBx
bx:origin TO bxOrigin
etc...
The idea is to create a camelCase property, remember that you are working with JSX, you are not creating a string as XML does.
Upvotes: 343
Reputation: 236
You first have to convert the SVG file to JSX then use it. Use https://svg2jsx.com/
Upvotes: 0
Reputation: 3104
In mine I had to change:
xmlns:svg="http://www.w3.org/2000/svg"
to
xmlnsSvg="http://www.w3.org/2000/svg"
Upvotes: 6
Reputation: 965
This is a nice tool that converts an SVG into a react friendly component and includes support for typescript
https://react-svgr.com/playground/
Upvotes: 5
Reputation: 177
The most simple solution is to just convert the SVG file to JSX.Then create a separate component or just copy paste the tag. This website does the job perfectly.
Upvotes: 15
Reputation: 11
I had the same issue with all my svg files. Opening them in Adobe Illustrator and saving them again solved the issue.
Upvotes: 1
Reputation: 159
I recommend using svgo to compress your SVG files. Simple CLI tool that runs with NodeJS and doesn't require you to upload your copyrighted SVGs anywhere.
Upvotes: 2
Reputation: 658
Since we are in the JSX playground your SVG tags should be written as camelCase
So you can easily optimize your SVG using the link below to avoid this error :)
Upvotes: 49