遇到的坑:webview和h5通讯时,会有一些延迟致使不能当即生效
具体描述:在使用react-native时,须要加载外部网页,加载后,RN提供一个按钮能够关闭网页,但若是打开的是内部网页就须要隐藏这个按钮,h5代码使用react写的,在componentDidMount时,发送postmessage给客户端(RN),此时发现收不到,查阅react-native官方文档后得已解决。
解决过程:
https://github.com/facebook/r...,解释了为何要延迟
https://github.com/react-nati... 解释了,升级后的webview为何postmessage不能直接用react
import React from 'react'; import { WebView } from 'react-native'; export default class myComponent extends React.Component<any, any> { public hide(){ // 隐藏按钮的逻辑 // 创建一个白名单,在白名单里的域名隐藏按钮,以外的不作处理 } public render(): React.ReactNode { const { navigation } = this.props; const { state } = navigation; const { params } = state; return <WebView ref={'webview'} source={{ uri: params.url }} onLoadEnd={this.hide} />; } }
感受对你有帮助的话,支持一下哈:git