-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
useHead.mjs
36 lines (27 loc) · 1.03 KB
/
useHead.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// @ts-check
import { useContext, useEffect } from "react";
import HeadManagerContext from "./HeadManagerContext.mjs";
/**
* A React hook to add document head tags while the component is mounted.
* @param {string} key Head tag fragment key.
* @param {import("react").ReactNode} content Memoized React content containing
* head tags.
* @param {number} [priority] Priority. Defaults to `0`.
*/
export default function useHead(key, content, priority) {
if (typeof key !== "string") {
throw new TypeError("Argument 1 `key` must be a string.");
}
if (priority !== undefined && typeof priority !== "number") {
throw new TypeError("Argument 3 `priority` must be a number.");
}
const headManager = useContext(HeadManagerContext);
if (!headManager) throw TypeError("Context value missing.");
if (typeof Deno !== "undefined") headManager.add(key, content);
useEffect(() => {
headManager.add(key, content, priority);
return () => {
headManager.remove(content);
};
}, [headManager, key, content, priority]);
}