Last active
December 2, 2021 10:42
-
-
Save jkga/4ee1c366d46a3eba9045f88c58d3bc37 to your computer and use it in GitHub Desktop.
React Native: Make an autoheight WebView inside ScrollView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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