Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace screen with React Navigation for React Native

How can I replace screen with React Navigation for React Native

Now I'm a newbie I can't understand about getStateForAction now I have params on Screen 1 and Navigate to Screen 2 with passing params username with basic Navigate it's nested screen 1 > 2 on stack

But I need to replace screen 1 with screen 2 after screen 2 it's on active (replace like ActionConst.REPLACE on router flux) and sending params on a new way

Can someone guide me thank you.



screen 1

  onPress = () => {

          this.props.navigation.navigate('Sc2', {username: this.state.username});

  }


Screen 2

componentWillMount() {

const {state} = this.props.navigation;
console.log(state.params.username);

}

---------------

Router

export const LoginNavStack = StackNavigator({
  Sc1: {
    screen: Sc1
  },
  Sc2: {
   screen: Sc2,
 },

});
like image 970
boy_v Avatar asked Oct 18 '22 08:10

boy_v


2 Answers

Incase anyone's still figuring out how to replace screens in React Native and is using react-navigation, here it is:

import { StackActions } from '@react-navigation/native';

navigation.dispatch(
  StackActions.replace('Profile', {
    user: 'jane',
  })
);

For more information refer: https://reactnavigation.org/docs/stack-actions/#replace

like image 120
Aajinkya Singh Avatar answered Oct 21 '22 09:10

Aajinkya Singh


Use this instead of navigation.replace

navigation.reset({
 index: 0,
 routes: [{ name: 'Profile' }],
 });
like image 37
Sehrish Waheed Avatar answered Oct 21 '22 07:10

Sehrish Waheed