klippy
klippy

Reputation: 243

Call two functions with onPress in react native

I have the following two buttons:

        <View style={styles.button}>
          <Button title="ADD" onPress={createDocumentHandler}/>
        </View>
        <View style={styles.button}>
          <Button title="CANCEL" color="red" onPress={props.onCancel}/>
        </View>

I want the first button to trigger both the "createDocumentHandler" function and the "props.onCancel" function

I thought it would be as easy as onPress={createDocumentHandler, props.onCancel} but that only triggers the second method for some reason

Upvotes: 0

Views: 1272

Answers (3)

Dionisio Malteso
Dionisio Malteso

Reputation: 51

Try this

onCancel() => {
    {...}
 };
 function createDocumentHandler()
 {
     {...}
     return onCancel();
 }

function render(){
    return(
        <Button title="ADD" onPress={createDocumentHandler}/>
    )
}

Upvotes: 0

Gaurav Roy
Gaurav Roy

Reputation: 12210

You create a custome function createDocumentHandler as :

createDocumentHandler = async() => {
// execute in whatever order
  let a =  await function1();
  let b =  await function2();
}

Upvotes: 1

David
David

Reputation: 16277

<Button title="ADD" onPress={createDocumentHandler}/>
createDocumentHandler = () => {
   actionA();
   actionB();
}

Upvotes: 2

Related Questions