From 369dac2e9efd03320c2f347a85807621136ac815 Mon Sep 17 00:00:00 2001 From: James Pearson Date: Thu, 10 Jan 2019 13:14:45 +0000 Subject: [PATCH 1/3] Added onMessage as a defaultProp to ensure there is a function there --- index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3c52a68..a70f48a 100644 --- a/index.js +++ b/index.js @@ -38,9 +38,14 @@ export default class MyWebView extends Component { webViewHeight: Number }; + static propTypes = { + onMessage: PropTypes.func + }; + static defaultProps = { - autoHeight: true, - } + autoHeight: true, + onMessage: () => {} + }; constructor (props: Object) { super(props); From 19adfe02d68b217b19de6b560b0eb24ddcef1c07 Mon Sep 17 00:00:00 2001 From: James Pearson Date: Thu, 10 Jan 2019 13:15:34 +0000 Subject: [PATCH 2/3] Reordered onMessage to be after the spread of props (prevents overwriting) --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a70f48a..4508d20 100644 --- a/index.js +++ b/index.js @@ -81,11 +81,11 @@ export default class MyWebView extends Component { ref={(ref) => { this.webview = ref; }} injectedJavaScript={Platform.OS === 'ios' ? iosScript : androidScript} scrollEnabled={this.props.scrollEnabled || false} - onMessage={this._onMessage} javaScriptEnabled={true} automaticallyAdjustContentInsets={true} {...this.props} - style={[{width: _w}, this.props.style, {height: _h}]} + onMessage={this._onMessage} + style={[{ width: _w }, this.props.style, { height: _h }]} /> ) } From 98a508639069d5ef9a00a85f1781ba903c647687 Mon Sep 17 00:00:00 2001 From: James Pearson Date: Thu, 10 Jan 2019 13:16:04 +0000 Subject: [PATCH 3/3] Call the pased onMessage after dealing with the height of the webview --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index 4508d20..c69b669 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ import { WebView, Platform, } from 'react-native'; +import PropTypes from "prop-types"; const injectedScript = function() { function waitForBridge() { @@ -57,9 +58,11 @@ export default class MyWebView extends Component { } _onMessage(e) { + const { onMessage } = this.props; this.setState({ webViewHeight: parseInt(e.nativeEvent.data) }); + onMessage(e); } stopLoading() {