React原生路由器flux:覆盖组件内部的左键或右键,并访问本地功能

react native router flux: override left or right button inside component and access local function

本文关键字:右键 访问 功能 flux 路由器 原生 覆盖 内部 组件 React      更新时间:2023-09-26

我能够覆盖组件内的按钮,但不能调用本地函数,例如,当我按下"Refresh"时,什么也没有发生。这是重写按钮文本并执行功能的正确方法吗?谢谢你的帮助。谢谢。

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;