-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: optimize tabBarItem by memoizing getter functions (#11427)
**Motivation** The goal of this PR is to memoize the result of getter functions passed to TabBarItem like: `getAccessibilityLabel()`. This is currently _the best_ we can do to optimize material-top-tabs re-renders. **Test plan** Go to Material top tabs example and examine re-renders using React Profiler.
- Loading branch information
1 parent
7d8b515
commit 1f94c8b
Showing
2 changed files
with
89 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import type { StyleProp, ViewStyle } from 'react-native'; | ||
import { Animated, StyleSheet } from 'react-native'; | ||
|
||
interface TabBarItemLabelProps { | ||
color: string; | ||
label?: string; | ||
labelStyle: StyleProp<ViewStyle>; | ||
icon: React.ReactNode; | ||
} | ||
|
||
export const TabBarItemLabel = React.memo( | ||
({ color, label, labelStyle, icon }: TabBarItemLabelProps) => { | ||
if (!label) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Animated.Text | ||
style={[ | ||
styles.label, | ||
icon ? { marginTop: 0 } : null, | ||
labelStyle, | ||
{ color: color }, | ||
]} | ||
> | ||
{label} | ||
</Animated.Text> | ||
); | ||
} | ||
); | ||
|
||
const styles = StyleSheet.create({ | ||
label: { | ||
margin: 4, | ||
backgroundColor: 'transparent', | ||
textTransform: 'uppercase', | ||
}, | ||
}); |