Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Native Webview auto Height not display large content in flatlist [Android] #241

Open
ViktorOsadchyi opened this issue Aug 29, 2022 · 12 comments
Assignees
Labels

Comments

@ViktorOsadchyi
Copy link

ViktorOsadchyi commented Aug 29, 2022

Bug description:
We use multiple webview components on one screen along with native listings in flatlist.
On iOS, there are no problems with display and performance. However, on Android, we encountered a crash when displaying several webview components. This issue was fixed by adding renderToHardwareTextureAndroid, androidHardwareAccelerationDisabled and style={{ opacity: 0.99 }}

We also noticed that large content is no longer displayed if the webview component is inside a flatlist
If you use androidHardwareAccelerationDisabled={false}, the content is sometimes displayed, but the behavior gets even weirder because sometimes the content still disappears.
At the same time, the height of the component is correctly calculated and the scrollbar looks correct, but instead of content, there is emptiness

Source:

const content = `
<!DOCTYPE html>
<html>
  <body style="margin: 0; padding: 0;">
    <div>
        <a  href="https://reactnative.dev/"></a>
        <img style="width: 350px; height: 300px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1200px-React-icon.svg.png" />
    </div>
    <div>
        <a  href="https://reactnative.dev/"></a>
        <img style="width: 350px; height: 300px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1200px-React-icon.svg.png" />
    </div>
    <div>
        <a  href="https://reactnative.dev/"></a>
        <img style="width: 350px; height: 300px;" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1200px-React-icon.svg.png" />
    </div>
  </body>
</html>
`
const WebView = () => {
  const bottomComponent = () => (
      <AutoHeightWebView
        cacheEnabled={true}
        onLoad={handleOnLoad}
        automaticallyAdjustContentInsets={true}
        scrollEnabled={false}
        source={{ html: content }}
        renderToHardwareTextureAndroid={true}
        androidHardwareAccelerationDisabled
        androidLayerType={
          Platform.OS === 'android' && Platform.Version <= 22
            ? 'hardware'
            : 'none'
        }
        style={{ opacity: 0.99 }}
      />
  )

  return (
    <FlatList
      contentContainerStyle={{ flexGrow: 1, paddingVertical: 1 }}
      data={[]}
      renderItem={() => <></>}
      keyExtractor={(item) => item.id.toString()}
      showsHorizontalScrollIndicator={false}
      ListFooterComponent={bottomComponent}
    />
  )
}

Screenshots/Videos:
Screenshot 2022-08-29 at 17 02 57

Environment:

  • OS: Android
  • OS version: 9, 10, 11, 12
  • react-native version: 0.67.3
  • react-native-webview version: 11.17.2
  • react-native-autoheight-webview version: 1.6.1
@alekszavialov
Copy link

same issue, please check it

@iou90
Copy link
Owner

iou90 commented Sep 6, 2022

@ViktorOsadchyi
Will style={{ opacity: 0.99 }} fix the emptiness issue?
And if this issue can also reproduce with react-native-webview ?
FYI: react-native-webview/react-native-webview#1915

@ViktorOsadchyi
Copy link
Author

@iou90 I used style={{ opacity: 0.99 }} and it didn't work for me.
I think the problem is that I am using a webview with high height content inside a flatlist

I have never been able to find a solution to this problem with this library.
I tried using react-native-webview and it worked for me, however I had to calculate the height of the webview content myself

@iou90
Copy link
Owner

iou90 commented Sep 7, 2022

@ViktorOsadchyi
I can not reproduce this issue with:
OS version: Android 12 emulator
react-native version: 0.69.1
react-native-webview version: 11.22.7
react-native-autoheight-webview version: 1.6.4

Can you test with the same conditions as above?

@andreialecu
Copy link

andreialecu commented Sep 22, 2022

Just a heads up. I'm seeing the same issue on Android, although we're using https://github.com/formidable-webview/webshell for autoheight, which works similar to this library.

The issue is most likely upstream in react-native-webview.

I see this in Flipper which may be relevant:

Screenshot 2022-09-22 at 10 54 56

Possibly related: https://stackoverflow.com/questions/62080137/android-webview-does-not-display-web-page-even-though-the-page-has-successfully

@andreialecu
Copy link

Opened react-native-webview/react-native-webview#2683

@ViktorOsadchyi
Copy link
Author

I solved my problem using the react-native-webview library, on which react-native-autoheight-webview is built
However, I had to write my own script to calculate the height and embed it in injectedJavaScript

It seems that this solved the problem and now the content is always displayed.

@andreialecu
Copy link

andreialecu commented Sep 23, 2022

@ViktorOsadchyi this issue may not always reproduce. It's possible something you're doing makes it less likely to occur, but it will still occur in other cases. I'm doing some manual height adjustments as well and not using this library, and I have a case where I can always trigger this bug.

@afonso-tsx
Copy link

i fixed this with removeClippedSubviews={false} on flatlist, and overflow: 'hidden' on the webview style

@choijiho0021
Copy link

DON'T USE THIS LIBRARY.

Lib can be replaced by WebView injectedJavaScript.

스크린샷 2023-06-22 오전 10 23 48

스크린샷 2023-06-22 오전 10 47 15

const webViewScript = `
  setTimeout(function() {
    window.ReactNativeWebView.postMessage(document.documentElement.scrollHeight);
  }, 300);
  true; // note: this is required, or you'll sometimes get silent failures
`;


<WebView
    style={ {height}}
    originWhitelist={['*']}
    source={{
      html,
    }}
    onMessage={(event) => {
      console.log('event : ', event);
      setHeight(parseInt(event.nativeEvent.data));
    }}
    scrollEnabled
    injectedJavaScript={webViewScript}
    javaScriptEnabled
  />

It automatically adjusts to fit the HTML content height.

For more information, please refer to the following.
https://github.com/react-native-webview/react-native-webview/blob/master/docs/Guide.md#communicating-between-js-and-native

@Bellarose143
Copy link

Bellarose143 commented Jul 7, 2023

@choijiho0021

Your comment did not work either. It does do SOMETHING but it is not accurate at all. It's not your fault, but the height value is wrong.

@omkar2737
Copy link

@choijiho0021 this worked like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants