简体   繁体   English

如何在反应原生的单个按钮上一个接一个地调用多个 function

[英]how to call multiple function one by one on single button in react native

I am new to react native.我是新来的反应本地人。 I have a single button.我有一个按钮。 and I have 3 functions.我有 3 个功能。 so I want that when user click on first time then first function should be call.所以我希望当用户第一次点击时,首先应该调用 function。 when user click on 2nd time then 2nd function should be call.当用户点击第二次时,应该调用第二次 function。 when user click on 3rd time then 3rd function should be call.当用户点击第三次时,应该调用第三次 function。 and when user click on 4th time then again first function should be call and so on.当用户点击第 4 次时,首先应该调用 function,依此类推。 please help me.请帮我。 thanks.谢谢。

here is my code这是我的代码

<View>
<Icon onPress={this.function} name="md-camera" size={30}/>
</View>

You need 4 functions for this and one counter.为此,您需要 4 个功能和一个计数器。 Forth function will choose your function.第四 function 将选择您的 function。

   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=()=>{

    }

and in button call the _gateway function on press并在按下按钮时调用_gateway function

onPress{() => { this._gateway();}}

You have to maintain a count that how many times the functions has been triggered.您必须维护触发功能的次数。 And using that you can choose whether which function to execute when button presses.使用它,您可以选择按下按钮时是否执行哪个 function。

Note: I hope that you are using class components because you mentioned onPress event with this keyword and also function name is equal to function .注意:我希望您使用 class 组件,因为您使用this关键字提到了 onPress 事件,并且 function 名称等于function

Class components Class 组件

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>
        ...
    )
}

or或者

Functional components功能组件

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>
    ...
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM