简体   繁体   中英

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

I was able to override the button inside the compononent but not able to call the local function, for example when I press "Refresh", nothing happen. Is this the correct way to override the button text and perform function. Your help is appreciate. Thanks.

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;

I get the answer after some research, dont use static, use 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM