useClipboard

useClipboard is a custom hook that handles copying content to clipboard.

Import#

import { useClipboard } from '@dwarvesf/react-hooks'

Return value#

The useClipboard hook returns an object with the following fields:

NameTypeDefaultDescription
valuestring_The copied value.
onCopyfunction_Callback function to copy content.
hasCopiedbooleanfalseIf true, the content has been copied .

Usage#

function Example() {
const [value, setValue] = React.useState('Hello world')
const { hasCopied, onCopy } = useClipboard(value)
return (
<>
<Flex mb={2}>
<Input value={value} isReadOnly placeholder="Welcome" />
<Button onClick={onCopy} ml={2}>
{hasCopied ? 'Copied' : 'Copy'}
</Button>
</Flex>
<Editable placeholder="Paste here">
<EditablePreview width="100%" />
<EditableInput />
</Editable>
</>
)
}

Parameters#

The useClipboard hook accepts a single value param:

NameTypeDefaultDescription
valuestring_The copied value.
Edit this page

Made with ❤️ by Dwarves Foundation