【发布时间】:2021-02-03 01:02:14
【问题描述】:
我正在尝试为我的通知标签显示标签栏徽章。如果用户有通知,在后端编写通知后,我将用户“hasNotifications”的字段设置为true。单击通知选项卡后,我将“hasNotifications”设置为 false。
这是我的实现:
function renderBadge() {
Firebase.firestore()
.collection('users')
.doc(Firebase.auth().currentUser.uid)
.onSnapshot(function(doc) {
if (doc.data().hasNotifications) {
console.log("true")
return true
}
else {
console.log("null")
return null
}
})
}
//Bottom Tabs
function Tabs() {
return (
<Tab.Navigator
initialRouteName="Home"
tabBarOptions={{
activeTintColor:"#FFFFFF",
inactiveTintColor:"#696969",
style: {
backgroundColor: '#000000',
borderTopColor: "transparent"
},
}}>
<Tab.Screen
name="Notificaton"
component={Notification}
options={{
tabBarLabel: ' ',
tabBarIcon: ({ color, size }) => (
<Ionicons name="md-notifications" size={size} color={color} />
),
tabBarBadge: renderBadge() <----- Render red dot if true, no red dot if null
}}
/>
</Tab.Navigator>
);
}
控制台日志显示监听器正在工作,并根据用户是否收到通知返回 true/null。但标签栏徽章不显示。我该如何解决这个问题?
编辑:看起来当我设置tabBarBadge: renderBadge() 时,徽章永远不会出现。当我设置tabBarBadge: renderBadge 时,徽章总是出现。侦听器工作正常,但事实并非如此。
编辑2:我把函数改成const renderBadge = () => {,还是不行。
【问题讨论】:
标签: javascript react-native react-native-navigation