在 App 需求中
若页面需要通过 URL 渲染远端 HTML 页面
若页面资料提供的是 html 字串
在 React Native 该怎麽做呢?
开始来用 react-native-webview 吧!
原本 WebView 是原生的,後来为了减小 React Native 核心包的体积,便将其单独提出到 react-native-webview
npm i react-native-webview
cd ios
pod install
import { WebView } from 'react-native-webview';
const WebViewComponent = props => {
return <WebView source={{uri: props.source}} />;
};
export default WebViewComponent;
这里有个关键要注意!!
WebView 预设的高度是 0,所以若外层没给予高度无法显示画面
所以须於外层包一个元件
View / ScrollView / SafeAreaView
并且给予 style={{flex:1}} 占全满画面
import React from 'react';
import WebViewComponent from '../components/WebViewComponent';
import {View} from 'react-native';
const WebViewScreen = () => {
return (
<View style={{flex: 1}}> //注意!!
<WebViewComponent source="https://paradisemrch.github.io/beka/" />
</View>
);
};
export default WebViewScreen;
npm run ios
btw 要算塔罗可以找卡卡唷!塔罗卡卡 <3 欢迎预约
originWhitelist - 如果用户点击导航到新页面但新页面不在此白名单中,则该 URL 将由操作系统处理。
default 的白名单是 “http:// ” 和 “https:// ”
我们可以自己设定
像是 originWhitelist = { [ ' https:/ /* ' , ' git://* ' ] } />
也或着 originWhitelist={["*"]}
source 换为 html
injectedJavaScript 在加载时注入网页的 js,window.ReactNativeWebView.postMessage(document.documentElement.scrollHeight) 用来侦测内容高度
onMessage webview 调用时调用的函数
深入 props 可以参考此 doc
<WebView
originWhitelist={["*"]}
source={{
html:
`<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>这里是一个标题</p>
</body>
</html>`,
}}
injectedJavaScript="window.ReactNativeWebView.postMessage(document.documentElement.scrollHeight)"
onMessage={onWebViewMessage}
/>
const [webViewHeight, setWebViewHeight] = React.useState(0);
const onWebViewMessage = (event) => {
setWebViewHeight(Number(event.nativeEvent.data));
};
npm run ios
”这里是一个标题“ 就会出现!!
Day 19 done ~~~
<<: Spring Framework X Kotlin Day 29 Observability
>>: 【设计+切版30天实作】|Day20 - Navigation bar - 打破预设的navbar排版
昨天稍微看了一下范例程序码是如何包装API的input+output参数型别 今天就来继续改造原本的...
先来复习一下永丰金流API需要准备的材料(?)有哪些吧~ 其实我们必要的API串接参数都已经准备得差...
传统在做语音辨识需要事先标注每个音框的标签,这麽做会耗费庞大的时间,因 此研究人员 Alex Gra...
今天继续来熟悉Unity介面~(ง •̀_•́)ง‼ 昨天了解了阶层管理区,今天打算接续着看游戏执行...
Intro UVa 一颗星选集 UVa Online Judge (wiki) 为线上自动评断系统,...