1- import { useState } from 'react' ;
2- import { StyleSheet , View , Text , Dimensions , SafeAreaView } from 'react-native' ;
1+ import { useCallback , useMemo , useRef } from 'react' ;
2+ import { StyleSheet , View , Text , Dimensions , FlatList } from 'react-native' ;
33import { TabBar } from 'react-native-bottom-tab-bar' ;
44
5- let { width } = Dimensions . get ( 'window' ) ;
5+ const { width : screenWidth } = Dimensions . get ( 'window' ) ;
66interface TabDataType {
77 name : string ;
88 id : number ;
9+ bgColor : string ;
910}
1011
1112const tabData : Array < TabDataType > = [
1213 {
1314 name : 'Home' ,
1415 id : 1 ,
16+ bgColor : '#FFC0C7' ,
1517 } ,
1618 {
1719 name : 'Cart' ,
1820 id : 2 ,
21+ bgColor : '#FF7128' ,
1922 } ,
2023 {
2124 name : 'Search' ,
2225 id : 3 ,
26+ bgColor : '#0088cc' ,
2327 } ,
2428 {
2529 name : 'Profile' ,
2630 id : 4 ,
31+ bgColor : '#ff6666' ,
2732 } ,
2833 // {
2934 // name: 'Setting',
3035 // id: 5,
36+ // bgColor: '#66ff99'
3137 // },
3238] ;
3339
3440export default function App ( ) {
35- const [ bgColor , setBgColor ] = useState ( '#FFC0C7' ) ;
36- const [ tabs , setTabs ] = useState < Array < TabDataType > | [ ] > ( tabData ) ;
41+ const tabs = useMemo ( ( ) => tabData , [ ] ) ;
42+ const flatListRef = useRef < FlatList > ( null ) ;
3743
38- const onTabChange = ( item : TabDataType ) => {
39- let tempTabs = [ ...tabs ] ;
40- setTimeout ( ( ) => {
41- tempTabs . map ( ( val ) => {
42- if ( item . name === 'Home' && val . name === 'Home' ) {
43- // val.activeIcon = Object.assign({}, activeHome(true))
44- setBgColor ( '#FFC0C7' ) ;
45- } else if ( item . name === 'Cart' && val . name === 'Cart' ) {
46- // val.activeIcon = Object.assign({}, activeList(true))
47- setBgColor ( '#FF7128' ) ;
48- } else if ( item . name === 'Search' && val . name === 'Search' ) {
49- // val.activeIcon = Object.assign({}, activeCamera(true))
50- setBgColor ( '#0088cc' ) ;
51- } else if ( item . name === 'Setting' && val . name === 'Setting' ) {
52- // val.activeIcon = Object.assign({}, activeNotification(true))
53- setBgColor ( '#ff6666' ) ;
54- } else if ( item . name === 'Profile' && val . name === 'Profile' ) {
55- // val.activeIcon = Object.assign({}, activeUser(true))
56- setBgColor ( '#66ff99' ) ;
57- } else {
58- // val.activeIcon = null
59- }
60- } ) ;
61-
62- setTabs ( tempTabs ) ;
63- } , 500 ) ;
64- } ;
44+ const onTabChange = useCallback ( ( _item : TabDataType , index : number ) => {
45+ flatListRef . current ?. scrollToIndex ( {
46+ animated : true ,
47+ index : index ,
48+ } ) ;
49+ } , [ ] ) ;
6550
6651 return (
6752 < View style = { styles . container } >
68- < View
69- style = { {
70- backgroundColor : bgColor ,
71- flex : 1 ,
72- justifyContent : 'center' ,
73- alignItems : 'center' ,
53+ < FlatList
54+ ref = { flatListRef }
55+ data = { tabs }
56+ keyExtractor = { ( item ) => item . id . toString ( ) }
57+ renderItem = { ( { item } : { item : TabDataType } ) => {
58+ return (
59+ < View
60+ style = { [
61+ styles . slide ,
62+ {
63+ backgroundColor : item . bgColor ,
64+ } ,
65+ ] }
66+ >
67+ < Text > { item . name } </ Text >
68+ </ View >
69+ ) ;
7470 } }
75- >
76- < Text > App component</ Text >
77- </ View >
71+ scrollEnabled = { false }
72+ horizontal
73+ bounces = { false }
74+ pagingEnabled
75+ showsHorizontalScrollIndicator = { false }
76+ />
7877 < TabBar
7978 tabs = { tabs as Array < TabDataType > }
8079 onTabChange = { onTabChange }
8180 // defaultActiveTabIndex={1}
82- containerWidth = { width }
81+ containerWidth = { screenWidth }
8382 tabBarBackground = { '#fff' }
8483 defaultActiveTabIndex = { 0 }
8584 // transitionSpeed={100}
@@ -91,13 +90,15 @@ export default function App() {
9190const styles = StyleSheet . create ( {
9291 container : {
9392 flex : 1 ,
94- // backgroundColor: '#ccc',
95- // alignItems: 'center',
96- // justifyContent: 'center',
9793 } ,
9894 box : {
9995 width : 60 ,
10096 height : 60 ,
10197 marginVertical : 20 ,
10298 } ,
99+ slide : {
100+ width : screenWidth ,
101+ justifyContent : 'center' ,
102+ alignItems : 'center' ,
103+ } ,
103104} ) ;
0 commit comments