Skip to content

Instantly share code, notes, and snippets.

@dbasedow
Last active September 26, 2022 18:55
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save dbasedow/ed6e099823cb8d5ab30e to your computer and use it in GitHub Desktop.
Save dbasedow/ed6e099823cb8d5ab30e to your computer and use it in GitHub Desktop.
Resize WebView to content height in react-native
'use strict';
var React = require('react');
var ReactNative = require('react-native');
var {AppRegistry, Text, WebView, View, Dimensions} = ReactNative;
var WebViewResizing = React.createClass({
getInitialState: function () {
return {
webViewHeight: 100 // default height, can be anything
};
},
render: function () {
var html = '<p><strong>WebView Content</strong></p><ul><li>Foo</li><li>Bar</li><li>Baz</li><\/ul>';
return (
<View style={{paddingTop: 20}}>
<Text>This is above the WebView.</Text>
<WebView
html={html}
injectedJavaScript="document.body.scrollHeight;"
scrollEnabled={false}
onNavigationStateChange={this._updateWebViewHeight}
automaticallyAdjustContentInsets={true}
style={{width: Dimensions.get('window').width, height: this.state.webViewHeight}}/>
<Text>This is below the WebView.</Text>
</View>
);
},
//called when HTML was loaded and injected JS executed
_updateWebViewHeight: function (event) {
//jsEvaluationValue contains result of injected JS
this.setState({webViewHeight: parseInt(event.jsEvaluationValue)});
}
});
AppRegistry.registerComponent('WebViewResizing', () => WebViewResizing);
@liuyimx
Copy link

liuyimx commented Apr 20, 2016

not work for me ,i print the event but no jsEvaluationValue there

@liyabo967
Copy link

jsEvaluationValue is undefined .

@yeegr
Copy link

yeegr commented Jun 10, 2016

This worked at 0.24, once I updated to 0.27 (skipped a few updates), it no longer works. jsEvaluationValue returns 667 for short content, and for longer doc, the returned value falls a bit short, thus cutting off the bottom of the content

@breadadams
Copy link

breadadams commented Oct 31, 2016

Very nice @dbasedow, using this snippet in rn 0.35.0 and working great. However I did have to alter the onNavigationStateChange prop to:

onNavigationStateChange={this._updateWebViewHeight.bind(this)}

Due to a this.setState function doesn't exist error. I guess without it, it's trying to set state inside the WebView component.

@breadadams
Copy link

Not working as nice in android, the above ☝️ was from testing iOS

@woshi82
Copy link

woshi82 commented Nov 23, 2016

yup, there is no jsEvaluationValue props in android

@gmchaturvedi1
Copy link

yup, there is no jsEvaluationValue props in android

@Dielz98
Copy link

Dielz98 commented Feb 11, 2017

not working

@dave-buzzy
Copy link

dave-buzzy commented Mar 23, 2017

jsEvaluationValue in Android - possibly this facebook/react-native#13006 or facebook/react-native#7788?

@amsheehan
Copy link

automaticallyAdjustContentInsets defaults to true

@tijot123
Copy link

I am also searching for this issue on internet for the last 2-3 days and i have got an awesome solution for this ...
https://stackoverflow.com/a/51513193/7760408

@kopax
Copy link

kopax commented Jun 15, 2020

The link is broken

@PabloDinella
Copy link

The link is broken

The right link for searchers like me: https://stackoverflow.com/a/51513193/7760408

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment