Skip to content

Instantly share code, notes, and snippets.

@jkga
Last active December 2, 2021 10:42
Show Gist options
  • Save jkga/4ee1c366d46a3eba9045f88c58d3bc37 to your computer and use it in GitHub Desktop.
Save jkga/4ee1c366d46a3eba9045f88c58d3bc37 to your computer and use it in GitHub Desktop.
React Native: Make an autoheight WebView inside ScrollView
import WebViewAutoHeight from './WebViewAutoHeight'
import { View, Text } from 'react-native'
import { ScrollView } from 'react-native-gesture-handler'
export default (() => {
return (
<ScrollView>
<View style={{flex: 1}}>
<WebViewAutoHeight html={'<h1>Hi</h1>'}/>
</View>
<View style={{flex: 1}}>
<Text>Hello!</Text>
</View>
</ScrollView>
)
})
import React, { useState } from 'react'
import { Dimensions} from 'react-native'
import { WebView } from 'react-native-webview'
export default (props) => {
const [webViewHeight, setWebViewHeight] = useState(10)
const onWebViewMessage = (event) => {
setWebViewHeight(Number(event.nativeEvent.data))
}
return (
<WebView onMessage={onWebViewMessage} injectedJavaScript='window.ReactNativeWebView.postMessage(document.body.scrollHeight)' source={{html: props.html}} style={{width: Dimensions.get('window').width, height: webViewHeight}} />
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment