写在前面的话:本博纯粹是为记录自己的辛路历程,若有缘的你觉得这篇博文中错漏百出,请不要嗤笑,我只是一个兴趣爱好者,而非专业人士,如果留言指正,我当然是非常开心的啦~
2017年1月14日21:29:01
wechat4u模块已经被npm收录,react-native init初始化项目后直接npm install wechat4u --save就能安装到项目目录,同时自动“保存”index.json文件,但是.babelrc文件还得自己配置,wechat4u的.babelrc文件中preset项里面都是latest,项目运行时经常报包含latest的错误,改成“es2015”就不这么报了。
目标要一步一步的来,首先只要实现获得QR的url并显示就行了,这个是我的代码:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Image,
Text,
View
} from 'react-native';
export default class XzlWX extends Component {
constructor(props) {
super(props);
const Wechat = require('wechat4u')
this.state = {
bot: new Wechat(),
qrurl: ''
};
}
componentDidMount() {
// *
this.bot.on('error', err => {
console.log('错误:', err)
})
this.bot.on('uuid', uuid => {
this.state.qrurl = './qrurl.txt','https://login.weixin.qq.com/qrcode/' + uuid;
})//*/
this.bot.start()
}
render() {
if (!this.state.qrurl) {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
waiting for QR...
</Text>
</View>
);
}
return (
<View style={styles.container}>
<Image
source={{uri: this.state.qrurl}}
style={styles.thumbnail}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
thumbnail: {
width: 53,
height: 81,
},
});
AppRegistry.registerComponent('XzlWX', () => XzlWX);
这个是错误提示,是因为wechat4u的事件不被react native支持所致?
目前就到这里了,下一步是不是该了解一下wechat4u的事件机制以及react native的事件注册方法呢?