React Native onPressOut - react-native

I am using react-native. I want to trigger the function when the user releases the screen. However, onPressOut fires even if the user drags the finger.
Is there any simple way that I can detect the timing when the user releases their finger?
onPressIn={() => {
console.warn('Pressed!');
}}
onPressOut={() => {
console.warn('Released');
}}

onPressOut will be called the moment you drag something even if the user still touching the screen.
You should check onPanResponderRelease, it will trigger when the user releases the touch after dragging. Documentation here

Related

React Native Pressable onPressIn only called when OnPress handler is present

In the android emulator, the following RN code does nothing (no log, no handler called)
<Pressable style={[styles.fullscreenContainer]}
onPressIn={()=> {console.log("onPress");this.props.touchHandler()}}>
However, if I insert a onPress handler, in otherwise exactly the same code, both logs appear and both handlers are called when the component is pressed.
<Pressable style={[styles.fullscreenContainer]}
onPress= {()=> {console.log("onPress"); this.props.touchHandler()}}
onPressIn={()=> {console.log("onPressIn");this.props.touchHandler()}}>
Why does onPressIn not work on its own?
FYI onPressIn works correctly on iOS in the expo client
onPressIn is followed by onPress, but if you haven't given onPress any logic to perform, then nothing will happen. You'll only get an onPressIn response, even though onPress is technically still triggered behind the scenes. (It's just a blank onPress).

React Navigation v4 lifecycle events do not fire when navigating to the same screen

Let's say I have a ProductScreen component that renders info about a product as well as a list of other similar products. When clicking on a similar product, a navigation is done from ProductScreen ==> ProductScreen with different params/key.
The problem I'm facing is that I want to do something on the React Navigation focus event (for example, scroll to top)
navigation.addListener('didFocus', () => {
console.log('didFocus')
scrollToTop()
})
I see the log the first time the component renders, but when doing a navigation from ProductScreen ==> ProductScreen, the didFocus event doesn't seem to be firing again so the component isn't scrolled to the top. In fact, none of the lifecycle events (willFocus, didFocus, willBlur, didBlur) seem to be firing.
Is there any way to be able to use react navigation lifecycle events when navigating from component X to component X with different params? Thanks!
You're navigating to the same screen so it's normal that didFocus doesn't fire, since the screen was already focused.
If you want to react to params change, you can use componentDidUpdate/useEffect to compare previous params with the new params and do what you need to do.
If you want to navigate to a new screen, you can use push instead of navigate

React Native Linking to web page and push back button

I built a react-native app and if user clicks a link then app opens a default web browser and go to the link.
Linking.openURL(notification.link);
If user push back button from the android or ios devices, is there any way we can detect the back move?
You can add a listener for the same in react native.
There are total 4 listners for the same.
willFocus - the screen will focus
didFocus - the screen focused (if there was a transition, the transition completed)
willBlur - the screen will be unfocused
didBlur - the screen unfocused (if there was a transition, the transition completed)
Try the below code
componentWillMount(){
const didFocusSubscription = this.props.navigation.addListener(
'didFocus',
payload => {
console.warn('didFocus ', payload);
# just write your code/call a method here which you want to execute when the app comes from background
}
);
}
Dont forget to remove it when it is done.
componentWillUnmount(){
didFocusSubscription.remove();
}
More you can read here. Thanks :)

How to detect tap on react-native-dropdownalert?

I'm using react-native-dropdownalert for my react native app. If a notification comes, the dropdownalert will be shown. How can I detect the tap on the bar?
I saw the official documentation and there is an onClose method. onClose doesn't meet the need above.
Any method I should use or any good react native 3rd party library I can use instead?
version>=4.1.0
onTap prop added in 4.1.0 release.
onTap={() => console.log('User tapped!')}
version<4.1.0
onClose returns data.
data = {type, title, message, action}
action is what you need.
action can be automatic, programmatic, tap or pan
onClose={({ type, title, message, action }) => action === 'tap' && console.log('User tapped!')}

Expo.js: Capture event when my app goes into the background?

If I'm using my app and then switch out of it using the home button, app switcher, etc, Is there a way to reliably run some code when this event is detected? I want to do some tasks such as cancelling timers, scheduling notifications, and so on.
You can track app's state using AppState provided by 'react-native'
By using AppState.currentState you will know whether the app is in foreground, background, active or inactive. If you want to perform some tasks whenever the state changes you can use event listeners provided by AppState.
You can add the following logic in your Component to get it done -
componentDidMount() {
AppState.addEventListener('change', this.handleStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleStateChange);
}
handleStateChange will be called every time the app moves from foreground to background or vice-versa with the app's current state passed as an argument.
For further reference see the docs
Hope it helped.