Skip to content

Commit 50941ed

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 4c9cee7 commit 50941ed

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

+6-13
Original file line numberDiff line numberDiff line change
@@ -191,24 +191,18 @@ const Image: React.AbstractComponent<
191191
}
192192
}
193193

194-
const [state, updateState] = React.useState(() => {
195-
const uri = resolveAssetUri(source);
196-
if (uri != null) {
197-
const isLoaded = ImageLoader.has(uri);
198-
if (isLoaded) {
199-
return LOADED;
200-
}
201-
}
202-
return IDLE;
203-
});
204-
194+
const [state, updateState] = React.useState(IDLE);
205195
const [layout, updateLayout] = React.useState({});
206196
const hasTextAncestor = React.useContext(TextAncestorContext);
207197
const hiddenImageRef = React.useRef(null);
208198
const filterRef = React.useRef(_filterId++);
209199
const requestRef = React.useRef(null);
200+
const uri = resolveAssetUri(source);
201+
const isCached = uri != null && ImageLoader.has(uri);
210202
const shouldDisplaySource =
211-
state === LOADED || (state === LOADING && defaultSource == null);
203+
state === LOADED ||
204+
isCached ||
205+
(state === LOADING && defaultSource == null);
212206
const [flatStyle, _resizeMode, filter, tintColor] = getFlatStyle(
213207
style,
214208
blurRadius,
@@ -261,7 +255,6 @@ const Image: React.AbstractComponent<
261255
}
262256

263257
// Image loading
264-
const uri = resolveAssetUri(source);
265258
React.useEffect(() => {
266259
abortPendingRequest();
267260

0 commit comments

Comments
 (0)