gpt4 book ai didi

react-native - React Native onPress 被自动调用

转载 作者:行者123 更新时间:2023-12-03 06:11:24 24 4
gpt4 key购买 nike

我在使用 react native onPress 功能时遇到问题。 onPress 应该仅在实际由触摸事件触发时才起作用(我想),即当我按下屏幕上的按钮时。但当调用渲染函数时, onPress 似乎会自行触发。当我尝试手动按下时,它不起作用。

  import React, { Component } from 'react';
import { PropTypes, Text, View ,Alert } from 'react-native';
import { Button } from 'react-native-material-design';

export default class Home extends Component {

render() {
return (
<View style={{flex:1}}>
<Button value="Contacts" raised={true} onPress={this.handleRoute('x')} />
<Button value="Contacts" raised={true} onPress={this.handleRoute('y')} />
<Button value="Contacts" raised={true} onPress={this.handleRoute('z')} />
</View>
);
}
handleRoute(route){
alert(route) // >> x , y, z
}
}

module.exports = Home;

我错过了什么?我分配的方式有问题还是这是一些错误?任何建议都将受到高度赞赏。

Video

最佳答案

尝试改变

onPress={this.handleRoute('x')} // in this case handleRoute function is called as soon as render happen

onPress={() => this.handleRoute.bind('x')} //in this case handleRoute doesn't called as soon as render happen

关于react-native - React Native onPress 被自动调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42329240/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com