A lightweight React hook for dynamically loading and applying Google Fonts.
npm install use-google-fontSimply call the hook with the font name you want to use and it will load the font in dynamically.
import { useGoogleFont } from 'use-google-font';
const App = () => {
useGoogleFont('Gigachad Gothic', { ...options })
}Use the optional weights option to change which weights are loaded.
By default only normal 500 and 700 wieghts will be loaded.
{
weights: {
normal: [500, 700],
italic: [100]
}
}By setting default to true, the font will be set on the body tag automatically.
{
default: true
}Optionally, you can add the following HTML snippet in your document's head to optimize font loading. Placing these tags in the ensures the preconnect requests happen early, reducing latency:
<head>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin />
</head>