RN中webview的一些思考

3次阅读

共计 676 个字符,预计需要花费 2 分钟才能阅读完成。

遇到的坑:webview 和 h5 通信时,会有一些延迟导致不能立即生效
具体描述:在使用 react-native 时,需要加载外部网页,加载后,RN 提供一个按钮可以关闭网页,但如果打开的是内部网页就需要隐藏这个按钮,h5 代码使用 react 写的,在 componentDidMount 时,发送 postmessage 给客户端(RN),此时发现收不到,查阅 react-native 官方文档后得已解决。
解决过程:
https://github.com/facebook/r…,解释了为什么要延迟
https://github.com/react-nati… 解释了,升级后的 webview 为什么 postmessage 不能直接用

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}
        />;
    }
}

感觉对你有帮助的话,支持一下哈:

正文完
 0