120

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 ?

Edgar
  • 6,022
  • 8
  • 33
  • 66

8 Answers8

304

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.

Mohammadreza Khedri
  • 2,523
  • 1
  • 11
  • 22
Frawel
  • 3,574
  • 1
  • 17
  • 13
  • 23
    So ... we have to use JSX-friendly SVGs now? Seems a bit ridiculous... – Josh M. Aug 15 '22 at 14:22
  • THANK YOU! This is the answer that keeps on giving. I am using Vectornator to create my SVG files and it outputs a lot of `vectornator:layerName` So, I went through and every time I found a colon that wasn't in a URL, but was naming something, I changed it to camelCase (like `vectornatorLayerName`) and the SVG works now! Thank you @Frawel – CWarrington Jun 07 '23 at 02:40
  • I don't understand the need to rewrite the SVG file settings to work wit JSX. I can use SVG files created in Python, view them through Django in html then view the same SVG files in TypeScript in Angular. Why can't the React people build converters for this? – Brian Quinn Jun 18 '23 at 19:04
40

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 :)

svgminify

Ahmad Salih
  • 558
  • 4
  • 9
14

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.

https://svg2jsx.com/

  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 14 '22 at 15:56
  • 2
    Thank you tharusha, this is the winning answer for me! [svgminify](https://www.svgminify.com) did not fix the root issue: React throwing a wobbler at namespaces. – Sean Mizen Mar 24 '22 at 09:56
3

This is a nice tool that converts an SVG into a react friendly component and includes support for typescript

https://react-svgr.com/playground/

ctown4life
  • 835
  • 1
  • 11
  • 24
2

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.

devgioele
  • 129
  • 2
  • 10
2

In mine I had to change:

   xmlns:svg="http://www.w3.org/2000/svg"

to

   xmlnsSvg="http://www.w3.org/2000/svg"
Sooth
  • 2,834
  • 23
  • 26
1

I had the same issue with all my svg files. Opening them in Adobe Illustrator and saving them again solved the issue.

Bozena Z.
  • 11
  • 1
0

You first have to convert the SVG file to JSX then use it. Use https://svg2jsx.com/