reactjs 如何将屏幕替换为React Native的React导航

fcipmucu  于 2023-01-12  发布在  React
关注(0)|答案(5)|浏览(100)

如何将屏幕替换为React Native的React导航
现在我是一个新手,我不能理解getStateForAction,现在我在屏幕1上有参数,并通过传递参数导航到屏幕2,用户名为基本导航,它嵌套在堆栈上的屏幕1〉2
但是我需要在屏幕2处于活动状态之后用屏幕2替换屏幕1(替换类似于路由器通量上的ActionConst.REPLACE),并以新的方式发送参数
有人能给我带路吗谢谢。
屏幕1

onPress = () => {

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

  }

屏幕2

componentWillMount() {

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

}

---------

路由器

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

});
erhoui1w

erhoui1w1#

如果有人还在研究如何替换React Native中的屏幕,并且正在使用react-navigation,请阅读以下内容:

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

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

有关详细信息,请参阅:https://reactnavigation.org/docs/stack-actions/#replace

3zwjbxry

3zwjbxry2#

使用此代替导航。替换

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

jqjz2hbq3#

“替换”仅在堆栈导航中可用
替换键入的:

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-functionshttps://reactnavigation.org/docs/typescript#annotating-usenavigation

laawzig2

laawzig24#

您可以使用navigation.replace
用途

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

代替

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

您可以在这里找到更多信息https://reactnavigation.org/docs/en/navigation-key.html

0yg35tkg

0yg35tkg5#

这对我很有效。

import { View, Text, Button } from 'react-native'

const Home = (nav:any) => {
    const goToLogin = () => {
        nav.navigation.replace('Login');
    }
  return (
    <View>
      <Text>Home</Text>
      <Button title='Go To Login' onPress={goToLogin} />
    </View>
  )
}

export default Home;

相关问题