Learning about various utility Hooks
We are now going to learn about a selection of some useful utility Hooks provided by the useHooks library.
useCopyToClipboard
The Copy To Clipboard Hook makes it easy to copy text to the clipboard across various browsers. If available, it uses the modern navigator.clipboard.writeText
API. Otherwise, it falls back to the traditional document.execCommand("copy")
method, ensuring that the functionality works for older and newer browsers. This Hook is also provided by .
The useCopyToClipboard
function has the following signature:
const [copiedText, copyToClipboard] = useCopyToClipboard()
It provides a similar API to the State Hook, with a copyToClipboard
function that accepts a string and copies it to the clipboard, as well as storing it in the copiedText
value. This value can also be used to check if we successfully copied the text to the clipboard.
Let’s now use the Hook to implement a way of copying a link...