Skip to content

Commit 37264cb

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 8aae58e commit 37264cb

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
@@ -211,24 +211,18 @@ const BaseImage: ImageComponent = React.forwardRef((props, ref) => {
211211
}
212212
}
213213

214-
const [state, updateState] = React.useState(() => {
215-
const uri = resolveAssetUri(source);
216-
if (uri != null) {
217-
const isLoaded = ImageLoader.has(uri);
218-
if (isLoaded) {
219-
return LOADED;
220-
}
221-
}
222-
return IDLE;
223-
});
224-
214+
const [state, updateState] = React.useState(IDLE);
225215
const [layout, updateLayout] = React.useState({});
226216
const hasTextAncestor = React.useContext(TextAncestorContext);
227217
const hiddenImageRef = React.useRef(null);
228218
const filterRef = React.useRef(_filterId++);
229219
const requestRef = React.useRef(null);
220+
const uri = resolveAssetUri(source);
221+
const isCached = uri != null && ImageLoader.has(uri);
230222
const shouldDisplaySource =
231-
state === LOADED || (state === LOADING && defaultSource == null);
223+
state === LOADED ||
224+
isCached ||
225+
(state === LOADING && defaultSource == null);
232226
const [flatStyle, _resizeMode, filter, tintColor] = getFlatStyle(
233227
style,
234228
blurRadius,
@@ -281,7 +275,6 @@ const BaseImage: ImageComponent = React.forwardRef((props, ref) => {
281275
}
282276

283277
// Image loading
284-
const uri = resolveAssetUri(source);
285278
React.useEffect(() => {
286279
abortPendingRequest();
287280

0 commit comments

Comments
 (0)