Open CDBridger opened 2 years ago
I believe this is a patch that fixes the issue for me. I'll have to do further testing
diff --git a/node_modules/react-native-render-html/src/elements/useIMGElementState.ts b/node_modules/react-native-render-html/src/elements/useIMGElementState.ts
index 6590d21..834499d 100644
--- a/node_modules/react-native-render-html/src/elements/useIMGElementState.ts
+++ b/node_modules/react-native-render-html/src/elements/useIMGElementState.ts
@@ -78,7 +78,7 @@ function useFetchedNaturalDimensions(props: {
if (source.uri && !hasCachedDimensions) {
getImageSizeAsync({ uri: source.uri, headers: source.headers })
.then((dimensions) => !cancelled && onNaturalDimensions(dimensions))
- .catch((e) => !cancelled && onError(e || {}));
+ .catch((e) => !cancelled && onError(e?.message || {}));
return () => {
cancelled = true;
};
EDIT: Nope, nvm doesn't work. this patch then jsut solves the error causing infinite re-rendering, but exposes infinite re-rendering due to dimension object causing infinite re-rendering
Ok this new patch seems to have done the trick.
diff --git a/node_modules/react-native-render-html/src/elements/useIMGElementState.ts b/node_modules/react-native-render-html/src/elements/useIMGElementState.ts
index 6590d21..f4d6e16 100644
--- a/node_modules/react-native-render-html/src/elements/useIMGElementState.ts
+++ b/node_modules/react-native-render-html/src/elements/useIMGElementState.ts
@@ -41,24 +41,33 @@ function useImageNaturalDimensions<P extends UseIMGElementStateProps>(props: {
? ImageDimensions
: ImageDimensions | null
>((cachedNaturalDimensions as any) || null);
+ const [naturalHeight, setNaturalHeight] = useState<number| null>();
+ const [naturalWidth, setNaturalWidth] = useState<number| null>();
+
const { width: cachedNaturalWidth, height: cachedNaturalHeight } =
cachedNaturalDimensions || {};
const [error, setError] = useState<null | Error>(null);
useEffect(
function resetOnURIChange() {
- setNaturalDimensions(
- (cachedNaturalWidth != null && cachedNaturalHeight != null
- ? { width: cachedNaturalWidth, height: cachedNaturalHeight }
- : null) as any
- );
+ // setNaturalDimensions(
+ // (cachedNaturalWidth != null && cachedNaturalHeight != null
+ // ? { width: cachedNaturalWidth, height: cachedNaturalHeight }
+ // : null) as any
+ // );
+ setNaturalHeight(cachedNaturalHeight)
+ setNaturalWidth(cachedNaturalWidth)
setError(null);
},
[cachedNaturalHeight, cachedNaturalWidth, source.uri]
);
return {
onNaturalDimensions: setNaturalDimensions,
+ onNaturalHeight: setNaturalHeight,
+ onNaturalWidth: setNaturalWidth,
onError: setError,
naturalDimensions,
+ naturalHeight,
+ naturalWidth,
error
};
}
@@ -69,7 +78,7 @@ function useFetchedNaturalDimensions(props: {
specifiedDimensions: IncompleteImageDimensions;
}) {
const { source, cachedNaturalDimensions } = props;
- const { error, naturalDimensions, onError, onNaturalDimensions } =
+ const { error, onError, onNaturalDimensions, onNaturalHeight, onNaturalWidth, naturalHeight, naturalWidth } =
useImageNaturalDimensions(props);
const hasCachedDimensions = !!cachedNaturalDimensions;
useEffect(
@@ -77,8 +86,8 @@ function useFetchedNaturalDimensions(props: {
let cancelled = false;
if (source.uri && !hasCachedDimensions) {
getImageSizeAsync({ uri: source.uri, headers: source.headers })
- .then((dimensions) => !cancelled && onNaturalDimensions(dimensions))
- .catch((e) => !cancelled && onError(e || {}));
+ .then((dimensions) => { if (!cancelled) { onNaturalHeight(dimensions.height); onNaturalWidth(dimensions.width) }})
+ .catch((e) => !cancelled && onError(e?.message || {}));
return () => {
cancelled = true;
};
@@ -87,16 +96,19 @@ function useFetchedNaturalDimensions(props: {
[
source.uri,
source.headers,
- onNaturalDimensions,
+ onNaturalHeight,
+ onNaturalWidth,
onError,
hasCachedDimensions
]
);
return {
- naturalDimensions,
+ naturalHeight,
+ naturalWidth,
error,
onError,
- onNaturalDimensions
+ onNaturalHeight,
+ onNaturalWidth
};
}
@@ -126,14 +138,14 @@ export default function useIMGElementState(
specifiedDimensions,
source
});
- const { naturalDimensions, onError, error } = useFetchedNaturalDimensions({
+ const { naturalHeight, naturalWidth, onError, error } = useFetchedNaturalDimensions({
source: nomalizedSource,
specifiedDimensions,
cachedNaturalDimensions
});
const concreteDimensions = useImageConcreteDimensions({
flatStyle,
- naturalDimensions,
+ naturalDimensions: naturalHeight && naturalWidth ? {width: naturalWidth, height: naturalHeight} : null,
specifiedDimensions,
computeMaxWidth,
contentWidth
I guess it's because even though the dimensions were the same, object equality wasn't, causing a rerender to trigger somewhere. Same with the error
@CDBridger Thanks for the thorough investigation, I'll try to provide a fix soon
Decision Table
<yyy>
is not rendered”Good Faith Declaration
Description
When using this code
The component constantly re renders if the URL fails to load. You can see the output of WhyDidYouRender.js here and this snippet is passed to my console continuously.
I don't get this if I use the userInternalRenderer route, but the reason I was changing to this style was So I could get at the default Props for the image component used to load a placeholder image if the image fails to load like in this example. Obviously I can't have the component infinitely re rendering though.
React Native Information
RNRH Version
6.3.4
Tested Platforms
Reproduction Platforms
Minimal, Reproducible Example
See code above
Additional Notes
No response