score:1

You can use navigation.replace

Use

this.props.navigation.replace('Sc2');

Instead of

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

You can find more from here https://reactnavigation.org/docs/en/navigation-key.html

score:1

Use this instead of navigation.replace

navigation.reset({
 index: 0,
 routes: [{ name: 'Profile' }],
 });

score:1

"Replace" is only available in stack navigation

replace typed:

import { useNavigation } from '@react-navigation/core'
import { StackNavigationProp } from '@react-navigation/stack'

type YourNavigatorParamList = {
  CurrentScreen: undefined
  DifferentScreen: { username: string }
}

const navigation = useNavigation<StackNavigationProp<YourNavigatorParamList, 'CurrentScreen'>>()

navigation.replace('DifferentScreen', { username: 'user01' })

https://reactnavigation.org/docs/navigation-prop/#navigator-dependent-functions https://reactnavigation.org/docs/typescript#annotating-usenavigation

score:6

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


Related Query

More Query from same tag