From 2d6c37ab713ce17e7e9429be0b599a5aa68e30b5 Mon Sep 17 00:00:00 2001 From: Anton Sozontov Date: Thu, 25 Aug 2022 11:04:41 +0300 Subject: [PATCH] Make sure it works with an empty fonts array Currently, it inserts link with src like this: "https://fonts.googleapis.com/css?family=" --- src/index.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index f984289..928ca87 100644 --- a/src/index.js +++ b/src/index.js @@ -12,6 +12,10 @@ const createLink = (fonts, subsets, display) => { ]; }, []).join('|'); + if (families.length === 0) { + return null; + } + const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = `https://fonts.googleapis.com/css?family=${families}`; @@ -31,9 +35,15 @@ const GoogleFontLoader = ({ fonts, subsets, display = null }) => { const [link, setLink] = useState(createLink(fonts, subsets, display)); useEffect(() => { - document.head.appendChild(link); + if (link) { + document.head.appendChild(link); + } - return () => document.head.removeChild(link); + return () => { + if(link) { + document.head.removeChild(link); + } + } }, [link]); useEffect(() => {