Javascript 如何在react native中的单个按钮上逐个调用多个函数
我是个新来的本地人。我只有一个按钮。我有3个功能。所以我希望当用户第一次点击时,第一个函数应该是call。当用户第二次单击时,应调用第二个函数。当用户第三次单击时,应调用第三个函数。当用户第四次单击时,第一个函数应该是call等等。请帮帮我。谢谢 这是我的密码Javascript 如何在react native中的单个按钮上逐个调用多个函数,javascript,react-native,Javascript,React Native,我是个新来的本地人。我只有一个按钮。我有3个功能。所以我希望当用户第一次点击时,第一个函数应该是call。当用户第二次单击时,应调用第二个函数。当用户第三次单击时,应调用第三个函数。当用户第四次单击时,第一个函数应该是call等等。请帮帮我。谢谢 这是我的密码 <View> <Icon onPress={this.function} name="md-camera" size={30}/> </View> 此计数器需要4个功能和一个计数器
<View>
<Icon onPress={this.function} name="md-camera" size={30}/>
</View>
此计数器需要4个功能和一个计数器。第四个函数将选择您的函数
state = {
count: 0
};
_gateway=()=>{
this.setState({
count: this.state.count==3 ? 1 : this.state.count+1
})
if(this.state.count==1){
_function1();
}
if(this.state.count==2){
_function2();
}
if(this.state.count==3){
_function3();
}
}
_function1=()=>{
}
_function2=()=>{
}
_function3=()=>{
}
按下按钮调用_gateway功能
onPress{() => { this._gateway();}}
您必须记录函数被触发的次数。使用它,您可以选择在按下按钮时是否执行哪个函数 注意:我希望您使用的是类组件,因为您在onPress事件中提到了
这个
关键字,并且函数名等于函数
类组件
构造函数(道具){
超级(道具);
此.state={
触发器计数:0
};
this.functionName=this.functionName.bind(this);
this.function1=this.function1.bind(this);
this.function2=this.function2.bind(this);
this.function3=this.function3.bind(this);
}
函数名(){
开关(this.state.triggerCount){
案例0:
这个函数是1();
打破
案例1:
这个函数是2();
打破
案例2:
这个函数是3();
打破
}
这是我的国家({
triggerCount:(this.state.triggerCount+1)%3
});
}
职能1(){
//函数1的逻辑
}
职能2(){
//函数2的逻辑
}
职能3(){
//函数3的逻辑
}
render(){
返回(
...
...
)
}
或
功能部件
const[triggerCount,setTriggerCount]=useState(0);
常量函数名=()=>{
开关(触发器计数){
案例0:
功能1();
打破
案例1:
函数2();
打破
案例2:
功能3();
打破
}
setTriggerCount((triggerCount+1)%3);
}
常量函数1=()=>{
//函数1的逻辑
}
常量函数2=()=>{
//函数2的逻辑
}
常量函数3=()=>{
//函数3的逻辑
}
返回(
...
...
);
undefined不是一个对象(计算'this.state.triggerCount')您将this
绑定到构造函数中的函数。不用担心,我已经根据您的错误更新了答案,并添加了如何将函数绑定到此。你现在可以继续了。谢谢,但我得到了我的答案,你的代码也在工作。谢谢,自动启动相机。谢谢,伙计,你做到了,谢谢,太感谢了,不客气:)
constructor(props) {
super(props);
this.state = {
triggerCount: 0
};
this.functionName = this.functionName.bind(this);
this.function1 = this.function1.bind(this);
this.function2 = this.function2.bind(this);
this.function3 = this.function3.bind(this);
}
functionName() {
switch (this.state.triggerCount) {
case 0:
this.function1();
break;
case 1:
this.function2();
break;
case 2:
this.function3();
break;
}
this.setState({
triggerCount: (this.state.triggerCount + 1) % 3
});
}
function1() {
// Your logic for function 1
}
function2() {
// Your logic for function 2
}
function3() {
// Your logic for function 3
}
render() {
return (
...
<View>
<Icon onPress={this.functionName} name="md-camera" size={30}/>
</View>
...
)
}
const [triggerCount, setTriggerCount] = useState(0);
const functionName = () => {
switch (triggerCount) {
case 0:
function1();
break;
case 1:
function2();
break;
case 2:
function3();
break;
}
setTriggerCount((triggerCount + 1) % 3);
}
const function1 = () => {
// Your logic for function 1
}
const function2 = () => {
// Your logic for function 2
}
const function3 = () => {
// Your logic for function 3
}
return (
...
<View>
<Icon onPress={functionName} name="md-camera" size={30}/>
</View>
...
);