编程语言
首页 > 编程语言> > javascript-反应本机路由器流量:覆盖组件内的向左或向右按​​钮并访问本地功能

javascript-反应本机路由器流量:覆盖组件内的向左或向右按​​钮并访问本地功能

作者:互联网

我能够覆盖组件内部的按钮,但无法调用本地函数,例如,当我按下“刷新”时,什么也没发生.这是覆盖按钮文本并执行功能的正确方法吗?感谢您的帮助.谢谢.

import { WebView } from 'react-native'
import React, { PropTypes, Component } from 'react';
import { View, Text } from 'react-native';

import styles from '../src/styles/home'

var WEBVIEW_REF = 'webview';
class WebViewComponent extends Component {

  static rightTitle = "Refresh";
  static onRight() {
    this.reload;
  }

  static propTypes = {
    url: PropTypes.string,
    scalesPageToFit: PropTypes.bool,
    startInLoadingState: PropTypes.bool,
  };

  static defaultProps = {
    url: 'http://google.com',
    scalesPageToFit: true,
    startInLoadingState: true,
  };

  render() {
    return (
      <WebView
          ref={ WEBVIEW_REF }
          style={ { flex: 1, marginTop: 50 } }
          source={ { uri: this.props.url } }
          scalesPageToFit={ this.props.scalesPageToFit }
          startInLoadingState={ this.props.startInLoadingState } />
      );
  }

  reload = () => {
    alert('reload');
  };

}

module.exports = WebViewComponent;

解决方法:

经过一番研究后我得到了答案,不要使用静态的,使用componentDidMount()

import { WebView } from 'react-native'
import React, { PropTypes, Component } from 'react';
import { View, Text } from 'react-native';

import styles from '../src/styles/home'

var WEBVIEW_REF = 'webview';
class WebViewComponent extends Component {

  //here is the answer
  componentDidMount() {
    Actions.refresh({rightTitle: 'Refresh', onRight: this.reload})
  }

  static propTypes = {
    url: PropTypes.string,
    scalesPageToFit: PropTypes.bool,
    startInLoadingState: PropTypes.bool,
  };

  static defaultProps = {
    url: 'http://google.com',
    scalesPageToFit: true,
    startInLoadingState: true,
  };

  render() {
    return (
      <WebView
          ref={ WEBVIEW_REF }
          style={ { flex: 1, marginTop: 50 } }
          source={ { uri: this.props.url } }
          scalesPageToFit={ this.props.scalesPageToFit }
          startInLoadingState={ this.props.startInLoadingState } />
      );
  }

  reload = () => {
    alert('reload');
  };

}

module.exports = WebViewComponent;

标签:jsx,reactjs,react-native,react-native-router-flux,javascript
来源: https://codeday.me/bug/20191118/2024616.html