In reactJS, how to copy text to clipboard?

前端 未结 21 1939
刺人心
刺人心 2020-12-02 04:43

I\'m using ReactJS and when a user clicks a link I want to copy some text to the clipboard.

I am using Chrome 52 and I do not need to support any other browsers.

21条回答
  •  抹茶落季
    2020-12-02 05:09

    here is my code:

    import React from 'react'
    
    class CopyToClipboard extends React.Component {
    
      textArea: any
    
      copyClipBoard = () => {
        this.textArea.select()
        document.execCommand('copy')
      }
    
      render() {
        return (
          <>
             this.textArea = textarea}  />
            
    CLICK
    ) } } export default CopyToClipboard

提交回复
热议问题