Skip to content

Instantly share code, notes, and snippets.

@BennyKok
Created June 19, 2023 04:02
Show Gist options
  • Save BennyKok/cf925c123c4b9555e21140d2f1c7f538 to your computer and use it in GitHub Desktop.
Save BennyKok/cf925c123c4b9555e21140d2f1c7f538 to your computer and use it in GitHub Desktop.
import useSWR from 'swr';
const fetcher = (url: string) => fetch(url).then((res) => res.json());
function NpmPackageInstaller({
name,
}: {
name: string;
}) {
const { data, error } = useSWR(`https://registry.npmjs.org/${name}`, fetcher);
if (error) return <div>Error loading package version</div>;
if (!data) return <div>Loading...</div>;
const latestVersion = data['dist-tags'].latest;
return (
<>
npm i {name}@{latestVersion}
</>
);
}
export default NpmPackageInstaller;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment