首页 > 解决方案 > 为什么反应原生的WebView没有打开?

问题描述

使用@react-native-community/react-native-webview 包(v^8.1.2)在 RN v0.61.5 项目中打开 webview,无论我做什么,一旦按钮是,我无法显示 webview按下。我有一个打开它的按钮,但没有任何反应。执行的道具中没有任何错误功能,什么都没有。

这是设置:

<Components.Button
    style={{ marginLeft: 15 }}
    text={"It's Copied. Continue"}
    type={'primary'}
    onPress={() => (
        <WebView
            style={{ flex: 0, height: height, width: width }}
            containerStyle={{ flex: 0, height: height, width: width }}
            automaticallyAdjustContentInsets={true}
            ref={(ref) => (this.webview = ref)}
            source={{ uri: 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=<....>&scope=read_write' }}
            originWhitelist={['https://*']}
            renderError={(error) => <View style={{ flex: 1 }}><Text>{error}</Text></View>}
            onError={syntheticEvent => {
                const { nativeEvent } = syntheticEvent;
                console.warn('WebView error: ', nativeEvent);
            }}
            onNavigatorStateChange={(event) => {
                if (event.url !== this.props.stripeUri) {
                    this.webview.stopLoading();
                    Linking.openURL(event.url);
                }
            }}
        />
    )}
/>

如您所见,我尝试过的事情:

控制台中没有错误注册,没有新的错误视图呈现。

上次我查看这段代码时它工作正常,不知道发生了什么。有什么想法吗?

编辑:小吃链接:https ://snack.expo.io/uTkqnGbny

标签: reactjsreact-nativewebviewwkwebview

解决方案


这是我的零食代码

https://snack.expo.io/sfDcMtiIR

代码:

import * as React from "react";
import {
  Text,
  View,
  StyleSheet,
  TouchableOpacity,
  Linking,
  Dimensions,
} from "react-native";
import { WebView } from "react-native-webview";
import Constants from "expo-constants";
const { height, width } = Dimensions.get("window");
const testURI = "https://google.com";

export default function App() {
  const [isShowWebView, setIsShowWebView] = React.useState(false);

  return (
    <View style={styles.container}>
      <TouchableOpacity
        style={{
          height: 50,
          width: "100%",
          borderRadius: 50,
          justifyContent:"center",
          alignItems:"center"
        }}
        onPress={() => setIsShowWebView(!isShowWebView)} >
        <Text>Open Webview</Text>
      </TouchableOpacity>

      {isShowWebView && (
        <WebView
          style={{ height: height, width: width }}
          containerStyle={{ height: height, width: width }}
          ref={(ref) => (this.webview = ref)}
          source={{ uri: testURI }}
          renderError={(error) => (
            <View style={{ flex: 1 }}>
              <Text>{error}</Text>
            </View>
          )}
          onError={(syntheticEvent) => {
            const { nativeEvent } = syntheticEvent;
            console.warn("WebView error: ", nativeEvent);
          }}
          onNavigatorStateChange={(event) => {
            if (event.url !== testURI) {
              this.webview.stopLoading();
              Linking.openURL(event.url);
            }
          }}
        />
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: Constants.statusBarHeight,
    backgroundColor: "#ecf0f1",
    padding: 8,
  },
});


推荐阅读