Check cache early

This commit is contained in:
Dan Abramov
2024-11-01 01:42:24 +00:00
parent 9b076032cc
commit fc0f7bbf9c
+6 -5
View File
@@ -21,9 +21,10 @@ export function useImageAspectRatio({
src: string src: string
dimensions: Dimensions | undefined dimensions: Dimensions | undefined
}) { }) {
const [raw, setAspectRatio] = React.useState( const [raw, setAspectRatio] = React.useState(() => {
dimensions ? calc(dimensions) : undefined, const dims = dimensions ?? imageSizes.get(src)
) return dims ? calc(dims) : undefined
})
let constrained: number | undefined let constrained: number | undefined
let max: number | undefined let max: number | undefined
@@ -37,7 +38,7 @@ export function useImageAspectRatio({
React.useEffect(() => { React.useEffect(() => {
let aborted = false let aborted = false
if (dimensions) return if (raw !== undefined) return
imageSizes.fetch(src).then(newDim => { imageSizes.fetch(src).then(newDim => {
if (aborted) return if (aborted) return
setAspectRatio(calc(newDim)) setAspectRatio(calc(newDim))
@@ -45,7 +46,7 @@ export function useImageAspectRatio({
return () => { return () => {
aborted = true aborted = true
} }
}, [dimensions, setAspectRatio, src]) }, [raw, setAspectRatio, src])
return { return {
dimensions, dimensions,