支持android&ios高度自适应及JS相互调用的组件react-native-webview2
2016-08-25 10:15
1061 查看
本项目是WebView 或 Web 组件,支持Android、iOS, 支持auto height高度自适应及在html页面和RN组件之间call js相互调用js方法,very useful & easily!
基于React-native原生的WebView组件纯js实现, 起名为react-native-webview2 或 WebView 或 Web.
react-native-webview2 当你设置了属性 source={uri:xxx}时可以动态改变WebView 的高度(目前还不支持source={html}),但若你设置了属性style={height:xxx}设置了高度就不能自适应了, 设置了高度后高度是固定的.
react-native-webview2 也可以让你在html页面和reactnative之间相互调用js代码。要从rn组件调用html页面的js,你只需要在rn组件中调用this.web.evalJs("js code...here") ,若要调用react-native代码,你只需要在html页面中调用 returnEval("rn code...here"),同时你需要设置这个组件的属性evalReturn={(r) => {eval(r)}.
react-native-webview2支持所有其他原生React Native WebView的属性.
WebView示例项目: https://github.com/greatbsky/react-native-webview2-demo
编写代码:
完整示例代码: https://github.com/greatbsky/react-native-webview2-demo/blob/master/WebView2App/app.js
evalJs: 从react-native调用html页面里的js的方法. 例如: this.web.evalJs('var t = document.title; alert(t)');
evalReturn: 若你需要从html页面调用react-native的方法,这个属性是需要的. 固定写法:evalReturn={(r) => {eval(r)}.
go: 打开一个新的url. 例如: this.web.go('http://xxxxxx');
其他
returnEval: 在html页面的一个function,从html页面调用react-native的方法时调用的js方法, 例如:returnEval('this.setText("from html page...")')
基于React-native原生的WebView组件纯js实现, 起名为react-native-webview2 或 WebView 或 Web.
react-native-webview2 当你设置了属性 source={uri:xxx}时可以动态改变WebView 的高度(目前还不支持source={html}),但若你设置了属性style={height:xxx}设置了高度就不能自适应了, 设置了高度后高度是固定的.
react-native-webview2 也可以让你在html页面和reactnative之间相互调用js代码。要从rn组件调用html页面的js,你只需要在rn组件中调用this.web.evalJs("js code...here") ,若要调用react-native代码,你只需要在html页面中调用 returnEval("rn code...here"),同时你需要设置这个组件的属性evalReturn={(r) => {eval(r)}.
react-native-webview2支持所有其他原生React Native WebView的属性.
WebView示例项目: https://github.com/greatbsky/react-native-webview2-demo
WebView示例
WebView 使用方法
执行 npm install react-native-webview2 --save编写代码:
import Web from 'react-native-webview2'; <Web ref={(c) => {this.web = c}} evalReturn={(r) => {eval(r)} source={{uri: 'xxx'}} style={[styles.web, {minHeight: 300}]} ...other props />
完整示例代码: https://github.com/greatbsky/react-native-webview2-demo/blob/master/WebView2App/app.js
WebView 配置
新增的属性evalJs: 从react-native调用html页面里的js的方法. 例如: this.web.evalJs('var t = document.title; alert(t)');
evalReturn: 若你需要从html页面调用react-native的方法,这个属性是需要的. 固定写法:evalReturn={(r) => {eval(r)}.
go: 打开一个新的url. 例如: this.web.go('http://xxxxxx');
其他
returnEval: 在html页面的一个function,从html页面调用react-native的方法时调用的js方法, 例如:returnEval('this.setText("from html page...")')
Licensed
MIT License
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件