Skip to content

Commit 88d8531

Browse files
fix: fix crash when change tab bar label during runtime (#250)
* fix: fix crash when change tab bar label during runtime * Apply suggestions from code review * docs: add changesets --------- Co-authored-by: Oskar Kwaśniewski <[email protected]>
1 parent c19fdef commit 88d8531

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-native-bottom-tabs': patch
3+
---
4+
5+
fix crash when change tab bar label during runtime

apps/example/src/Examples/NativeBottomTabs.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { Contacts } from '../Screens/Contacts';
44
import { Chat } from '../Screens/Chat';
55
import { createNativeBottomTabNavigator } from '@bottom-tabs/react-navigation';
66
import { Platform } from 'react-native';
7+
import { useState } from 'react';
78

89
const Tab = createNativeBottomTabNavigator();
910

1011
function NativeBottomTabs() {
12+
const [label, setLabel] = useState('Article');
1113
return (
1214
<Tab.Navigator
1315
initialRouteName="Chat"
@@ -40,11 +42,13 @@ function NativeBottomTabs() {
4042
console.log(
4143
`${Platform.OS}: Long press detected on tab with key ${data.target} at the screen level.`
4244
);
45+
setLabel('New Article')
4346
},
4447
}}
4548
options={{
4649
tabBarButtonTestID: 'articleTestID',
4750
tabBarBadge: '10',
51+
tabBarLabel: label,
4852
tabBarIcon: ({ focused }) =>
4953
focused
5054
? require('../../assets/icons/person_dark.png')

packages/react-native-bottom-tabs/android/src/main/java/com/rcttabview/RCTTabView.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,16 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {
197197
}
198198

199199
private fun onTabSelected(item: MenuItem) {
200-
val selectedItem = items.first { it.title == item.title }
200+
val selectedItem = items[item.itemId]
201201
selectedItem.let {
202202
onTabSelectedListener?.invoke(selectedItem.key)
203203
emitHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK)
204204
}
205205
}
206206

207207
private fun onTabLongPressed(item: MenuItem) {
208-
val longPressedItem = items.firstOrNull { it.title == item.title }
209-
longPressedItem?.let {
208+
val longPressedItem = items[item.itemId]
209+
longPressedItem.let {
210210
onTabLongPressedListener?.invoke(longPressedItem.key)
211211
emitHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
212212
}
@@ -220,6 +220,10 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {
220220
this.items = items
221221
items.forEachIndexed { index, item ->
222222
val menuItem = getOrCreateItem(index, item.title)
223+
if (item.title !== menuItem.title) {
224+
menuItem.title = item.title
225+
}
226+
223227
menuItem.isVisible = !item.hidden
224228
if (iconSources.containsKey(index)) {
225229
getDrawable(iconSources[index]!!) {
@@ -370,7 +374,7 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {
370374

371375
private fun updateTextAppearance() {
372376
if (fontSize != null || fontFamily != null || fontWeight != null) {
373-
val menuView = getChildAt(0) as? ViewGroup ?: return
377+
val menuView = bottomNavigation.getChildAt(0) as? ViewGroup ?: return
374378
val size = fontSize?.toFloat()?.takeIf { it > 0 } ?: 12f
375379
val typeface = ReactFontManager.getInstance().getTypeface(
376380
fontFamily ?: "",
@@ -403,7 +407,9 @@ class ReactBottomNavigationView(context: ReactContext) : LinearLayout(context) {
403407

404408
private fun updateTintColors(item: MenuItem? = null) {
405409
// First let's check current item color.
406-
val currentItemTintColor = items.find { it.title == item?.title }?.activeTintColor
410+
val currentItemTintColor = item?.itemId?.let { itemId ->
411+
items[itemId].activeTintColor
412+
}
407413

408414
// getDefaultColor will always return a valid color but to satisfy the compiler we need to check for null
409415
val colorPrimary = currentItemTintColor ?: activeTintColor ?: Utils.getDefaultColorFor(

0 commit comments

Comments
 (0)