0

So I'm trying to copy text to clipboard when user clicks on an html element in react. So far I've only seen solutions for html & Javascript, but none of them related to just react. For example, using query selectors. So I'm mapping through an array to provide text: whenever a user clicks on that mapped element, i want to copy to clipboard. Like so:

const array = ['Email', 'Phone', 'Address']
const arrayrow = array.map(el=>{
 return <p onClick={()=>copyToClipboard()}>
 {el}
</p>
 })

So just wondering how i should go about writing this function.

function copyToClipboard(){}
Joshua Bitton
  • 383
  • 2
  • 12
  • You already have access to the value for each `p` when you are mapping, so just pass the value in to your function? `() => copyToClipboard(el)` - what specifically about `react` are you having a problem with? – chazsolo Apr 20 '21 at 15:41
  • Just avoiding query selectors because I know there a way of doing it in javascript, but not sure if there is a 'react' way of doing it – Joshua Bitton Apr 20 '21 at 15:48
  • [Copy text from a div to clipboard using document.execCommand in React](https://stackoverflow.com/a/67032810/2873538) – Ajeet Shah Apr 20 '21 at 15:51
  • The parameter passed down to navigator.clipboard.writeText(newClip), would be the text? IF so, how come i am getting an error? – Joshua Bitton Apr 20 '21 at 15:53

0 Answers0