[英]React-navigation: How to set initialParams for screen based on condition?
I have BottomTabsNavigator
as part of StackNavigator
.我有
BottomTabsNavigator
作为StackNavigator
的一部分。
When I launch the app, I need to pass initialParams
in Home tab based on a condition in BottomTabsNavigator
.当我启动应用程序时,我需要根据
BottomTabsNavigator
中的条件在主页选项卡中传递initialParams
。
Apparently, BottomTabsNavigator is rendered once only and initialParams always sends default value instead of new value based on condition.显然,BottomTabsNavigator 仅呈现一次,而 initialParams 始终根据条件发送默认值而不是新值。
<Tab.Screen
name="Home"
component={HomeScreen}
options={{
title: 'Home',
tabBarIcon: 'home-outline',
tabBarLabel: 'Home',
}}
initialParams={{ 'tappedNotification1': notificationOpened }} // <---- here I want to send notificationOpened value when its value is updated,
/>
I use below hook to update value for notificationOpened
to true (which needs to be sent as initialParams
for Home screen.我使用下面的挂钩将
notificationOpened
的值更新为 true(需要作为主屏幕的initialParams
发送。
function onOpened(openResult) {
navigation.navigate('NotificationDetailsScreen', {
...openResult.notification.payload.additionalData,
tappedNotification: true,
isRead: false,
});
setNotificationOpened(true);
}
OneSignal.addEventListener('opened', onOpened);
return () => {
OneSignal.removeEventListener('opened', onOpened);
}; // unsubscribe on unmount
}, [navigation, user]);
Updated Comment:更新评论:
@Guruparan Giritharan I did exactly the same using your suggestion. @Guruparan Giritharan 我根据你的建议做了完全相同的事情。 its a little hard to explain but please stay with me.
这有点难以解释,但请留在我身边。
In my BottomTabsNavigator
I declare a state 'notificationOpened' with intialValue false and pass it to NotificationContext.Provider
value.在我的
BottomTabsNavigator
,我声明了一个 state 'notificationOpened' with intialValue false 并将其传递给NotificationContext.Provider
值。 which is accessible in Home.可在主页中访问。
Home
screen has a modal popup which should display based on the value received in context's notificationOpened
(modal should display when notificationOpened is false)
Home
屏幕有一个模态弹出窗口,应根据上下文的notificationOpened
中收到的值(modal should display when notificationOpened is false)
in my case, I update notificationOpened
value from BottomTabsNavigator
to true so modal won't display.在我的例子中,我将
notificationOpened
值从BottomTabsNavigator
更新为 true,因此模式不会显示。
but Home
receives false
from context at the beginning and show the modal.但是
Home
在开始时从上下文中接收到false
并显示模态。 Hope that makes sense.希望这是有道理的。
The official documentation recommends using context or some sort of store like redux to update the count variables on the tabs.官方文档建议使用上下文或类似 redux 的某种存储来更新选项卡上的计数变量。 You have a similar requirement.
你有类似的要求。 You can look at this sample to get an idea to do that.
您可以查看此示例以获得执行此操作的想法。
First you will need to create a context file首先,您需要创建一个上下文文件
const NotificationContext = React.createContext(0);
export default NotificationContext;
And the file which contains your tabs以及包含您的选项卡的文件
const MyTabs = () => {
const [count, setCount] = React.useState(0);
return (
<NotificationContext.Provider value={count}>
<View style={{ flex: 1 }}>
<Text>{count}</Text>
<Button title="count" onPress={() => setCount(count + 1)} />
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} options={{ title: 'My home' }}/>
<Tab.Screen name="Settings" component={SettingsScreen} options={{ title: 'My home 2' }}/>
</Tab.Navigator>
</View>
</NotificationContext.Provider>
);
};
And the Homescreen file can read and update itself using the 'usecontext'主屏幕文件可以使用“usecontext”读取和更新自身
import NotificationContext from './NotificationContext';
export function HomeScreen() {
const count = React.useContext(NotificationContext);
return (
<View>
<Text>{count}</Text>
</View>
);
}
This sample is based on the snack you provided.此示例基于您提供的零食。 https://snack.expo.io/@guruparan/tabs-with-context
https://snack.expo.io/@guruparan/tabs-with-context
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.