简体   繁体   English

尝试停止在react-native中加载webView时发生未定义的错误

[英]Undefined error when trying to stop webView loading in react-native

After looking through the react-native docs and various answers on here, I can see: 在浏览了本机文档和各种答案之后,我可以看到:

this.refs[WEBVIEW_REF].stopLoading();

is used to stop the webView from continuing to load. 用于阻止webView继续加载。

I've tried this but always get an: 我已经尝试过了,但是总是得到:

undefined is not an object (evaluating 'this.refs[WEBVIEW_REF]')

My code is as follows and I'm trying to stop the app from loading the help page and launch in a browser rather than the app. 我的代码如下,并且我试图阻止该应用程序加载帮助页面并在浏览器而不是应用程序中启动。

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  View,
  WebView,
  Linking,
} from 'react-native';

const HEADER = '#3b5998';
const BGWASH = 'white';

const WEBVIEW_REF = 'webview';
const DEFAULT_URL = 'https://somesite.com/';

export default class reactNativeApp extends Component {
  state = {
    url: DEFAULT_URL,
    scalesPageToFit: true,
  };
  _onShouldStartLoadWithRequest(e) {
    if (e.url.indexOf('assistenza') >= 0) {
      Linking.openURL(e.url);
      return false
    }

    return true
  }
  _onNavigationStateChange(e) {
    if (e.url.indexOf('assistenza') >= 0) {
      this.refs[WEBVIEW_REF].stopLoading();
      Linking.openURL(e.url);

      return false
    }

    return true
  }
  render() {
    return (
      <View>
        <WebView
          ref={WEBVIEW_REF}
          automaticallyAdjustContentInsets={false}
          style={styles.webView}
          source={{uri: this.state.url}}
          javaScriptEnabled={true}
          domStorageEnabled={true}
          decelerationRate="normal"
          startInLoadingState={true}
          scalesPageToFit={this.state.scalesPageToFit}
          onShouldStartLoadWithRequest={this._onShouldStartLoadWithRequest}
          onNavigationStateChange={this._onNavigationStateChange}
        />
      </View>
    );
  }
}

AppRegistry.registerComponent('reactNativeApp', () => reactNativeApp);

Any thoughts on why this is? 有什么想法为什么呢? Every example I've seen used it :/ 我见过的每个例子都使用它:/

You need to bind the function using either: 您需要使用以下任一方法绑定功能:

constructor (props) {
  super(props)

  this._onNavigationStateChange = this._onNavigationStateChange.bind(this)
}

Or in render : 或在render

onNavigationStateChange={this._onNavigationStateChange.bind(this)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-native 中的 WebView 出现错误“加载页面时遇到错误” - WebView in react-native gets error "Encountering an error loading page" 停止在本机反应中加载 Webview - Stop loading Webview in react native JSON 解析错误:尝试解析数组中的对象时出现意外的标识符“未定义”(React-Native) - JSON Parse error: Unexpected identifier "undefined" when trying to parse an object from an array (React-Native) 尝试将属性传递到 react-native 中的第二个屏幕时出现错误“未定义不是 object(评估 'this.route')” - Error “Undefined is not an object (evaluating 'this.route')” when trying to pass properties to a second screen in react-native 在加载之前,React-native android WebView句柄单击了Url - React-native android WebView handle clicked Url before loading react-native异步获取,未定义错误 - react-native async fetch, undefined error 错误:undefined 不是函数 react-native - Error: undefined is not a function react-native 尝试执行本机应用程序时出现“错误无法读取未定义的属性‘拆分’” - "error Cannot read property 'split' of undefined" in trying to execute react-native app React-Native中的WebView没有显示 - WebView in React-Native is not showing React-Native引用未定义 - React-Native Refs are Undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM