Newbie009 Newbie009 - 24 days ago 16
Javascript Question

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;

Answer

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;