'Call 2 function in onPress ( navigation and submit form)
i made a form with the API react-hook-form and i would like to make another call when I click in the submit (i have already a function which get the data from the form). I use react navigation 5.0.5 for page changes but when i do the 2 actions it's not longer possible. I would like to know if you do have ideas for make multiple calls, I already tried to build a fonction with the different functions but nothing is called when I call this new function.
here is my code :
import React from 'react'
import { StyleSheet, View, TextInput, Button, Text, ActivityIndicator,Image,TouchableOpacity,Icon } from 'react-native'
import Form from "./Form";
import { useForm, Controller } from "react-hook-form";
function Filtrage({ navigation }) {
const { control, handleSubmit, errors } = useForm();
const onSubmit = data => console.log(JSON.stringify(data));
return (
<View style={styles.main_container}>
<Text style={styles.titreHome} >Vos études ?</Text>
<Controller
control={control}
render={({ onChange, onBlur, value }) => (
<TextInput
style={styles.testField}
onBlur={onBlur}
onChangeText={value => onChange(value)}
value={value}
/>
)}
name="etudes"
rules={{ required: true }}
defaultValue=""
/>
<TouchableOpacity style={styles.bouton} >
<Button color="white" title="Voir les fiches disponible" onPress={handleSubmit(onSubmit)} />
</TouchableOpacity>
</View>
);
}
and the function I would like in the submit Button:
onPress={() => navigation.navigate("listeCours")}
thanks for your help
Solution 1:[1]
Just wrap it in a lambda
<TouchableOpacity style={styles.bouton} >
<Button color="white" title="Voir les fiches disponible" onPress={() => {
handleSubmit(onSubmit)
navigation.navigate("listeCours")
}
} />
</TouchableOpacity>
Solution 2:[2]
You can use like this;
onPress={() => {
handleSubmit(onSubmit)() // <-- don't forget add additional '()'
handleNewFunction()
}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | anthony willis muñoz |
| Solution 2 | Ugur |
