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: '點(diǎn)擊0',data1: '點(diǎn)擊1',data2: '點(diǎn)擊2',data3: '點(diǎn)擊3',data4: '點(diǎn)擊4',data5: '點(diǎn)擊5',}
};//箭頭函數(shù)(無(wú)參)press0= () => {
this.setState({
data0: "0被點(diǎn)擊了"});ToastAndroid.show('0被點(diǎn)擊了',ToastAndroid.SHORT);};
//一般方法(無(wú)參)press1() {
this.setState({
data1: "1被點(diǎn)擊了"});ToastAndroid.show('1被點(diǎn)擊了',ToastAndroid.SHORT);};
//一般方法(無(wú)參)press2() {
this.setState({
data2: "2被點(diǎn)擊了"});ToastAndroid.show('2被點(diǎn)擊了',ToastAndroid.SHORT);};
//一般方法(有參)press3(x,y) {
this.setState({
data3: x+y
});ToastAndroid.show('3被點(diǎn)擊了: '+this.state.data3,ToastAndroid.SHORT);};
//一般方法(有參)press4(x) {
this.setState({
data4: x
});ToastAndroid.show('4被點(diǎn)擊了',ToastAndroid.SHORT);};
//箭頭函數(shù)(有參)press5= (x) => {
this.setState({
data5: x
});ToastAndroid.show('5被點(diǎn)擊了',ToastAndroid.SHORT);};
render() {
return(
{/*正確的方式應(yīng)該不在render的時(shí)候立即執(zhí)行。因此正確調(diào)用方法如下,同時(shí),箭頭函數(shù)將一個(gè)函數(shù)賦值給press0變量,變量在調(diào)用的時(shí)候自然不需要加()*/}
// onPress={this.press0} //如果該方法本身就是箭頭函數(shù),那么直接調(diào)用this.press0// onPress={()=>this.press0()}//無(wú)論該方法是箭頭函數(shù)還是普通函數(shù),都可以利用該方法來(lái)執(zhí)行()=>this.press0()onPress={this.press0.bind(this)}//無(wú)論該方法是箭頭函數(shù)還是普通函數(shù),都可以利用該方法來(lái)執(zhí)行()=>this.press0()>{this.state.data0}{/*普通函數(shù)的無(wú)參與有參的調(diào)用方式相同。注意的是有參的函數(shù)使用bind方式傳遞參數(shù)時(shí)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}
{/*下面的調(diào)用方法錯(cuò)誤,原因:下面的調(diào)用方式導(dǎo)致onpress事件直接被調(diào)用press5方法修改了state,由于state被修改,頁(yè)面被重新渲染,再次直接調(diào)用press5形成循環(huán)*/}
{/*正確函數(shù)調(diào)用方式1、箭頭方法 onPress={() => this.press1()} (無(wú)參、箭頭函數(shù)或普通函數(shù),都可以用此方式調(diào)用)2、箭頭方法 onPress={() => this.press1(xx)} (有參、箭頭函數(shù)或普通函數(shù),都可以用此方式調(diào)用)3、bind方式 onPress={this.press2.bind(this)} (無(wú)參、箭頭函數(shù)或普通函數(shù),都可以用此方式調(diào)用)4、bind方式 onPress={this.press2.bind(this,x)}(有參、箭頭函數(shù)或普通函數(shù),都可以用此方式調(diào)用)*/}
//錯(cuò)誤//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',},});
總結(jié)
以上是生活随笔為你收集整理的C语言调用es6,ES6 箭头函数、普通函数、调用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c语言程序设计实践教程张卫国,C语言程序
- 下一篇: android拦截短信获取短信内容,《英