Skip to content

Commit 171e900

Browse files
thibmaekGertjanReynaert
authored andcommitted
Add the option to pass headers to useImageDimensions
1 parent 5c8506a commit 171e900

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/useImageDimensions.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useEffect, useState} from 'react'
2-
import {Image, ImageRequireSource} from 'react-native'
2+
import {Image, ImageRequireSource, ImageURISource} from 'react-native'
33

44
export interface URISource {
55
uri: string
@@ -21,10 +21,12 @@ export interface ImageDimensionsResult {
2121

2222
/**
2323
* @param source either a remote URL or a local file resource.
24+
* @param headers headers to be passed to a remote URL resource.
2425
* @returns original image dimensions (width, height and aspect ratio).
2526
*/
2627
export function useImageDimensions(
2728
source: ImageDimensionsSource,
29+
headers?: ImageURISource['headers']
2830
): ImageDimensionsResult {
2931
const [result, setResult] = useState<ImageDimensionsResult>({loading: true})
3032

@@ -43,16 +45,29 @@ export function useImageDimensions(
4345

4446
if (typeof source === 'object' && source.uri) {
4547
setResult({loading: true})
46-
47-
Image.getSize(
48-
source.uri,
49-
(width, height) =>
50-
setResult({
51-
dimensions: {width, height, aspectRatio: width / height},
52-
loading: false,
53-
}),
54-
(error) => setResult({error, loading: false}),
55-
)
48+
49+
if (typeof headers === 'object') {
50+
Image.getSizeWithHeaders(
51+
source.uri,
52+
headers,
53+
(width, height) =>
54+
setResult({
55+
dimensions: {width, height, aspectRatio: width / height},
56+
loading: false,
57+
}),
58+
(error) => setResult({error, loading: false}),
59+
)
60+
} else {
61+
Image.getSize(
62+
source.uri,
63+
(width, height) =>
64+
setResult({
65+
dimensions: {width, height, aspectRatio: width / height},
66+
loading: false,
67+
}),
68+
(error) => setResult({error, loading: false}),
69+
)
70+
}
5671

5772
return
5873
}

0 commit comments

Comments
 (0)