Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SVG from react-icons components

I need to get the svg element from a react-icon component to render the image using a different Javascript library.

I'm using paperjs as the drawing engine for this demo I'm working on, for the UI I use react-icons and react-bootstrap. Now paperjs allows to importSVG images, so I try the following:

import { MdMemory } from "react-icons/md";

const addDevice = () => {
  const svgGroup = Paper.project.importSVG(<MdMemory />);
  svgGroup.position = pointA.clone();
}

But when I do so I get the following error: Error: Unsupported SVG source: [object Object]

When I inspect the other place where I use <MdMemory /> I get an svg element, so I wonder if what I'm trying to achieve is possible as I don't want to load duplicated assets.

UPDATE

After spending a bit more time, I came up with the following:

console.log(MdMemory().props.children[0].props.d);
const svgGroup = Paper.project.importSVG(`<svg><path d=${MdMemory().props.children[0].props.d}></path></svg>`);

Where MdMemory().props.children[0].props.d is the actual svg path, but I'm still unable to render anything...

like image 420
Ricardo Sanchez Avatar asked Jun 02 '26 09:06

Ricardo Sanchez


2 Answers

This worked better for me:

ReactDOMServer.renderToString(<MdMemory/>)
like image 107
Tovi Newman Avatar answered Jun 05 '26 00:06

Tovi Newman


I just searched for the icon on React Icons and inspected that icon through console, and then I copied and pasted it:

  • Browser

    browser

  • Copying

    copying

  • Paste

    pasting

like image 34
Saroj Ghalan Avatar answered Jun 04 '26 23:06

Saroj Ghalan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!