React Native在render渲染界面时控件的onPress中方法就被自动执行

问题:

render界面渲染时候onPress指向的doSomething方法就执行了;

期望:
只有当点击这个按钮时才触发doSomething方法

<Button
  ...
  onPress={
    
    this.doSomething()}
  ...
>

原因:

猜想是RN会把{}中的对象或者方法都预加载,所以导致一进来就会被执行

解决办法:
使用箭头函数

<Button
  ...
  onPress={
    
    () => this.doSomething()}
  ...
>

猜你喜欢

转载自blog.csdn.net/nongminkouhao/article/details/109290697