Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6290

How to declare onPress function with optional input parameter in react native using typescirpt

$
0
0

I am working with react-native using typescript. I want an onPress method with an optional boolean input parameter. And I want to pass it directly to the onPress method without creating a new arrow function if there is no input. Like below code:

const onClose(clearMessages?: boolean) => {   // doing stuff   if (clearMessages) {      // doing extra stuff   }}// use it like this<Pressable onPress={onClose}><Text>{'Close'}</Text></Pressable><Pressable onPress={() => onClose(true)}><Text>{'Clear messages'}</Text></Pressable>

The thing is when I call onClose directly I get the typescript error below:

Type '(clearMessage?: boolean) => (event: GestureResponderEvent) => void' is not assignable to type '(event: GestureResponderEvent) => void'.Types of parameters 'clearMessage' and 'event' are incompatible.Type 'GestureResponderEvent' is not assignable to type 'boolean'.ts(2322)


Viewing all articles
Browse latest Browse all 6290

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>