Skip to content

Commit

Permalink
refactor(react-components): reduce JSON.parse()
Browse files Browse the repository at this point in the history
This patch uses fontFaces.map() to reduce JSON.parse().
  • Loading branch information
taylrj committed Aug 4, 2022
1 parent 04a9e21 commit 00b9504
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions packages/react-components/src/hook/use-font-face-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import FontFaceObserver from 'fontfaceobserver-es'

const useFontFaceObserver = (fontFaces = [], callback) => {
const [isResolved, setIsResolved] = useState(false)
const fontFacesString = JSON.stringify(fontFaces)

useEffect(() => {
const promises = JSON.parse(fontFacesString).map(
({ family, weight, style, stretch }) =>
new FontFaceObserver(family, {
weight,
style,
stretch,
}).load('測試文字')
const promises = fontFaces.map(({ family, weight, style, stretch }) =>
new FontFaceObserver(family, {
weight,
style,
stretch,
}).load('測試文字')
)

Promise.all(promises)
Expand All @@ -28,7 +26,7 @@ const useFontFaceObserver = (fontFaces = [], callback) => {
console.error(`An error occurred during font loading`)
console.log(e)
})
}, [fontFacesString, callback])
}, [fontFaces, callback])

return isResolved
}
Expand Down

0 comments on commit 00b9504

Please sign in to comment.