Home > Net >  How to execute a function through conditional statement in react-native?
How to execute a function through conditional statement in react-native?

Time:10-27

function OK() {
  return (
    <View  style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Modal>
        <Text>OK</Text>
      </Modal>    
    </View>
  )
}

const choice1 = () => {
  return (OK)  
}

function MyButton () {
  return (
      <View>
          <TouchableOpacity onPress={choice1}>
              myButton Touch
          </TouchableOpacity>
      </View>  
  );
}

function App() {
  return (
    <View>
      <MyButton />
    <View>
)}

When myButton is pressed, I want to display Ok through some conditional.
The conditional will be put in choice1.
How can I execute OK through choice1?

)My end goal is to get the ok character to appear when mybutton is pressed.
And I want to put a conditional statement in the process.
In other words, when the mybutton is pressed, choice1 containing the conditional statement is executed, and when the condition is satisfied, I want to display the OK character.
I want to know what is the error in my code or how to implement the above.

CodePudding user response:

If you want to check the onpress condition true or false. before button press set one state. delare state false, where button press change the state false into true. this time you can get your output.

CodePudding user response:

In this way the choice function can run conditionally

function OK() {
  return (
    <View  style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Modal>
        <Text>OK</Text>
      </Modal>    
    </View>
  )
}

const choice1 = () => {
  return (OK)  
}

function MyButton () {
  return (
      <View>
          <TouchableOpacity onPress={() => {
              if (condition) {
                  choice1();
              } else {
                // Other code
              }
          }}>
              myButton Touch
          </TouchableOpacity>
      </View>  
  );
}

function App() {
  return (
    <View>
      <MyButton />
    <View>
)}
  • Related