'How to programmatically call the onPress() method of Pressable?

I want to call the onPress() method of my Pressable. How can I achieve this? I tried calling the onPress() method when I hit the second button via a ref but it did not work.

const pressableRef = useRef(null);

return (
    <Pressable 
        style={{ width: 100, height: 100, backgroundColor: 'yellow' }} 
        onPress={() => console.log('I want to print this')} 
        ref={pressableRef} 
    />
    <Button
        title="Klick me"
        onPress={() => {pressableRef.current.onPress()}
    />
);


Solution 1:[1]

my code not work in this situation, but i do some changes and it works for me :)

this is my code:

const ElementRef = createRef();
useEffect(() => {
        if (ElementRef.current) {
            setTimeout(() => {
                ElementRef.current?.scrollToIndex({
                    index: 3,
                    animated: true
                })
            }, 500);
        }
    }, [ElementRef])

// ...

<FlatList
                            nestedScrollEnabled
                            data={Calendar}
                            keyExtractor={item => item.visit_day_identity}
                            renderItem={renderItem}
                            horizontal
                            showsHorizontalScrollIndicator={false}
                            snapToEnd
                            ref={ElementRef}
                        />

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 fatemeh kazemi