javascriptreactjssafarimobile-safari

Safari clipboard error "TypeError: undefined is not an object"


Have an error only in safari browser:

TypeError: undefined is not an object (evaluating 'navigator.clipboard.writeText')

I'm passing my current link. What could be the problem?

copyLink = e => {
  e.preventDefault();
  console.log(document.location.href)
  navigator.clipboard
    .writeText(document.location.href)
    .then(() => {
      this.setState({
        urlIsCopied: true
      });
    })
    .catch(e => console.error(e));
};

Solution

  • Can you please look at the plugin copy-to-clipboard. Simply use below code and it works.

    copy('Text to Copy!');
    

    Hope this will help you!