✨ React hook to dynamically update your page’s favicon.
Install the @williamgrosset/react-use-favicon
package:
npm install @williamgrosset/react-use-favicon
Import the useFavicon
hook:
import React from 'react'
import useFavicon from '@williamgrosset/react-use-favicon'
export default function App() {
const { url, update, restore } = useFavicon()
return (
<div>
{url && <img src={url} alt="Favicon" />}
<button onClick={() => update('/favicon.ico')}>Update</button>
<button onClick={restore}>Restore</button>
</div>
)
}
useFavicon
hook returns:
Prop | Type | Default | Description |
---|---|---|---|
url |
string |
'' |
Current favicon URL |
update |
(src: string) => void |
- | Update the favicon |
restore |
() => void |
- | Restore the favicon |
useFavicon
hook options:
Prop | Type | Default | Description |
---|---|---|---|
selectors |
string |
"link[rel*='icon']" |
Valid CSS selector(s) |
pnpm install
pnpm build
pnpm test
Within demo
directory:
pnpm install
pnpm start
MIT