import React, { useState } from 'react'; import { ScrollView, StyleSheet, Text, TouchableOpacity, Platform, Linking } from 'react-native'; import AutoHeightWebView from 'react-native-autoheight-webview'; import { autoHeightHtml0, autoHeightHtml1, autoHeightScript, autoWidthHtml0, autoWidthHtml1, autoWidthScript, autoDetectLinkScript, style0, inlineBodyStyle } from './config'; const Explorer = () => { const [{ widthHtml, heightHtml }, setHtml] = useState(() => ({ widthHtml: autoWidthHtml0, heightHtml: autoHeightHtml0 })); const changeSource = () => setHtml({ widthHtml: widthHtml === autoWidthHtml0 ? autoWidthHtml1 : autoWidthHtml0, heightHtml: heightHtml === autoHeightHtml0 ? autoHeightHtml1 : autoHeightHtml0 }); const [{ widthStyle, heightStyle }, setStyle] = useState(() => ({ heightStyle: null, widthStyle: inlineBodyStyle })); const changeStyle = () => setStyle({ widthStyle: widthStyle == inlineBodyStyle ? style0 + inlineBodyStyle : inlineBodyStyle, heightStyle: heightStyle == null ? style0 : null }); const [{ widthScript, heightScript }, setScript] = useState(() => ({ heightScript: autoDetectLinkScript, widthScript: null })); const changeScript = () => setScript({ widthScript: widthScript !== autoWidthScript ? autoWidthScript : null, heightScript: heightScript !== autoDetectLinkScript ? autoDetectLinkScript : autoHeightScript + autoDetectLinkScript }); const [heightSize, setHeightSize] = useState(() => ({ height: 0, width: 0 })); const [widthSize, setWidthSize] = useState(() => ({ height: 0, width: 0 })); return ( console.log('height on error')} onLoad={() => console.log('height on load')} onLoadStart={() => console.log('height on load start')} onLoadEnd={() => console.log('height on load end')} onShouldStartLoadWithRequest={result => { console.log(result); return true; }} onSizeUpdated={heightSize => setHeightSize(heightSize)} source={{ html: heightHtml }} customScript={heightScript} onMessage={event => { const { data } = event.nativeEvent; let messageData; // maybe parse stringified JSON try { messageData = JSON.parse(data); } catch (e) { console.log(e.message); } if (typeof messageData === 'object') { const { url } = messageData; // check if this message concerns us if (url && url.startsWith('http')) { Linking.openURL(url).catch(error => console.error('An error occurred', error)); } } }} /> height: {heightSize.height}, width: {heightSize.width} console.log('width on error')} onLoad={() => console.log('width on load')} onLoadStart={() => console.log('width on load start')} onLoadEnd={() => console.log('width on load end')} onShouldStartLoadWithRequest={result => { console.log(result); return true; }} onSizeUpdated={widthSize => setWidthSize(widthSize)} source={{ html: widthHtml }} customScript={widthScript} /> height: {widthSize.height}, width: {widthSize.width} change source change style change script ); }; const styles = StyleSheet.create({ button: { marginTop: 15, backgroundColor: 'aliceblue', borderRadius: 5, padding: 5 } }); export default Explorer;