use-link-preview
is a React custom hook that simply provides link-preview metadata by link-preview-server.
npm install use-link-preview
npm install use-link-preview
import useLinkPreview from "use-link-preview";
function LinkPreview({ url }) {
const { metadata, isLoading, isError } = useLinkPreview(url);
// Usage example
return (
<>
{isLoading && <LoadingComponent />}
{isError && <ErrorComponent />}
{metadata && (
<>
<h3>{metadata.title}</h3>
<p>{metadata.description}</p>
<p>{metadata.domain}</p>
<img src={metadata.img} />
<p>{metadata.requestUrl}</p>
</>
)}
</>
);
}
const { metadata, isLoading, isError } = useLinkPreview(url);
url
: url string to get link-preview metadata
metadata
: Received link-preview metadata (or null)isLoading
: Check if metadata is being parsed. true or falseisError
: error thrown by parsing metadata (or null)
-
metadata
- title: string | null;
- description: string | null;
- img: string | null;
- domain: string;
- requestUrl: string;
-
isLoading: boolean
-
isError: Error | null