React Native - open links in browser

前端 未结 13 714
遇见更好的自我
遇见更好的自我 2020-12-04 11:59

Hi i am using react native\'s webview to display some html, i want that whenever a user clicks a link inside that html, it will open the user\'s browser with that link.

13条回答
  •  生来不讨喜
    2020-12-04 12:46

     {
            this.webView.ref = webView;
          }}
          onNavigationStateChange={(navState) => {
            this.webView.canGoBack = navState.canGoBack;
            if (!navState.url.includes('yourdomain.com')) {
              Linking.openURL(navState.url);
              return false;
            }
          }}
          onShouldStartLoadWithRequest={(event) => {
            if (!event.url.includes('yourdomain.com')) {
              Linking.openURL(event.url);
              return false;
            }
            return true;
          }}
          style={{flex: 1}}
          startInLoadingState={true}
          renderLoading={() => {
            return (
              
                
              
            );
          }}
        />
    

    I struggled with this one, my use case was to open external links in a separate browser. This solution worked for me.

提交回复
热议问题