【问题标题】:Undefined error when trying to stop webView loading in react-native尝试在 react-native 中停止 webView 加载时出现未定义的错误
【发布时间】:2017-05-22 09:05:29
【问题描述】:

在这里查看 react-native 文档和各种答案后,我可以看到:

this.refs[WEBVIEW_REF].stopLoading();

用于阻止 webView 继续加载。

我试过了,但总是得到:

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

我的代码如下,我试图阻止应用加载帮助页面并在浏览器而不是应用中启动。

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);

有什么想法吗?我见过的每个例子都使用它:/

【问题讨论】:

    标签: javascript android webview react-native react-native-android


    【解决方案1】:

    您需要使用以下任一方式绑定函数:

    constructor (props) {
      super(props)
    
      this._onNavigationStateChange = this._onNavigationStateChange.bind(this)
    }
    

    或者在render:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 2020-01-16
      • 2021-05-31
      • 2019-02-16
      • 2014-06-24
      • 2021-02-06
      • 1970-01-01
      相关资源
      最近更新 更多