General
Copytext

Copy To Clipboard

This Copy To Clipboard Function is used for Copying the Contents to the Clipboard

Copy to Clipboard

copyToClipboard.ts
export function copyToClipBoard(text: string){
    navigator.clipboard.writeText(text).then(
        () => {
            alert('copied');
        },
        () =>{
            alert('cannot copy');
        }
    )
}

Usage

BlogCard.tsx
function BlogCard() {
    const text: string = "This is CopyToClipboard Util Function";
 
    const copyHandle = () => {
        copyToClipBoard(text)
    }
 
    return (
        <>
            <p>{text}</p>
            <button
                onClick={copyHandle}
            >
                Copy
            </button>
        </>
    )
}