React Native Linking to web page and push back button - react-native

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 :)

Related

How to change native splash screen with Javascript splash screen?

I need that when the native splash screen finishes loading, another splash screen (now on the javascript side) continues loading... with the same image and the same size shown on the native splash screen.
I already tried to define some sizes and positions on screen. But they never look alike. Can anyone point me to some documentation for me to study or guide me on how I can do this?
Technically splash screen is a part of the native application. You can't modify it from React Native side.
However, using libraries like https://github.com/zoontek/react-native-bootsplash you can control when to hide the splash screen. For example:
const App = () => {
componentDidMount() {
// do your logic even async
RNBootSplash.hide(); // call it to hide native splash screen
}
}

React Native doesn't re-render when re-opening app

I have a functional component in my react native app that has an event listener watching the app state in order to clear out a cache when the app is closed. This works as expected, and I have logic written in a useEffect block to add/remove this event listener when the app is closed.
The functionality works perfectly when the user either navigates elsewhere in the app or closes the app, but when the app is re-opened after closing (not fully shutting down the app, just moving to a different app) the page is not re-rendered and so the event listener is not re-added.
I've attached the code in my useEffect below.
useEffect(() => {
if (!listenerRef.current) {
toggleEventListener(true);
}
return () => {
toggleEventListener(false);
};
}, []);
You can use appState from react-native to see foreground and background events
https://reactnative.dev/docs/appstate

How to refresh a component which is already present in stack after navigating from another component in react native

Ex- I have two components A and B. I need to refresh component A after navigating from component B.
componentDid Mount doesn't work because A is already mounted.
How to achieve this. I am using react navigation to navigate from B->A
You can either use NavigationEvents from react-navigation or can pass a callback which will trigger on navigation.goBack().
Check this snack : Temporary Link
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
componentDidMount(){
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 a component
this.handleRefresh()
}
);
}
Dont forget to remove it when it is done.
componentWillMount(){
didFocusSubscription.remove();
}
More you can read here. Thanks :)

How to make app change to foreground from background programmatically in react-native?

As the title, how can we start a react-native app from background to foreground, like we tap on push notification, it will show the app?
Thank you!
Look at https://github.com/geektimecoil/react-native-onesignal library it is easy to use and you can even specify what screen you want to open within your app with the
opened event listener
OneSignal.addEventListener('opened', (data) => {
if (data.notification.payload.title === 'New mensaje!') {
this.props.pushNewRoute('chat'); // open specific app screen (Redux)
}
});

React Native - AppState 'change' event get no call when navigating between screens

AppState.addEventListener('change', ...)
The listener does not getting any call. I'm using StackNavigator to put root screen and the screen with AppState
(Updated) It's been called when I restart the app. But the Component where I use AppState is not yet the active screen
In my case, I just want to get notified when a navigation come back to or leave a screen. So I can simply do this
this.props.navigation.addListener('didFocus', ...);
this.props.navigation.addListener('didBlur', ...);
The problem with AppState still remains