'How to add copy code snippet button in React

I have code snippet. I need to add space between different lines so I have <code> and <pre> for the code block. I want users to click on a button and copy this entire code as such.


import {CopyToClipboard} from 'react-copy-to-clipboard';

let snippet =  <pre><code>  {`
 z <!-- Start of test client code snippet -->

           <script>
               fjs.parentNode.insertBefore(js, fjs);
           })(window, document, "script", "_hw", "./widget.js");
           _hw("init", { debug: true, clientID: ${} });

           </script>
 <!-- End of test client code snippet -->
       `}
       </code></pre>


<CopyToClipboard text={snippet}>
          <button>Copy Snippet</button>
          </CopyToClipboard>

But it's not working. Does it work only for input field?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source