Skip to content

Commit e77deca

Browse files
committed
[fix] Image: image LOADED state is only captured initially
If the Image component is rendered with a `null` source, and consecutively updated with actual source url that was already loaded, it would fail to pic kup the change - `state` would be `IDLE` for a brief moment and this would cause a small flicker when the image renders Let's always start from IDLE state, and update `shouldDisplaySource` condition to be based on `ImageLoader.has` cache or not
1 parent 524d647 commit e77deca

File tree

1 file changed

+6
-13
lines changed
  • packages/react-native-web/src/exports/Image

1 file changed

+6
-13
lines changed

packages/react-native-web/src/exports/Image/index.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,18 @@ const Image: React.AbstractComponent<
205205
}
206206
}
207207

208-
const [state, updateState] = React.useState(() => {
209-
const uri = resolveAssetUri(source);
210-
if (uri != null) {
211-
const isLoaded = ImageLoader.has(uri);
212-
if (isLoaded) {
213-
return LOADED;
214-
}
215-
}
216-
return IDLE;
217-
});
218-
208+
const [state, updateState] = React.useState(IDLE);
219209
const [layout, updateLayout] = React.useState({});
220210
const hasTextAncestor = React.useContext(TextAncestorContext);
221211
const hiddenImageRef = React.useRef(null);
222212
const filterRef = React.useRef(_filterId++);
223213
const requestRef = React.useRef(null);
214+
const uri = resolveAssetUri(source);
215+
const isCached = uri != null && ImageLoader.has(uri);
224216
const shouldDisplaySource =
225-
state === LOADED || (state === LOADING && defaultSource == null);
217+
state === LOADED ||
218+
isCached ||
219+
(state === LOADING && defaultSource == null);
226220
const [flatStyle, _resizeMode, filter, _tintColor] = getFlatStyle(
227221
style,
228222
blurRadius,
@@ -277,7 +271,6 @@ const Image: React.AbstractComponent<
277271
}
278272

279273
// Image loading
280-
const uri = resolveAssetUri(source);
281274
React.useEffect(() => {
282275
abortPendingRequest();
283276

0 commit comments

Comments
 (0)