reactjs - 从 Webview 获取和传递 React Native 中的变量
问题描述
我在我的项目 React Native 中使用 [react-native-webview][1]。我的 webview 有一个 javascript 数组,我需要获取这个数组并在一个菜单中显示值,就像这个幻灯片菜单:https://raw.githubusercontent.com/instea/react-native-popup-menu/master/android.demo .gif
代码:
function WebViewPage(){
const html = `
<html>
<body>
<div id="reportContainer">here apear the content..</div>
<script>
var reportContainer = document.getElementById('reportContainer');
var pages = [];
reportContainer.getPages().then(function (reportPages) {
//HERE RETURN A LIST FROM PAGES FROM MY REPORT..
pages = reportPages;
});
</script>
</body>
</html>
`
return(
<Page>
<WebView source={{html:html}} />
</Page>
);
}
export default WebViewPage;
基本上我需要获取页面数组并在我的标题处放置菜单。我正在考虑使用钩子,但我不知道如何获得这个数组,有什么想法吗?谢谢你。[1]:https ://github.com/react-native-community/react-native-webview
解决方案
您可以在组件上使用onMessage
回调函数。webview
function WebViewPage(){
const html = `
<html>
<body>
<div id="reportContainer">here apear the content..</div>
<script>
var reportContainer = document.getElementById('reportContainer');
var pages = [];
reportContainer.getPages().then(function (reportPages) {
//HERE RETURN A LIST FROM PAGES FROM MY REPORT..
pages = reportPages;
window.postMessage(pages);
});
</script>
</body>
</html>
`
const onMessage(data) {
console.log(data);
}
return(
<Page>
<WebView onMessage={onMessage} source={{html:html}} />
</Page>
);
}
export default WebViewPage;
推荐阅读
- asp.net-core - Serilog 未登录到控制台
- sql - PostgreSQL 更新 + 加入一个额外的 WHERE 子句
- c - 需要帮助来理解此代码的 Big-O 复杂性的答案
- python - 使用 Python 或任何可能的列表按 ID 查找自治系统位置和名称
- java - Spring找不到bean
- react-native - 在navigation.goBack()时反应原生如何重新渲染routeHeader
- jenkins - Jenkins 无法将 URL 集识别为 Katalon 全局变量
- c++ - 在cpp中查找1到n范围内的重复/缺失数字
- architecture - 用于与自定义应用程序集成以进行工作流管理的无头 BPM 解决方案
- python - 如果它们出现在模式之后,是否有一个正则表达式可以只获取字符串中的两个数字?