[javascript] In reactJS, how to copy text to clipboard?

Best solution with react hooks, no need of external libraries for that

_x000D_
_x000D_
import React, { useState } from 'react';_x000D_
_x000D_
const MyComponent = () => {_x000D_
const [copySuccess, setCopySuccess] = useState('');_x000D_
_x000D_
// your function to copy here_x000D_
_x000D_
  const copyToClipBoard = async copyMe => {_x000D_
    try {_x000D_
      await navigator.clipboard.writeText(copyMe);_x000D_
      setCopySuccess('Copied!');_x000D_
    } catch (err) {_x000D_
      setCopySuccess('Failed to copy!');_x000D_
    }_x000D_
  };_x000D_
_x000D_
return (_x000D_
 <div>_x000D_
    <Button onClick={() => copyToClipBoard('some text to copy')}>_x000D_
     Click here to copy_x000D_
     </Button>_x000D_
  // after copying see the message here_x000D_
  {copySuccess}_x000D_
 </div>_x000D_
)_x000D_
}
_x000D_
_x000D_
_x000D_

check here for further documentation about navigator.clip board , navigator.clipboard documentation navigotor.clipboard is supported by a huge number of browser look here supported browser

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to reactjs

Error: Node Sass version 5.0.0 is incompatible with ^4.0.0 TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined raised when starting react app Template not provided using create-react-app How to resolve the error on 'react-native start' Element implicitly has an 'any' type because expression of type 'string' can't be used to index Invalid hook call. Hooks can only be called inside of the body of a function component How to style components using makeStyles and still have lifecycle methods in Material UI? React Hook "useState" is called in function "app" which is neither a React function component or a custom React Hook function How to fix missing dependency warning when using useEffect React Hook? Unable to load script.Make sure you are either running a Metro server or that your bundle 'index.android.bundle' is packaged correctly for release

Examples related to clipboard

In reactJS, how to copy text to clipboard? Copy output of a JavaScript variable to the clipboard Leave out quotes when copying from cell How to Copy Text to Clip Board in Android? How does Trello access the user's clipboard? Copying a rsa public key to clipboard Excel VBA code to copy a specific string to clipboard How to make vim paste from (and copy to) system's clipboard? Python script to copy text to clipboard Copy to Clipboard for all Browsers using javascript