Created
June 19, 2023 04:02
-
-
Save BennyKok/cf925c123c4b9555e21140d2f1c7f538 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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