best way to debug React-Native application? - debugging

I am new to React-Native Development.
Previously, I developed many applications in different Hybrid mobile application frameworks (Ionic, Cordova, etc.)
Debugging is very easy on Ionic, Cordova frameworks applications.
Please suggest the best way to debug React-Native applications

if you’re using Android Emulator then press CTRL + M from your computer keyboard to open the debug menu.
if you’re using iOS Simulator then press CMD + D from your computer keyboard to open the debug menu.
Click on Debug with Chrome to open the Chrome Debugger Tool in your computer. After opening the Debug tool in your Chrome.
Right Click on the screen -> Inspect .
Now you can see the Debugger Console Window on the Right Side of Screen. Select Console TAB.
Now in your app you can putlogs to debug app.
console.log('debugging');
Example:
import React, { useState, useEffect } from 'react';
import { View, StyleSheet, Text, LogBox } from 'react-native';
export default function App() {
//LogBox.ignoreLogs(['Remote debugger']);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'GET',
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
})
.catch((error) => {
console.error(error);
});
}, []);
console.log('App Called...');
return (
<View style={styleSheet.MainContainer}>
<Text style={styleSheet.text}>
Debug App in Android Emulator in React Native using Chrome Debugger Tool.
</Text>
</View>
);
}
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
text: {
textAlign: 'center',
fontSize: 27,
padding: 10
}
});

If you use expo for your react-native project you should use the expo-cli
otherwise the react-native-cli with an emulator or a phone connected in debug mode.

As per my past experience, the best and easy way to debug React Native Applications is by using Flipper Application(Developed by Facebook for React-Native App)

Related

React Native Appearance.addChangeListener() does nothing

I am writing my first react-native app and would like to switch themes like GMail does.
Indeed GMail get darker colors when we change the theme mode, or set automatic dark theme during nights, into the phone settings.
So I try to implement the Appearance.addChangeListener as explained in the doc but unfortunately the feature doesn't work.
I am trying on Android 10.
How can I, without restarting the app, update the color of my application when the phone theme change ?
useEffect(() => {
dispatch(changeTheme(Appearance.getColorScheme()));
Appearance.addChangeListener(onThemeChange);
return () => Appearance.removeChangeListener(onThemeChange);
}, []);
// This function is never call
const onThemeChange = ({ colorScheme }) => {
console.log("onThemeChange", colorScheme)
dispatch(changeTheme(colorScheme));
}
I resolved this problem by adding this code to MainActivity.java:
import android.content.res.Configuration;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getReactInstanceManager().onConfigurationChanged(this, newConfig);
}
and for testing this feature, you can change DarkTheme in Android 10 and above ON/OFF.
useColorScheme
you can use useColorScheme, it will update automatically.
first
import {useColorScheme} from 'react-native;
then, in you functional component
const colorScheme = useColorScheme();
I also suggest to write your own custom hook to update StatusBar and NavigationBar on colorScheme changed
use useColorScheme
It will change the dark and light mode automatically
import {useColorScheme} from 'react-native;
then,for class component
componentDidMount() {
Appearance.getColorScheme()=='dark'?this.setState({ theme: true }): this.setState({ theme: false});
}
componentDidUpdate(){
this.listener =Appearance.addChangeListener((theme) => {
// console.log("theme", theme);
theme.colorScheme == "dark"
? this.setState({ theme: true })
: this.setState({ theme: false });
});
}

How to navigate to specific page on back button event in nativescript 6

I wrote some code to capture the back button event on android when I was using nativescript 5 and it was working fine, but after upgrading to nativescript 6 weird behaviors appeared like:
1- if clear history is set to true, the app navigate to the page then close.
2- if clear history is set to false it navigates to the page then navigate back to the previous page.
An example for this behavior:
Let's say I want the app to navigate to page A when back button pressed,
and I am in page B so the two weird behaviors are:
clearHistory: true the app navigate to page A and close.
clearHistory: false the app navigate to A and return to B.
Here is the code:
if (application.android) {
application.android.on(application
.AndroidApplication
.activityBackPressedEvent, backEvent);
}
function backEvent(){
console.log('pressed')
const navigationEntry = {
moduleName: 'views/mainPage/main-page',
animated: true,
clearHistory: false,
transition: {
name: "slideLeft",
duration: 380,
curve: "linear"
}
}
frame.topmost().navigate(navigationEntry)
}
Is there something that I miss in nativescript 6?
Project github repo here
If you like to navigate upon back button, you must cancel the back navigation first by setting the cancel flag to true.
function backEvent(args) {
args.cancel = true;
console.log('pressed')
const navigationEntry = {
moduleName: 'views/mainPage/main-page',
animated: true,
clearHistory: false,
transition: {
name: "slideLeft",
duration: 380,
curve: "linear"
}
}
frame.topmost().navigate(navigationEntry)
}

React navigation Tab in Stack with button in Header

I'd like to have a TabNavigator embedded in a StackNavigator with a button in the Header used to navigate in the main Stack.
Here is a snack of my issue: https://snack.expo.io/#guigui64/tabs-in-stack
The propblem is when I create the TabNavigator:
const TabNavigator = createMaterialTopTabNavigator(
{
ScreenA,
ScreenB,
},
{
navigationOptions: {
headerTitle: 'Title when in tab',
headerRight: (
<Button
onPress={() => this.props.navigation.navigate('C')} // here is the issue !
title="ScreenC"
/>
),
},
}
);
I also tried creating a Component with static navigationOptions and have render() return a TabNavigator. In this case, the header appears fine but not the tabs.
Thanks in advance !

How to change the direction of the animation in StackNavigator?

How to change the direction of the animation in StackNavigator?
Current Behavior
When user goes to another screen, the screen flies from bottom to top.
Expected Behavior
When user goes to another screen, the screen flies from right to left. (Like Facebook or Instagram!)
StackNavigator Code
export default StackNavigator ({
Main: {
screen: MainScreen,
},
...
}, {
navigationOptions: ({navigation, screenProps}) => ({
tabBarOnPress: blahblaj
}),
lazy: true
// I guess we can do something here
});
If we can set the animation time, it will be even better! Currently it looks like the screen flies from middle of the screen to top. I want natural animation like Facebook or Instagram :)
Thanks in advance,
For react navigation > 5.0:
import {
CardStyleInterpolators,
createStackNavigator,
} from '#react-navigation/stack';
const Stack = createStackNavigator();
export default () => (
<Stack.Navigator
screenOptions={{
cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS
}}
>
<Stack.Screen name="Screen 1" component={ScreenComponent1} />
<Stack.Screen name="Screen 2" component={ScreenComponent2} />
</Stack.Navigator>
);
You may also want to use headerStyleInterpolator: HeaderStyleInterpolators.forUIKit
More info here: https://reactnavigation.org/docs/stack-navigator/#pre-made-configs
For react navigation < 5.0
On iOS it's standard behavior. Android requires a little bit of configuration. There are two options you can use to set screen transitions: mode and transitionConfig. In this case transitionConfig will work:
import CardStackStyleInterpolator from 'react-navigation/src/views/CardStack/CardStackStyleInterpolator';
// this path can be different depending on react-navigation version, this one is for #1.0.0-beta.15
export default StackNavigator ({
Main: {
screen: MainScreen,
},
...
}, {
transitionConfig: () => ({
screenInterpolator: CardStackStyleInterpolator.forHorizontal,
}),
})
We use CardStackStyleInterpolator from react-navigation source, but you can provide custom transition if you want, here is how to make one or here or this article.
mode is more for default behavior:
export default StackNavigator ({
Main: {
screen: MainScreen,
},
...
}, {
mode: 'card',
navigationOptions: ({navigation, screenProps}) => ({
tabBarOnPress: blahblaj
}),
lazy: true
});
mode can have only two values:
card - Use the standard iOS (right to left) and Android (bottom to
top) screen transitions. This is the default.
modal - Make the screens slide in from the bottom which is a common
iOS pattern. Only works on iOS, has no effect on Android.
For react navigation >= 5.0:
import {
CardStyleInterpolators,
createStackNavigator,
} from '#react-navigation/stack';
const Stack = createStackNavigator();
export default () => (
<Stack.Navigator
screenOptions={{
cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS
}}
>
<Stack.Screen name="Screen 1" component={ScreenComponent1} />
<Stack.Screen name="Screen 2" component={ScreenComponent2} />
</Stack.Navigator>
);
You may also want to use headerStyleInterpolator: HeaderStyleInterpolators.forUIKit
More info here: https://reactnavigation.org/docs/stack-navigator/#pre-made-configs
Updated answer:
import ReactNavigation from "react-navigation";
createStackNavigator({...},{
transitionConfig: () =>
ReactNavigation.StackViewTransitionConfigs.SlideFromRightIOS
})
Here,I just post my answer so that you can change the direction of the animation! That's all! The answer you have accepted is just default!
import CardStackStyleInterpolator from 'react-navigation/src/views/CardStackStyleInterpolator';
export default StackNavigator ({
Main: {
screen: MainScreen,
},
...
}, {
transitionConfig: () => ({
screenInterpolator: CardStackStyleInterpolator.forHorizontal,
}),
});
In this way, the screen transitions will become right to left on both two platforms!
What you need to pay more attention to is you can set any screen transitions whatever you want by using transitionConfig props!
The solution is very simple. In React navigation 4.x you can do like this
import { createAppContainer } from 'react-navigation'
import { createStackNavigator, StackViewTransitionConfigs } from 'react-navigation-stack';
const Navigation = createStackNavigator({
screenA: ComponentA,
screenB: ComponentB,
}, {
mode: 'card',
transitionConfig: () => StackViewTransitionConfigs.SlideFromRightIOS,
}
export const AppNavigation = createAppContainer(Navigation)
Note: You can achieve like this transition in previous react navigation versions also, but you have to change the import
animation​
How the screen should animate when pushed or popped.
Supported values:
default: use the platform default animation
fade: fade screen in or out
fade_from_bottom: fade the new screen from bottom
flip: flip the screen, requires stackPresentation: "modal" (iOS only)
simple_push: default animation, but without shadow and native header transition (iOS only, uses default animation on Android)
slide_from_bottom: slide in the new screen from bottom
slide_from_right: slide in the new screen from right (Android only, uses default animation on iOS)
slide_from_left: slide in the new screen from left (Android only, uses default animation on iOS)
none: don't animate the screen
Only supported on Android and iOS.

Electron tray.displayBalloon() not working from async calls

This works:
tray.displayBalloon({ title: 'my app', 'content': 'Access app settings from tray menu.' });
This does not:
setTimeout(function() {
tray.displayBalloon({ title: 'my app', 'content': 'Access app settings from tray menu.' });
}, 100);
Why?
P.S. I'm running it on Win10 and Electron 1.8.1
Looks like this is a known 'wontfix'
The solution is to provide an icon in the options for the displayBalloon call.
setTimeout(function () {
let img = nativeImage.createFromPath('some path to a png works');
tray.displayBalloon({
title: 'my app',
content: 'Access app settings from tray menu.',
icon: img
});
}, 100);
Make sure that your application is running at the moment when you call tray.displayBalloon. To prevent application quit you can handle will-quit event (see docs)
app.on('will-quit', function (event) {
event.preventDefault()
})
Calling event.preventDefault() in will-quit event handler will prevent the default behaviour, which is terminating the application.

Resources