【问题标题】:React-Navigation Push replaces the previous screen dataReact-Navigation Push 替换之前的屏幕数据
【发布时间】:2021-07-08 10:10:36
【问题描述】:

我正在使用navigation.push 导航到具有不同参数的同一屏幕,它用最近推送的屏幕替换以前的所有屏幕数据

我的堆栈根目录

      <Stack.Screen
        name='ViewProduct'
        component={ViewProduct}
        options={({ navigation, route }) => ({
          headerTitle: props => <Text style={{fontSize:16,fontWeight:'bold'}}>Product Details</Text>,
        })}
        />

我的产品视图屏幕

<TouchableOpacity delayPressIn={500} onPress={() => props.navigation.dispatch(StackActions.push('ViewProduct',{"uid":item.uid}))}>
     <View>
           <Image style={styles.prodImage} source={{uri:IMG_URL+'/images'+item.image_base_path+item.image_variant+'/m/'+item.cover_image}}/>                      
      </View>
 </TouchableOpacity>

需要的方法

In navigation.push

product-1 >> product-2 >> product-3 >> product-4

In goback

product-1 << product-2 << product-3 << product-4

当前问题

In navigation.push

product-1 >> product-2 >> product-3 >> product-4

In goback

product-4 << product-4 << product-4 << product-4

【问题讨论】:

标签: react-native react-navigation


【解决方案1】:

您必须使用 navigation.navigate('yourscreen')。这会产生您预期的输出。如下所述使用。

=> this.props.navigation.navigate('FirstScreen); 
=> this.props.navigation.navigate('SecondScreen'); 
=> this.props.navigation.navigate('ThirdScreen');

不要使用任何 push()dispatch() 方法。如果是基于类的导航,请使用以下方法。

例子:

this.props.navigation.navigate('DetailsS​​creen');

如果是功能组件,则从 @react-navigation/native 导入 useNavigation 道具。请看下面这个例子。

例子:

import * as React from 'react';
import { Button } from 'react-native';
import { useNavigation } from '@react-navigation/native';

function GoToButton({ screenName }) {
  const navigation = useNavigation();

  return (
    <Button
      title={`Go to ${screenName}`}
      onPress={() => navigation.navigate(screenName)}
    />
  );
}

【讨论】:

  • 我不想导航到不同的屏幕,我想用不同的参数导航同一个屏幕,例如产品视图屏幕对于同一个产品有不同的颜色变体,所以当我们触摸它将触发的图像打开一个新屏幕以显示产品的所选颜色,这种方法工作正常,但我们回到上一个屏幕,它仍然显示最后选择的产品详细信息
猜你喜欢
  • 2017-12-31
  • 2017-11-01
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 2020-05-29
  • 2019-07-08
  • 1970-01-01
  • 2020-04-20
相关资源
最近更新 更多