C语言调用es6,ES6 箭头函数、普通函数、调用方法
importReact,{Component} from'react';import{
Platform,StyleSheet,Text,Image,View,TouchableOpacity,ToastAndroid,} from'react-native';
export default classsrrowFunDemo extendsComponent {
constructor(props) {
super(props);this.state= {
data0: '點擊0',data1: '點擊1',data2: '點擊2',data3: '點擊3',data4: '點擊4',data5: '點擊5',}
};//箭頭函數(無參)press0= () => {
this.setState({
data0: "0被點擊了"});ToastAndroid.show('0被點擊了',ToastAndroid.SHORT);};
//一般方法(無參)press1() {
this.setState({
data1: "1被點擊了"});ToastAndroid.show('1被點擊了',ToastAndroid.SHORT);};
//一般方法(無參)press2() {
this.setState({
data2: "2被點擊了"});ToastAndroid.show('2被點擊了',ToastAndroid.SHORT);};
//一般方法(有參)press3(x,y) {
this.setState({
data3: x+y
});ToastAndroid.show('3被點擊了: '+this.state.data3,ToastAndroid.SHORT);};
//一般方法(有參)press4(x) {
this.setState({
data4: x
});ToastAndroid.show('4被點擊了',ToastAndroid.SHORT);};
//箭頭函數(有參)press5= (x) => {
this.setState({
data5: x
});ToastAndroid.show('5被點擊了',ToastAndroid.SHORT);};
render() {
return(
{/*正確的方式應該不在render的時候立即執行。因此正確調用方法如下,同時,箭頭函數將一個函數賦值給press0變量,變量在調用的時候自然不需要加()*/}
// onPress={this.press0} //如果該方法本身就是箭頭函數,那么直接調用this.press0// onPress={()=>this.press0()}//無論該方法是箭頭函數還是普通函數,都可以利用該方法來執行()=>this.press0()onPress={this.press0.bind(this)}//無論該方法是箭頭函數還是普通函數,都可以利用該方法來執行()=>this.press0()>{this.state.data0}{/*普通函數的無參與有參的調用方式相同。注意的是有參的函數使用bind方式傳遞參數時this必須在最前面。*/}
onPress={() => this.press1()}
>{this.state.data1}
onPress={this.press2.bind(this)}
>{this.state.data2}
onPress={this.press3.bind(this,2222,1111)}
>{this.state.data3}
onPress={()=>this.press4(2222)}
>{this.state.data4}
{/*下面的調用方法錯誤,原因:下面的調用方式導致onpress事件直接被調用press5方法修改了state,由于state被修改,頁面被重新渲染,再次直接調用press5形成循環*/}
{/*正確函數調用方式1、箭頭方法 onPress={() => this.press1()} (無參、箭頭函數或普通函數,都可以用此方式調用)2、箭頭方法 onPress={() => this.press1(xx)} (有參、箭頭函數或普通函數,都可以用此方式調用)3、bind方式 onPress={this.press2.bind(this)} (無參、箭頭函數或普通函數,都可以用此方式調用)4、bind方式 onPress={this.press2.bind(this,x)}(有參、箭頭函數或普通函數,都可以用此方式調用)*/}
//錯誤//onPress={this.press5(2222)}//正確onPress={this.press5.bind(this,2222)}
//正確//onPress={()=>this.press5(2222)}>{this.state.data5});}
}
conststyles = StyleSheet.create({
text: {
backgroundColor: 'red',width: 200,height: 30,marginBottom: 50,justifyContent:'center',alignItems: 'center',},});
總結
以上是生活随笔為你收集整理的C语言调用es6,ES6 箭头函数、普通函数、调用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言程序设计实践教程张卫国,C语言程序
- 下一篇: android拦截短信获取短信内容,《英