反応ネイティブWebビューから外部JavaScript関数にパラメータを渡す方法、
私のreactnativeプロジェクトに追加されたapp.jsファイルがあり、callfromapp(data)のような関数が含まれているので、react nativewebviewからapp.js関数にパラメーターを渡す必要があります。
iam webview.js内に関数injectJSFileFromWeb()を追加します。iamはapp.jsファイルパスを指定しますが、app.jsファイルを呼び出すと、ドキュメントのようなエラーを取得するiamが定義されていません。
反応ネイティブwebview.js内、
<WebView
ref={ref => (this.webview = ref)}
source={{ uri: 'example/index.html' }}
originWhitelist={["*"]}
domStorageEnabled={true}
startInLoadingState={true}
allowFileAccess={true}
javaScriptEnabled={true}
onLoad={()=>injectJSFileFromWeb()}
/>
injectJSFileFromWeb() {
//give the filename according to your need
var jsFileName = require('../../../ios/build/../example/app');
}
WebViewにはonMessage
、からメッセージを受信できる属性がありますWebView
。公式APIドキュメントの例を参照してくださいhttps://github.com/react-native-community/react-native-webview/blob/master/docs/Guide.md#the-windowreactnativewebviewpostmessage-method-and-onmessage-prop
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加