react native Alert使用

1,Alert.alert("参数一:标题", "参数二:内容",    [  {text: '确认'  , onPress:  ()=>{ }} , {text:'取消' , onPress: ()=>{ } } ]   );

2,在react native中函数调用什么时候加this,什么时候加()?
  加括号表示立即执行,但是一些事件(比如点击触摸),是在事件发生的时候才调用,所以不需要添加括号。而箭头函数代表一个函数,里面调用的函数添加括号以后在内部执行了,在外部并没有执行。

调用函数无参情况下:
    <Button  onPress={  this.getdata()  }   />   //会立即执行
    <Button  onPress={  ()=>{ this.getdata() })    />    //不会立即执行
    <Button  onPress={ this.getdata }  />          //不会立即执行,正确

调用函数有参情况下:
    <Button onPress={   this.getdata("string")   }  />                          //会立即执行
    <Button  onPress={  ()=>{ this.getdata("string1")  }} />                  //不会立即执行
    <Button  onPress={   this.getdata.bind(this, "string") }   />             //不会立即执行

 

4,说到这个不得不提一提js中函数,变量调用规则:
   

function a(){
    console.log(1);
 }

 function a(){
     console.log(2)
 }
  a();
//打印结果是2
a();
var  a=6;
function  a(){
    console.log(3);
}
function  a(){
    console.log(4);
}
//打印结果是4;
function a(){
   console.log(1);
}
function a(){
    console.log(2)
}
 a();
function  a(){
    console.log(3);
}
function  a(){
    console.log(4);
}
//打印结果是4
var  a=0;
    function a(){
    console.log(1);
 }
 function a(){
     console.log(2)
 }
  a();
 function  a(){
     console.log(3);
 }
 function  a(){
     console.log(4);
 }
//报错:a is not a function
function a(){
   console.log(1);
}

function a(){
    console.log(2)
}
 a();
var  a=6;
function  a(){
    console.log(3);
}
function  a(){
    console.log(4);
}
//打印结果4
 function a(){
    console.log(1);
 }

 function a(){
     console.log(2)
 }
  a();
 function  a(){
     console.log(3);
 }
 function  a(){
     console.log(4);
 }
var  a=5;
//打印结果是4

5,react native类中的函数调用方式:
   class  testfunction   {
    constructor(){

   super(props);

_this=this;
this.delete_database=this.delete_database.bind(this);

}
function  delete_database(){
Alert.alert("你调用了我,我删除选项一了哦");
}

function  onpress_alert(){
Alert.alert( "这是标题",  "这是内容",
[  {text:   "这是选项一",  onPress: _this.delete_database   },
   {text:  "这是选项二",   onPress: ()=>console.log("你选择了选项二")  }
]   }

这里作为一个小白,我踩了很多坑:
1,delete_database()函数作为被调用的,不一定要放在onpress_alert()函数之前,可以放在它后面。
2,delete_database()函数作为onPress事件触发时被调用的回调函数,有两种形式书写:上面已经讲过了
3,听说: 在js中,this指向调用自己的那个对象;而在react native中,this指向函数定义的时候的位置,比如:
  class   wind{
   function  a(){
  this1=this;     //this1指向函数a() 定义时的位置,即类 wind,那么在这里可以调用
}  
function  b(){
   https://www.cnblogs.com/fingersense/p/8098117.html
    

 

 

 

 


    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值