Answer the question
In order to leave comments, you need to log in
What is the error in the component for copying the svg icon code to the clipboard?
I expect that when you click on the button, the icon code will be copied to the clipboard. Only [object object] is
copied https://codesandbox.io/s/vv63po7zw5
Answer the question
In order to leave comments, you need to log in
JSX is syntactic sugar over the React.createElement call. Therefore, the element described using JSX svg is just a call to React.createElement, which returns an object, which is the so-called Virtual DOM Node, according to which the library builds a node in the real DOM.
To solve your problem, you can use ref.
let iconRef;
const getIconRef = node => iconRef = node;
const IconsList = {
internet: (
<svg ref={getIconRef} width="22px" height="16px" viewBox="0 0 22 16" version="1.1">
/* ... */
</svg>
),
];
copyToClipboard = text => {
var textField = document.createElement("textarea");
textField.innerText = iconRef;
document.body.appendChild(textField);
textField.select();
document.execCommand("copy");
textField.remove();
};
import { renderToString } from 'react-dom/server';
textField.innerText = renderToString(/* иконка описанная JSX */);
You have a tree in internet jsx and not a string.
Use something like https://www.npmjs.com/package/jsx-to-string
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question