'How to call multiple functions with onPress, which has its own parameter in React Native?
I am trying to call multiple functions when onPress is called in React Native. The problem is that where I call the functions, that function itself has a handlePress parameter. Here is the code:
const _renderTruncatedFooter = (handlePress) => {
return (
<Button
accessoryLeft={MoreIcon}
appearance='ghost'
status='control'
size = 'medium'
style={styles.button}
onPress={()=>{handlePress, storeRead(item), changeTitleColor();}} >
{i18n.t('ShowMore')}
</Button>
);
}
the other two functions, that I want to call are:
const storeRead = async (value) => {
try {
const jsonValue = JSON.stringify(value)
await AsyncStorage.setItem('read_data', jsonValue)
alert('data saved')
} catch (e) {
alert('Failed to save the data to the storage')
}
}
and the second one is:
const changeTitleColor = () => {
setRead(true);
}
But this is not working. How can I achieve this?
Solution 1:[1]
don't use comma inside your onPress props, just do like this
onPress={() => {
handlePress();
storeRead(item);
changeTitleColor();
}}
Solution 2:[2]
call one function in on press like sectionOne
so in section one, you can call your all rest function
not call all functions in onPress
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 | iChwan |
Solution 2 | Pushpendra Chouhan |