作者:手机用户2502877197 | 来源:互联网 | 2023-10-12 11:58
1.RN向webview传值,js页面importReact,{Component}fromreact;import{Text,View,WebView,Style
1.RN向webview传值,js页面
import React, { Component } from 'react';
import {Text,View,WebView,StyleSheet,AppRegistry,TouchableOpacity,
} from'react-native';
export default class MyWeb extends Component {constructor(props){super(props);this.state={webstring:''}}handleMessage(){const message = 'hello web 199!';this.webview.postMessage(message);//通过handleMessage向RN传递字符串Message,若写成ref="wbview",这里写成this.refs.webview.postMessage(message)}render(){return ({this.state.webstring}简单的网页{ this.webview = webview;}} //或者可以写成:ref="webview"source={require('./html/aa.html')}>);}
}
var styles =StyleSheet.create({webview:{backgroundColor:'#ffffff',}
});
html页面:
Click on the header to alert its value
2.webview向RN传值,js页面
import React, { Component } from 'react';
import {Text,View,WebView,StyleSheet,AppRegistry,TouchableOpacity,
} from'react-native';
export default class MyWeb extends Component {constructor(props){super(props);this.state={webstring:''}}render(){return ({this.state.webstring}简单的网页{ this.webview = webview;}}source={require('./html/aa.html')}onMessage={(event)=>{this.setState({webstring:event.nativeEvent.data})}}//通过这个函数接受webview传来的值>);}
}
var styles =StyleSheet.create({webview:{backgroundColor:'#ffffff',}
});
html页面:
This is a header
Click on the header to alert its value