Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (How To Disable Screen Rotation In React Native).
In this note series, we will learn how to Disable Screen Rotation In React Native. Handheld mobile devices always have two orientation mode Portrait mode and Landscape mode because of their design to hold in the hand. Portrait mode is by default mode in mobile devices, and if we rotate the device, it turns into Landscape mode.
So Let’s begin.
For Android Devices
Open Project -> android -> app -> src -> main ->AndroidManifest.xml .
Now, we have to put android:screenOrientation=“portrait” in AndroidManifest file.
After changing AndroidManifest.xml file, we need to re-run our project. This would disable the landscape mode in our current application in android devices.
For iOS Devices
Open Project ->ios -> Project_Name.xcodeproj file in XCode.
After opening the project in XCode, select project name.
Now Goto -> General -> Deployment Info and only select the Portrait mode.
Now Re-run our project. This would disable the landscape mode in our current application in iOS devices.
Conclusion
In this note series, we understood how to Disable Screen Rotation in React Native. This article showed the Disable Screen Rotation Steps in React Native.
Thanks for reading ! I hope you enjoyed and learned about Disable Screen Rotation Concepts in React Native. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :
Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (Restart/Reset Current Screen In React Native Without Mounting It Again).
In this note series, We will learn how to Restart / Reset Current Screen In React Native without mounting it again. React Native does not provide the default refresh component to restart the screen. In some cases, we need to restart or reset the screen again. If we are using React Navigation then we have a choice to mount the screen again and to refresh the screen have a look at Refresh Previous Screen after Going Back in React Navigation but in the normal case, if we need to refresh the screen then react-native-restart can help us by providing RNRestart Component.
So Let’s begin.
To Import the Component
import RNRestart from 'react-native-restart';
To Restart or Reset The Current Screen In React Native
RNRestart.Restart();
Example
In this example, we are going to make a single screen with a setInterval which will update the counter in every second. We will have a button below the counter and on the press of the button, we will refresh the screen.
Example Project Setup
To demonstration of Restart/Reset Current Screen, we have to follow the below steps:
Create a new React Native project
Install the Dependency
Install CocoaPods
1. Create a new React Native project
Assuming that we have node installed, we can use npm to install the react-native-clicommand line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project.
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.
2. Install the Dependency
To install the dependencies, open the terminal and jump into our project
cd ProjectName
Now run the following commands to install the dependencies
npm install react-native-restart --save
3. Install CocoaPods
We need to install pods for the iOS
cd ios && pod install && cd ..
Example Code to Restart/Reset Current Screen In React Native
Now Open App.js in any code editor and replace the code with the following code.
// Restart/Reset Current Screen in React Native Without Mounting it Again
// import React in our code
import React, { useState, useEffect } from "react";
// import all the components we are going to use
import {
SafeAreaView,
Text,
View,
Button,
StyleSheet,
TextInput,
} from "react-native";
import RNRestart from "react-native-restart";
const App = () => {
const [value, setValue] = useState("");
useEffect(() => {
// Resetting default value for the input on restart
setValue("Default Value");
}, []);
const onButtonClick = () => {
RNRestart.Restart();
};
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<View style={styles.container}>
<Text style={styles.heading}>Example of React Native Restart</Text>
<Text style={styles.paragraph}>
Insert any value in Input and Click Restart Screen. You will see
blank TextInput again.
</Text>
<TextInput
placeholder="Please Insert Something"
defaultValue={value}
placeholderTextColor="#808080"
underlineColorAndroid="transparent"
onChangeText={(text) => setValue(text)}
style={styles.textInputStyle}
/>
<View style={{ marginTop: 20 }}>
<Button title="Restart Screen" onPress={onButtonClick} />
</View>
</View>
<Text
style={{
fontSize: 18,
textAlign: "center",
color: "grey",
}}
>
Restart/Reset Current Screen in React Native
</Text>
<Text
style={{
fontSize: 16,
textAlign: "center",
color: "grey",
}}
>
www.coolmonktechie.com
</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "white",
padding: 10,
},
textStyle: {
textAlign: "center",
fontSize: 18,
color: "black",
},
heading: {
fontSize: 18,
fontWeight: "bold",
textAlign: "center",
},
paragraph: {
fontSize: 16,
marginVertical: 16,
textAlign: "center",
},
textInputStyle: {
fontSize: 25,
textAlign: "center",
height: 50,
backgroundColor: "#d6d6d6",
},
});
export default App;
To Run the React Native Example Code
Open the terminal again, and jump into our project using
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running (macOS only)
react-native run-ios (macOS only).
The output of example code is as below:
After click the Restart Screen button, the output is as below :
Conclusion
In this note series, we understood how to Restart/Reset Current Screen in React Native Without mounting it again. This article showed the example code to Restart/Reset Current Screen in React Native.
Thanks for reading ! I hope you enjoyed and learned about Restart/Reset Current Screen Concepts in React Native. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :
Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (How Authentication Works In React Native ?) .
In this note series, we will learn how Authentication Works In React Native. Mostly Authentication is done by the OAuth2 Authentication Protocol in React Native application. This protocol is incredibly popular nowadays, prided as the most complete and secure protocol around. The OpenID Connect protocol is also based on this protocol.
So Let’s begin.
OAuth2 and Redirects
The OAuth2 authentication protocol is incredibly popular and the most complete and secure protocol around. In OAuth2, the user asks to the authenticate via a third party. On successful completion, this third party redirects back to the requesting application with a verification code which can be exchanged for a JWT — a JSON Web Token. JWT is an open standard for securely transmitting information between parties on the web.
PKCE (Proof of Key Code Exchange)
On the web, this redirect step is secure, because it guarantees URLs to be unique on the web. This is not true for apps because, as mentioned earlier, there is no centralized method of registering URL schemes! In order to address this security concern, an additional check must be added in the form of PKCE.
PKCE, pronounced “Pixy” stands for Proof of Key Code Exchange, and is an extension to the OAuth 2 spec. This involves adding a layer of security, which verifies that the authentication and token exchange requests come from the same client. PKCE uses the SHA 256 Cryptographic Hash Algorithm. SHA 256 creates a unique “signature” for a text or file of any size, but it is:
Always the same length regardless of the input file.
Guaranteed to be always produce the same result for the same input.
One way (that is, we can’t reverse engineer it to reveal the original input).
Now we have two values:
code_verifier – a large random string generated by the client.
code_challenge – the SHA 256 of the code_verifier.
During the initial /authorize request, the client also sends the code_challenge for the code_verifier , it keeps in memory. After the authorize request has returned correctly, the client also sends the code_verifierthat was used to generate the code_challenge. The IDP will then calculate the code_challenge, see if it matches what was set on the very first /authorize request, and only send the access token if the values match.
This guarantees that only the application that triggered the initial authorization flow would be able to successfully exchange the verification code for a JWT. So even if a malicious application gets access to the verification code, it will be useless on its own.
OAuth Library
A library to consider for native OAuth is react-native-app-auth. React-native-app-auth is an SDK for communicating with OAuth2 providers. It wraps the native AppAuth-iOS and AppAuth-Android libraries and can support PKCE. React-native-app-auth can support PKCE only if our Identity Provider supports it.
Conclusion
In this note series, we understood how Authentication works in React Native. We discussed the work flow of OAuth2 Authentication Protocol with PKCE in react native.
Thanks for reading ! I hope you enjoyed and learned about OAuth2 Authentication Protocol concept in React Native. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe to the blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :
Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (How To Control Component Size In React Native ?).
In this note series, we will learn how to control component size in React Native. A component’s height and width determine its size on the screen.
So Let’s begin.
Fixed Dimensions
The general way to set the dimensions of a component is by adding a fixed width and height to style. All dimensions in React Native are unitless and represent density-independent pixels.
Setting dimensions this way is common for components that should always render at exactly the same size, regardless of screen dimensions.
The Preview of the above example is :
Flex Dimensions
We use flex in a component’s style to have the component expand and shrink dynamically based on available space. Normally we will use flex: 1, which tells a component to fill all available space, shared evenly amongst other components with the same parent. The larger the flex given, the higher the ratio of space a component will take compared to its siblings.
A component can only expand to fill available space if its parent has dimensions greater than 0. If a parent does not have either a fixed width and height or flex, the parent will have dimensions of 0 and the flex children will not be visible.
Flex Dimensions
import React from 'react';
import { View } from 'react-native';
const FlexDimensionsBasics = () => {
return (
// Try removing the `flex: 1` on the parent View.
// The parent will not have dimensions, so the children can't expand.
// What if you add `height: 300` instead of `flex: 1`?
<View style={{flex: 1}}>
<View style={{flex: 1, backgroundColor: 'powderblue'}} />
<View style={{flex: 2, backgroundColor: 'skyblue'}} />
<View style={{flex: 3, backgroundColor: 'steelblue'}} />
</View>
);
};
export default FlexDimensionsBasics;
The Preview of the above example is :
Conclusion
In this note series, we understood how to control component size in React Native. This note series showed Fixed Dimensions and Flex Dimensions to control the component size in react native.
Thanks for reading! I hope you enjoyed and learned about Fixed Dimensions and Flex Dimensions concepts in React Native. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe to the blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :
Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (How to Improve the Performance of React Native App?).
In this note series, we will understand about the pro tips to improve the performance of the React Native app. There are so many ways to do this but here we will discuss few most preferred ways as below:
Use of shouldComponentUpdate method of react-native life cycle
Pure Components of React
Use Memo from Hooks API
Build the module with native whenever it is possible
Remove the console logs before building the application
Reduce Application size
So Let’s begin.
1. Use of shouldComponentUpdate method of react-native life cycle
Using shouldComponentUpdate method, we can reduce un-necessary re-rendering of components.
shouldComponentUpdate returns the boolean value and according to that components will re-render. For example, if it returns true then it will re-render and if it returns false then it will not re-render component.
So, on state update or on new props to the component, this method will be called with two arguments called nextState and nextProps. So, we can compare the new props and state with the previous ones. And according to our business logic requirements, we can use the boolean to render the component again.
2. Pure Components of React
The pure component of React helps to render only the specific props that have been changed or the state has updated.
For example, if we are using a flatlist which is rendering 1000 rows. Now, on every re-render, that 1000 data will be rendered again.
Using Flatlist in the pure component, we can ensure that only the change on required props components will re-render again.
3. Use Memo from Hooks API
React Hooks provide the life cycle of components to functions.
Memo from react helps to do a shallow comparison on upcoming changes. So, it allows re-rendering functions only if the props get changed.
4. Build the module with the native driver whenever it is possible
Here, We are not talking about not using React Native. Sometimes, we have to use some features which can be done by the native more efficiently. We build that module in native one and can use the feature from the React Native by creating a bridge.
5. Remove the console logs before building the application
It has been identified that the console logs are taking some amount of time for logging purposes. So, it is good to use at the time of development but in the production build, it should be removed which will help to improve the performance of the app which we will realize while using it.
6. Reduce Application size
In React Native, we use external libraries and components form libraries so it impacts application size.
To reduce the size, we have to optimize the resources, use ProGaurd, create different app sizes for different device architectures, and make sure to compress the graphics elements, i.e. images.
The components on the JavaScript side use a bridge to communicate with the Native side. Reduce the load on the bridge and improve the app’s performance.
Conclusion
In this note series, we understood about the pro tips to improve the performance of the React Native app.
Thanks for reading! I hope you enjoyed and learned about the React Native app Performance Improvement pro tips. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe to the blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :
Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (Functional vs Class Components in React Native).
In this note series, we will understand about the difference between Functional and Class Components in React Native.
So Let’s begin.
In React Native, there are two main types of components that make up an application: functional components and class components. These are structured the same as they would be in a regular React app for the web.
Class Components vs Functional Components
1. Syntax
Class components are JavaScript ES2015 classes that extend a base class from React called Component and create a render function that returns a React element. This requires more code as well.
In Class components, there is a way to store and manage state built in to React Native. This gives the class App access to the React life cycle methods like render as well as state/props functionality from the parent. While managing the state in classes, we use setState and this.state to set and get the state, respectively.
Another most important difference in Class and Functional Component is the life cycle methods, or we can say Lifecycle Hooks. We all know how important is the Lifecycle methods while developing any React Native application. Life cycle methods give us the power to control the data and application flow on different activities. We are very much familiar with the class component Lifecycle methods but in case of Functional Components we have to manage all this with the help of useEffect hook.
In this note series, we understood about the difference between Functional and Class Components in React Native. We conclude that :
Class components are used as container components to handle state management and wrap child components.
Functional components are just used for display purposes – these components call functions from parent components to handle user interactions or state updates.
Thanks for reading! I hope you enjoyed and learned about Functional Vs Class Components Concepts in React Native. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe to the blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :
Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (7 Quick Valuable Commands To Improve React Native Development).
In this note series, we will learn about seven quick valuable commands which is commonly used and helps to improve React Native Development to start developing iOS and Android apps.
So, Let’s begin.
1. Starting a new project
There are different ways, we can bootstrap a react native application. You can use Expo or create-react-native-app (which in turns uses Expo-Cli) to start our new project, but with this method, we are in more control of what happened in our project and can communicate, tweak and write our own modules with native libraries for iOS and Android mobile platform.
react-native init [PROJECT-NAME]
cd [PROJECT-NAME]
2. Run app in Android Emulator
This command is self explanatory and as it says it will start the Android emulator and install the app you just created. You need to be in the root of the project to run this command.
react-native run-android
3. Run app in iOS Emulator
This command do exactly the same as react-native run-android but instead of the Android emulator, it opens the iPhone simulator.
react-native run-ios
4. Link dependencies to native projects
Some libraries have dependencies that need to be linked in the native code generated for React Native. If something doesn’t work after you installed a new library, maybe is because you skip this step.
react-native link [LIBRARY-NAME]
5. Clear bundle
If something don’t run as expected, maybe you need to clear and create a new bundle with this command.
watchman watch-del-all
6. Support decorators
JSX doesn’t support decorators by default so you need to install the Babel plugin to make it work.
With the following commands you will have and unsigned apk so you can install and share with your colleagues for testing purposes. Just remember that this apk is not ready to upload to the App Store or production. You will find your fresh apk in android/app/build/outputs/apk/app-debug.apk.
In this note series, we understood about seven quick valuable commands which is commonly used in React Native Development.
Thanks for reading! I hope you enjoyed and learned about React Native Commands List. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe to the blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :
Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (7 Most Common Glossary Terms Used In React Navigation).
In this note series, we will learn about commonly used React Navigation glossary terms in React Native. React Navigation glossary terms is the most common question asked in an interview with the experience react native developer. This note series shows some commonly used glossary terms of React Navigation in React Native.
So, Let’s begin.
1. Header
We also know header as navigation header, navigation bar, nav bar, and probably many other things. This is the rectangle at the top of your screen that contains the back button and the title for your screen. It often refers the entire rectangle to as the header in React Navigation.
2. Navigator and Navigation Container
A Navigator contains Screen elements as its children to define the configuration for routes.
Navigation Container is a component which manages our navigation tree and contains the navigation state. This component must wrap all navigators structure.
Usually, we would render this component at the root of our app, which is usually the component exported from App.js.
function App() {
return (
<NavigationContainer>
<Stack.Navigator> // <---- This is a Navigator
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
3. Screen Component
A screen component is a component that we use in our route configuration.
We saw earlier that our screen components are provided with the navigation prop. It’s important to note that this only happens if the screen is rendered as a route by React Navigation.
For example, if we render DetailScreen as a child of HomeScreen, then DetailsScreen won’t be provided with the navigation prop, and when you press the “Go to Details.. again” button on the Home Screen, the app will throw one of JavaScript exceptions as “undefined is not an object”.
Each screen component in your app is provided with the navigation prop automatically. This prop will be passed into all screens, and it can be used for the following :
dispatch will send an action up to the router
navigate, goBack are available to dispatch actions in a convenient way.
Navigators can also accept a navigation prop, which they should get from the parent navigator, if there is one.
5. Router Prop
This prop will be passed into all screens. It contains information about current route, i.e. params, key and name.
6. Navigation State
The state of a navigator looks something like this :
For this navigation state, there are two routes which may be tabs, or cards in a stack. The index shows the active route, which is “B”.
7. Route
Each route is a piece of navigation state which contains a key to identify it, and a “name” to designate the type of route.It can also contain arbitrary params.
In this note series, we understood the React Navigation glossary terms in React Native.
Thanks for reading! I hope you enjoyed and learned about React Navigation glossary terms. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe to the blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :