1- import React from 'react' ;
1+ import React , { useEffect } from 'react' ;
22import { createBottomTabNavigator } from '@react-navigation/bottom-tabs' ;
33import { getFocusedRouteNameFromRoute } from '@react-navigation/native' ;
44import DailyPage from '@/page/daily/DailyPage' ;
@@ -38,28 +38,25 @@ interface IProps {
3838//创建底部导航器
3939const Tab = createBottomTabNavigator < BottomParamList > ( ) ;
4040
41- class BottomTabs extends React . Component < IProps > {
42- componentDidMount ( ) {
43- this . setOptionTitle ( ) ;
44- }
41+ //hook方式
42+ //注意点:1.不能使用this 2.
43+ function BottomTabs ( props : IProps ) {
44+ //组件渲染完后都会调用useEffect方法
45+ useEffect ( ( ) => {
46+ setOptionTitle ( ) ;
47+ } ) ;
4548
46- componentDidUpdate ( ) {
47- this . setOptionTitle ( ) ;
48- }
49-
50- onPress = ( ) => {
51- const { navigation} = this . props ;
49+ const onPress = ( ) => {
50+ const { navigation} = props ;
5251 navigation . navigate ( 'SearchPage' ) ;
5352 } ;
5453
55- headerRight = ( ) => {
56- const { route} = this . props ;
54+ const headerRight = ( ) => {
55+ const { route} = props ;
5756 const routeName = getFocusedRouteNameFromRoute ( route ) ?? 'Daily' ;
5857 if ( routeName === 'Daily' ) {
5958 return (
60- < TouchableWithoutFeedback
61- style = { styles . headerRight }
62- onPress = { this . onPress } >
59+ < TouchableWithoutFeedback style = { styles . headerRight } onPress = { onPress } >
6360 < IconSearch />
6461 </ TouchableWithoutFeedback >
6562 ) ;
@@ -68,8 +65,8 @@ class BottomTabs extends React.Component<IProps> {
6865 }
6966 } ;
7067
71- setOptionTitle = ( ) => {
72- const { navigation, route} = this . props ;
68+ const setOptionTitle = ( ) => {
69+ const { navigation, route} = props ;
7370 const routeName = getFocusedRouteNameFromRoute ( route ) ?? 'Daily' ;
7471 if ( routeName === 'Mine' ) {
7572 navigation . setOptions ( {
@@ -91,81 +88,79 @@ class BottomTabs extends React.Component<IProps> {
9188 navigation . setOptions ( {
9289 headerShown : true ,
9390 headerTitle : title ,
94- headerRight : this . headerRight ,
91+ headerRight : headerRight ,
9592 } ) ;
9693 }
9794 } ;
9895
99- render ( ) {
100- return (
101- < Tab . Navigator
102- tabBarOptions = { {
103- activeTintColor : '#000000' ,
104- inactiveTintColor : '#9a9a9a' ,
105- } } >
106- < Tab . Screen
107- name = "Daily"
108- component = { DailyPage }
109- options = { {
110- tabBarLabel : '日报' ,
111- tabBarIcon : ( { focused} ) => {
112- return (
113- < Image
114- style = { styles . icon }
115- source = { focused ? ic_home_selected : ic_home_normal }
116- />
117- ) ;
118- } ,
119- } }
120- />
121- < Tab . Screen
122- name = "Discover"
123- component = { DiscoverTabs }
124- options = { {
125- tabBarLabel : '发现' ,
126- tabBarIcon : ( { focused} ) => {
127- return (
128- < Image
129- style = { styles . icon }
130- source = { focused ? ic_discovery_selected : ic_discovery_normal }
131- />
132- ) ;
133- } ,
134- } }
135- />
136- < Tab . Screen
137- name = "Hot"
138- component = { HotTabs }
139- options = { {
140- tabBarLabel : '热门' ,
141- tabBarIcon : ( { focused} ) => {
142- return (
143- < Image
144- style = { styles . icon }
145- source = { focused ? ic_hot_selected : ic_hot_normal }
146- />
147- ) ;
148- } ,
149- } }
150- />
151- < Tab . Screen
152- name = "Mine"
153- component = { MinePage }
154- options = { {
155- tabBarLabel : '我的' ,
156- tabBarIcon : ( { focused} ) => {
157- return (
158- < Image
159- style = { styles . icon }
160- source = { focused ? ic_mine_selected : ic_mine_normal }
161- />
162- ) ;
163- } ,
164- } }
165- />
166- </ Tab . Navigator >
167- ) ;
168- }
96+ return (
97+ < Tab . Navigator
98+ tabBarOptions = { {
99+ activeTintColor : '#000000' ,
100+ inactiveTintColor : '#9a9a9a' ,
101+ } } >
102+ < Tab . Screen
103+ name = "Daily"
104+ component = { DailyPage }
105+ options = { {
106+ tabBarLabel : '日报' ,
107+ tabBarIcon : ( { focused} ) => {
108+ return (
109+ < Image
110+ style = { styles . icon }
111+ source = { focused ? ic_home_selected : ic_home_normal }
112+ />
113+ ) ;
114+ } ,
115+ } }
116+ />
117+ < Tab . Screen
118+ name = "Discover"
119+ component = { DiscoverTabs }
120+ options = { {
121+ tabBarLabel : '发现' ,
122+ tabBarIcon : ( { focused} ) => {
123+ return (
124+ < Image
125+ style = { styles . icon }
126+ source = { focused ? ic_discovery_selected : ic_discovery_normal }
127+ />
128+ ) ;
129+ } ,
130+ } }
131+ />
132+ < Tab . Screen
133+ name = "Hot"
134+ component = { HotTabs }
135+ options = { {
136+ tabBarLabel : '热门' ,
137+ tabBarIcon : ( { focused} ) => {
138+ return (
139+ < Image
140+ style = { styles . icon }
141+ source = { focused ? ic_hot_selected : ic_hot_normal }
142+ />
143+ ) ;
144+ } ,
145+ } }
146+ />
147+ < Tab . Screen
148+ name = "Mine"
149+ component = { MinePage }
150+ options = { {
151+ tabBarLabel : '我的' ,
152+ tabBarIcon : ( { focused} ) => {
153+ return (
154+ < Image
155+ style = { styles . icon }
156+ source = { focused ? ic_mine_selected : ic_mine_normal }
157+ />
158+ ) ;
159+ } ,
160+ } }
161+ />
162+ </ Tab . Navigator >
163+ ) ;
169164}
170165
171166const styles = StyleSheet . create ( {
0 commit comments