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

onSizeUpdated will be called after all resources is loaded #170

Open
349989153 opened this issue Jun 11, 2020 · 2 comments
Open

onSizeUpdated will be called after all resources is loaded #170

349989153 opened this issue Jun 11, 2020 · 2 comments
Assignees

Comments

@349989153
Copy link

349989153 commented Jun 11, 2020

onSizeUpdated will be called after all resources is loaded, including <video /> resource, and that will cause a considerable delay.

User will not see anything until the whole <video /> was downloaded.

STEP TO PRODUCE:
Just use
react-native-autoheight-webview to load a html that contains a big <video /> file.

@349989153
Copy link
Author

349989153 commented Jun 12, 2020

I found that the delay was caused by the timing when injectedJavaScript was triggered by react-native-webview.react-native-webview trigger injectedJavaScript when onLoad event was fired, at that time all resources was loaded.

So use injectedJavaScript={script} maybe not the best choice.

My advice is use onLoadProgress event. Like:

const AutoHeightWebView = React.memo(
  forwardRef((props, ref) => {
    ...
    const innerRef = React.useRef(ref);
    ...
    return (
      <WebView
        {...props}
        ref={innerRef}
        onMessage={handleMessage}
        style={[
          styles.webView,
          {
            width,
            height
          },
          style
        ]}
        // injectedJavaScript={script}
        source={currentSource}
        onLoadProgress={({ nativeEvent }) => {
          console.log(nativeEvent.progress);
          console.log(innerRef);
          if (nativeEvent.progress > 0.5 && innerRef.current) {
            // trigger script
            innerRef.current.injectJavaScript(script);
          }
          if (props.onLoadProgress) {
            props.onLoadProgress({ nativeEvent })
          }
        }}
        scrollEnabled={currentScrollEnabled}
      />
    );
  }),
  (prevProps, nextProps) => !shouldUpdate({ prevProps, nextProps })
);

This way will trigger onSizeUpdated much faster

@iou90 iou90 added enhancement and removed bug labels Jul 21, 2020
@iou90
Copy link
Owner

iou90 commented Jul 22, 2020

Please try using onLoadProgress & injectJavaScript on AutoHeightWebView directly.

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

No branches or pull requests

2 participants