React-Native: Go back on android hardware back button pressed
<p>I am trying to add going back on webview when the android backbutton was pressed and I still couldn't manage to make it work.</p>

<p>This is my code:</p>

<pre><code>&lt;WebView
    ref={WEBVIEW_REF}
    source={source}
    domStorageEnabled={true}
    onNavigationStateChange={this.onNavigationStateChange}
/&gt;

componentDidMount() {
    BackAndroid.addEventListener('hardwareBackPress', function() {
        if(this.state.backButtonEnabled) {
            this.refs[WEBVIEW_REF].goBack();
            return true;
        }
    });
};

onNavigationStateChange = (navState) =&gt; {
    this.setState({
        backButtonEnabled: navState.canGoBack,
    });
};
</code></pre>

<p>With the code above I'm getting the error undefined is not an object this.state.backButtonEnabled (which is set in the state).</p>

<p>Than I just wanted to see if the goBack works so I removed the if statement and than I was getting the error undefined is not an object this.refs[WEBVIEW_REF].</p>

<p>What is the best solution for this?</p>