Handle division by zero in the hook

This commit is contained in:
Dan Abramov
2024-11-01 03:00:29 +00:00
parent 79a61adef7
commit c95f8e8659
4 changed files with 17 additions and 17 deletions
@@ -52,7 +52,7 @@ const ImageItem = ({
isScrollViewBeingDragged,
}: Props) => {
const [isScaled, setIsScaled] = useState(false)
const imageDimensions = useImageDimensions({
const [_aspectRatio, imageDimensions] = useImageDimensions({
src: imageSrc.uri,
knownDimensions: imageSrc.dimensions,
})
@@ -47,7 +47,7 @@ const ImageItem = ({
const scrollViewRef = useAnimatedRef<Animated.ScrollView>()
const translationY = useSharedValue(0)
const [scaled, setScaled] = useState(false)
const imageDimensions = useImageDimensions({
const [_aspectRatio, imageDimensions] = useImageDimensions({
src: imageSrc.uri,
knownDimensions: imageSrc.dimensions,
})
@@ -182,7 +182,7 @@ const styles = StyleSheet.create({
})
const getZoomRectAfterDoubleTap = (
imageDimensions: ImageDimensions | null,
imageDimensions: ImageDimensions | undefined,
touchX: number,
touchY: number,
): {
+2 -10
View File
@@ -21,12 +21,11 @@ function useImageAspectRatio({
src: string
knownDimensions: Dimensions | null
}) {
const dims = useImageDimensions({src, knownDimensions})
const [raw] = useImageDimensions({src, knownDimensions})
let constrained: number | undefined
let max: number | undefined
let isCropped: boolean | undefined
if (dims !== undefined) {
const raw = calc(dims)
if (raw !== undefined) {
const ratio = 1 / 2 // max of 1:2 ratio in feeds
constrained = Math.max(raw, ratio)
max = Math.max(raw, 0.25) // max of 1:4 in thread
@@ -227,10 +226,3 @@ export function AutoSizedImage({
)
}
}
function calc(dim: Dimensions) {
if (dim.width === 0 || dim.height === 0) {
return 1
}
return dim.width / dim.height
}