在 React Native 中向 Stack Navigator 添加 if 语句
Posted
技术标签:
【中文标题】在 React Native 中向 Stack Navigator 添加 if 语句【英文标题】:Adding an if statement to a Stack Navigatior in React Native 【发布时间】:2021-11-22 10:12:55 【问题描述】:我有一个“聊天”屏幕,可以从我的 React Native 应用程序中的多个屏幕访问。我正在尝试添加带有“route.params”的“if 语句”?到堆栈导航器,因此它将根据用户的来源将用户导航回正确的屏幕,但我无法执行“if语句”。
下面是我的代码:
function MainTabScreen ( navigation, route )
const didComeFromNextScreen = () => route.params
const didComeFromOtherScreen = () => route.params
const chatChecker = ()=>
if(route.params?.didComeFromNextScreen)
console.log('It came from NextScreen')
navigation.navigate('NextScreen')
return;
if(route.params?.didComeFromOtherScreen)
console.log('It came from OtherScreen')
navigation.navigate('OtherScreen')
return;
<MainTab.Navigator screenOptions =
headerStyle:
backgroundColor:"#fff" ,
,
gestureEnabled: false,
headerTintColor: 'red',
headerTitleStyle:
fontWeight: 'bold',
fontSize: height*0.023,
textAlign: 'center',
>
<MainTab.Screen name = "NextScreen" component=NextScreen options=
title: '' ,
headerTintColor: '#fff',
headerLeft: null,
headerRight: () => (
<Icon.Button name = "message" size=25 backgroundColor= "#fff" onPress= () => navigation.navigate('ChatScreen',
didComeFromNextScreen: true
)>
</Icon.Button>
/>
<MainTab.Screen name = "ChatScreen" component=ChatScreen options=
title: '' ,
headerTintColor: '#fff',
headerLeft: ()=>(
<Icon.Button name = "arrow-left" size=25 backgroundColor= "#fff" onPress=chatChecker/>
)
/>
<MainTab.Screen name = "OtherScreen" component=OtherScreen options=
title: '' ,
headerTintColor: '#fff',
headerLeft: null,
headerRight: () => (
onPress= () => navigation.navigate('ChatScreen',
didComeFromOtherScreen: true
)>
/>
</MainTab.Navigator>
【问题讨论】:
【参考方案1】:我不确定你为什么要制作这些函数,但函数总是正确的,所以它不会工作
const didComeFromNextScreen = () => route.params
const didComeFromOtherScreen = () => route.params
更新到
const didComeFromNextScreen = route.params
const didComeFromOtherScreen = route.params
【讨论】:
这不起作用,仍然不允许我向后导航以上是关于在 React Native 中向 Stack Navigator 添加 if 语句的主要内容,如果未能解决你的问题,请参考以下文章
如何在 React Native 中向 NavigatorIOS 添加右键?