How to remove previous page in react native using #react-navigation/native - react-native

I want to finish my splash screen in react native how can I do this I have search a lot but did not find anything I'm using this #react-navigation/native.

Try to use react-native-splash-screen
import SplashScreen from 'react-native-splash-screen'
export default class App extends Component {
componentDidMount() {
// do stuff while splash screen is shown
// After having done stuff (such as async tasks) hide the splash screen
SplashScreen.hide();
}
}

Related

ReactNative TabBarIOS Undefined

Hell, I'm trying to create a tab bar in my react native app, but after importing it, it appears it's always undefined. Has this component been deprecated? I still see it listed in the docs. https://facebook.github.io/react-native/docs/tabbarios.html
import React, { Component } from 'react';
import {
TabBarIOS
} from 'react-native';
export default class App extends Component {
render() {
return (
<TabBarIOS/>
);
}
}
I'm using react-native 0.59.3
Looks like this has been removed as part of a core cleanup effort. There doesn't appear to be any native alternative that also behaves correctly on tvos.
https://github.com/facebook/react-native/commit/02697291ff41ddfac5b85d886e9cafa0261c8b98
I've gone ahead and extracted TabBarIOS out into a native module for anyone looking for this.
https://github.com/modavi/NativeIOS
install instructions:
npm install git+https://github.com/modavi/NativeIOS#master
react-native link native-ios

How To Use both 'adjustPan' and 'adjustResize' for 'windowSoftInputMode' in React Native Android app

How can I use both 'adjustPan' and 'adjustResize' in AndroidManifest.xml react native app.
Use Case
My navigation is made upon ReactNavigation with StackNavigator and TabNavigator. I have a text box where the user can type any data. While performing this, the tab bar is displaying on the top of Keyboard. In order to block this i used 'adjustPan' and it worked fine.
On another screen, I have a registration with multiple text boxes. Here I cant scroll the entire screen unless and clicking 'tick' on the keyboard or manually click system back button. To solve this issue I found 'KeyboardAvoidingView' which is working fine. but to activate this need to change 'windowSoftInputMode' to 'adjustResize'.
In documentation, found that these two have entirely different property and I can't both together. could someone help me on this?
References:https://medium.freecodecamp.org/how-to-make-your-react-native-app-respond-gracefully-when-the-keyboard-pops-up-7442c1535580
I found an npm package called react-native-android-keyboard-adjust, which allows us to switch the windowSoftInputMode on demand, this should be able to cater for your use case. However, the library seems to be not actively maintained and the installation documentation is a little bit out of date but for the most part, you can follow the instructions given by the README.md.
For the Update MainActivity.java in your project part, the recent versions of React Native should be able to auto-link the dependencies and there is no need to do this modification manually.
After the above steps, you can try to start your app. If you encountered an error related to something like The number of method references in a .dex file cannot exceed 64k, you can add the followings to your android/app/build.gradle file
android {
...
defaultConfig {
...
multiDexEnabled true
}
...
}
After installing the package, you can call the methods provided by the library to change the windowSoftInputMode as you need.
For example, assuming you have a default windowSoftInputMode of adjustResize, and you want to use adjustPan within ScreenA, you can call AndroidKeyboardAdjust.setAdjustPan() when ScreenA mount, and reset the windowSoftInputMode to adjustResize on unmount by calling AndroidKeyboardAdjust.setAdjustResize()
As of 2023, the best choice is react-native-avoid-softinput. react-native-android-keyboard-adjust isn't supported anymore.
You can use AvoidSoftInput.setAdjustPan and AvoidSoftInput.setAdjustResize.
I use custom hook to disable my default behavior on some screens.
import { useCallback } from 'react'
import { AvoidSoftInput } from 'react-native-avoid-softinput'
import { useFocusEffect } from '#react-navigation/native'
import { Platform } from 'react-native'
function useAndroidKeyboardAdjustNothing() {
useFocusEffect(
useCallback(() => {
if (Platform.OS === 'android') {
AvoidSoftInput.setAdjustNothing()
AvoidSoftInput.setEnabled(true)
}
return () => {
if (Platform.OS === 'android') {
AvoidSoftInput.setEnabled(false)
AvoidSoftInput.setAdjustResize()
}
}
}, []),
)
}

React native navigation tab logout function

So I have a react native app and I am using react-navigation tab navigator. My problem is I put my Logout function on the tab navigator too, but each tab requires a screen. Now what I want to happen is when I click Logout it doesn't have to navigate anywhere it just have to clear asyncstorage then redirect to Login screen.
Thanks in advance!
P.S. I am new to react-native.
I am guessing that you have logged in your user and store his info with AsyncStorage (session).
1) What i would do with react-navigation or react-native-router-flux is to load a component that on componentWillMount() function will trigger the logout function and redirect him on the scene you need. See the example below:
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { NavigationActions } from 'react-navigation'
import { logoutUser } from '../actions/index' //Action from Redux
class LogoutScene extends Component {
componentWillMount() {
this.props.logoutUser()
NavigationActions.navigate({ routeName: someRouteName })
}
}
const mapStateToProps = (state) => {}
export default connect(mapStateToProps, {logoutUser})(LogoutScene)
2) I highly recommend for you to use Redux on your project because although is hard and makes no sense at the beginning of a project in the long term makes your life so easy. In order to do though you will need something more than the above example. I think you should check this https://medium.com/#jonlebensold/getting-started-with-react-native-redux-2b01408c0053
Let me know if this makes sense

React Native get navigation object outside screen component

I need to be able to navigate and reset navigation stack from modules that are not necessarily screen components. It means, I can't use:
const {navigate} = this.props.navigation;
In my case, I need to properly redirect user to the right screen when he taps on push notification. I only have a callback:
onNotification: function(notification) {
// show correct screen and reset navigation stack
}
I just need more flexibility with navigation. So how can I do it for my case?
Here is my solution. I implemented base class for all screens. With this I always know what screen I am at and can always get navigation object to redirect user whenever needed:
import React, {Component} from 'react';
export default class Base extends Component {
static screen;
constructor(props) {
super(props);
Base.screen = this;
}
nav() {
return this.props.navigation;
}
}
In some other component/module I can just call this to navigate to a different screen:
Base.screen.nav().navigate(...);
I have created a navigation aware screen component to take care of it on screens.
For outside the screen you can directly access store.navigation. I have used it with redux in my example. See if this helps you.
https://github.com/aajiwani/react-navigation-aware-helper

how to get a splash screen first and then go the main home screen when using react-native-navigation?

I am using react-native 0.32.0 and "react-native-navigation": "^1.0.30", And I want to get a splash screen first and then go to the main home screen, where I begin to use react-native-navigation. I googled a lot and did a lot re research but still do not know how to make this. I try to get the simplest config passed to Navigation.startSingleScreenApp, but I still get the navbar in iOS. is it possible to get a raw splash screen first and then use react-native-navigation for navigation?
I implemented this through navigation experimental itself. you can refer this repository on github for navigation experimental https://github.com/jlyman/RN-NavigationExperimental-Redux-Example
I used setTimeout() function in the constructor of the page which I am loading first through my navigation experimental. The page in which I used this function became the splash screen of my app. Here is the code.
class FirstScreen extends Component{
constructor(props) {
super(props);
setTimeout(this.props.OnChange, 3000); //Constructor of Splash Screen page
}
//render() code
}
and here is my onChange() function inside mapDispatchToProps() which is calling navigatePush() action creater of my navigation experimental
OnChange: () => {
dispatch(navigatePush('Login'))
}