Skip to content

useShare

See: (https://developer.mozilla.org/en-US/docs/Web/API/Web_Share_API).

Demo

INFO

The component uses useShare hook to know if Web share API is supported and returns a button to share link to this library.

Loading demo…
Show source code
tsx
import { useRef } from "react"
import { useShare } from "../../../.."

export const UseShare = () => {
	const sharedDate = useRef<ShareData>({
		title: "React-tools",
		text: "React tools",
		url: window.location.origin
	})
	const { isSupported, share } = useShare();

	const onClick = () => share(sharedDate.current);

	return <div style={{textAlign: "center"}}>
		<p>Is supported: {isSupported.toString()}</p>
		<button onClick={onClick}>Share</button>
	</div>
}

Types

UseShareResult

Result object returned by useShare.

PropertyTypeRequiredDescription
share(data: ShareData) => Promise<void>Invoke the native share sheet with the provided data.
isSupportedbooleantrue when the Web Share API (navigator.share) is available in the current browser.

Released under the MIT License.