Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 87a4a9c

Browse files
committed
Fix prop change on every render
1 parent 9f07567 commit 87a4a9c

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/index.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ export interface ReactNativeImageCacheHocProps {
4444
fileHostWhitelist?: string[]
4545
}
4646

47-
export interface ReactNativeImageCacheHocState {
48-
loadedAt: number
49-
localFilePath: string | null
47+
interface ReactNativeImageCacheHocState {
48+
source?: {
49+
uri?: string
50+
}
5051
}
5152

5253
export interface ReactNativeImageCacheHocOptions {
@@ -161,8 +162,7 @@ const imageCacheHoc = <P extends object>(
161162

162163
// Set initial state
163164
this.state = {
164-
loadedAt: 0,
165-
localFilePath: null,
165+
source: undefined,
166166
}
167167

168168
// Assign component unique ID for cache locking.
@@ -290,7 +290,7 @@ const imageCacheHoc = <P extends object>(
290290
.subscribe((info) => this.onSourceLoaded(info))
291291
}
292292
} else {
293-
this.setState({ localFilePath: null })
293+
this.setState({ source: undefined })
294294
}
295295
}
296296

@@ -306,7 +306,13 @@ const imageCacheHoc = <P extends object>(
306306
}
307307

308308
onSourceLoaded({ path }: CacheFileInfo) {
309-
this.setState({ loadedAt: Date.now(), localFilePath: path })
309+
this.setState({
310+
source: path
311+
? {
312+
uri: path + (Platform.OS === 'android' ? '?' + Date.now() : ''),
313+
}
314+
: undefined,
315+
})
310316
this.invalidUrl = path === null
311317

312318
if (path && this.props.onLoadFinished) {
@@ -320,16 +326,13 @@ const imageCacheHoc = <P extends object>(
320326

321327
render() {
322328
// If media loaded, render full image component, else render placeholder.
323-
if (this.state.localFilePath) {
329+
if (this.state.source) {
324330
// Android caches images in memory, if we are rendering the image should have changed locally so appending a timestamp to the path forces it to be loaded from disk
325331
// The internals of te Android behaviour have not been investigated but perhaps it would be beneficial to use the last modified date instead
326-
const props = Object.assign({}, this.props, {
327-
source: {
328-
uri:
329-
this.state.localFilePath +
330-
(Platform.OS === 'android' ? '?' + this.state.loadedAt : ''),
331-
},
332-
})
332+
const props = {
333+
...this.props,
334+
source: this.state.source,
335+
}
333336

334337
return <Wrapped key={this.componentId} {...(props as P)} />
335338
} else {

tests/CacheableImage.test.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ describe('CacheableImage', function () {
177177
// Ensure defaults set correctly
178178
cacheableImage.props.should.have.properties(mockData.mockCacheableImageProps)
179179
cacheableImage.state.should.have.properties({
180-
localFilePath: null,
180+
source: undefined,
181181
})
182182
cacheableImage.options.should.have.properties({
183183
validProtocols: ['https'],
@@ -321,7 +321,11 @@ describe('CacheableImage', function () {
321321
'file:///base/file/path/react-native-image-cache-hoc/d3b74e9fa8248a5805e2dcf17a8577acd28c089b.png',
322322
})
323323

324-
wrapper.setState({ localFilePath: './test.jpg' })
324+
wrapper.setState({
325+
source: {
326+
uri: './test.jpg',
327+
},
328+
})
325329

326330
expect(wrapper.prop('source')).toStrictEqual({
327331
uri: './test.jpg',

0 commit comments

Comments
 (0)