The need to copy something to the clipboard can be handled easily using this component: CopyToClipboard
.
- Add the component
<CopyToClipboard />
This is the simplest form using defaultProps, which will show you how it works. You will want to change the props:
CopyToClipboard.defaultProps = {
label: "Copy",
silent: false,
text: "Set prop 'text' to your text to copy, or add call back function 'onCopy' which returns the value to copy",
onCopy: txt => {
// return "Nothing to copy "+new Date();
return txt;
}
};
What to show in the button
Should a prompt be shown with the copied content?
One way to use the component is to provide in a prop the text to copy.
You can provide a function which should then return a text which will be copied to clipboard
The function will be called with the value of text
property.
I have made a ReactJS pen which is using this component.
Send your feedback to me on twitter: @netsi1964